import { ChangeEvent, useEffect, useState } from 'react'; import styled from '@emotion/styled'; import { css } from '@emotion/react'; import { rgba } from 'polished'; import { nobel, primary, white } from 'styles/colors'; import noop from 'utils/noop'; const Container = styled.label<{ checked?: boolean }>` position: relative; display: inline-block; width: 48px; height: 24px; `; const Input = styled.input` width: 0; height: 0; opacity: 0; `; const Knob = styled.span` position: absolute; top: 0; right: 0; bottom: 0; left: 0; border: 1px solid ${nobel['400']}; border-radius: 24px; background-color: ${white}; transition: 0.3s; cursor: pointer; ${({ theme: { rtl } }) => css` &:before { content: ''; position: absolute; bottom: 2px; ${rtl ? 'right' : 'left'}: 3px; width: 18px; height: 18px; border-radius: 50%; background-color: ${nobel['400']}; transition: 0.3s; } &:after { box-sizing: border-box; content: ''; position: absolute; top: 4px; ${rtl ? 'right' : 'left'}: 9px; visibility: hidden; width: 6px; height: 11px; border: solid ${primary.main}; border-width: 0 2px 2px 0; transform: rotate(45deg); transition: 0.3s; transition-property: transform; } input:disabled ~ & { opacity: 0.5; cursor: not-allowed; } input:checked ~ & { border-color: ${primary.main}; background-color: ${primary.main}; } input:checked ~ &:before { background-color: ${white}; transform: ${rtl ? 'translateX(-22px)' : 'translateX(22px)'}; } input:checked ~ &:after { visibility: visible; transform: ${rtl ? 'translateX(-22px)' : 'translateX(22px)'} rotate(45deg); } input:active:not(:disabled) ~ &, input:focus:not(:disabled) ~ & { box-shadow: 0 0 0 4px ${rgba(nobel['400'], 0.2)}; } input:active:checked:not(:disabled) ~ &, input:focus:checked:not(:disabled) ~ & { box-shadow: 0 0 0 4px ${rgba(primary.main, 0.2)}; } label:hover input:not(:disabled) ~ & { background-color: ${nobel['100']}; } label:hover input:not(:disabled):checked ~ & { background-color: ${primary['800']}; } `} `; interface Props { /** Name for the input element */ name?: string; /** Boolean; if present and true, the toggle is currently swtiched on */ checked?: boolean; /** Boolean; the default toggle checked state */ defaultChecked?: boolean; /** Callback function, to be executed when user switch the toggle on/off */ onChange?: (_e: ChangeEvent) => void; /** A Boolean which, if true, disables the toggle */ disabled?: boolean; /** String representing information related to the toggle */ title?: string; /** Rest Props */ [rest: string]: any; } /** * Toggle component is a checkbox with switch-like shape and animation. * Commonly used for an option that can be turned on or off. */ const Toggle = ({ name, checked, defaultChecked = false, onChange = noop, disabled = false, title, ...rest }: Props) => { const [isChecked, setIsChecked] = useState(checked ?? defaultChecked); useEffect(() => { if (typeof checked === 'boolean' && checked !== isChecked) { setIsChecked(checked); } }, [checked]); const handleChange = e => { if (typeof checked === 'undefined') { setIsChecked(prev => !prev); } onChange(e); }; return ( ); }; export default Toggle;