import { useState, useCallback, useEffect, ReactNode, ChangeEvent, } from 'react'; import { css, useTheme } from '@emotion/react'; import styled from '@emotion/styled'; import { nobel, white } from 'styles/colors'; import noop from 'utils/noop'; import { rgba } from 'polished'; const Input = styled.input` width: 0; height: 0; opacity: 0; `; const Container = styled.label<{ disabled?: boolean }>` position: relative; display: inline-block; width: 74px; height: 32px; `; const easeInOutCubic = 'cubic-bezier(0.65, 0, 0.35, 1)'; const Track = styled.div<{ disabled?: boolean }>` position: absolute; display: flex; justify-content: center; align-items: center; top: 0; right: 0; bottom: 0; left: 0; background-color: ${nobel[200]}; border-radius: 8px; cursor: pointer; transition: all 0.3s ${easeInOutCubic}; input:active:not(:disabled) ~ &, input:focus:not(:disabled) ~ & { box-shadow: 0 0 0 4px ${rgba(nobel['400'], 0.2)}; } ${({ disabled }) => disabled && css` opacity: 0.5; cursor: not-allowed; `} `; const Thumb = styled.div<{ checked?: boolean }>` position: absolute; top: 2px; width: 40px; height: 28px; display: flex; justify-content: center; align-items: center; background-color: ${white}; border-radius: 8px; box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1); ${({ theme }) => `${theme.rtl ? 'right' : 'left'}: 2px`}; transition: all 0.3s ${easeInOutCubic}; ${({ checked, theme }) => checked && `transform: translateX(${theme.rtl ? '-30px' : '30px'})`}; `; const IconContainer = styled.div<{ position: 'left' | 'right'; checked: boolean; }>` position: absolute; top: 0; bottom: 0; left: ${({ position }) => (position === 'right' ? '37px' : '0')}; display: flex; justify-content: center; align-items: center; width: 37px; & * { ${({ checked, position }) => { const styleMap = { checked: { left: 'translateX(1px)', right: 'translateX(-1px)', opacity: 1, scale: 'scale(1)', }, unchecked: { left: 'translateX(-1px)', right: 'translateX(1px)', opacity: 0.725, scale: 'scale(0.874)', }, }; const variant = checked ? 'checked' : 'unchecked'; const { opacity, scale } = styleMap[variant]; return css` opacity: ${opacity}; transform-origin: center; transition: all 0.3s ${easeInOutCubic}; transform: ${scale} ${styleMap[variant][position]}; `; }} } `; 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; /** A ReactNode to be rendered at the checked position */ checkedIcon?: ReactNode; /** A ReactNode to be rendered at the unchecked position */ uncheckedIcon?: ReactNode; /** Props to be passed to label */ labelProps?: Record; /** Rest Props */ [rest: string]: any; } /** * Switch component is a checkbox with switch-like shape and animation and customizable icons. * Commonly used for an option that can be turned on or off. */ const Switch = ({ name = '', checked, defaultChecked = false, onChange = noop, disabled = false, title = '', checkedIcon = null, uncheckedIcon = null, labelProps = {}, ...rest }: Props) => { const [checkState, setCheckState] = useState(checked || defaultChecked); const theme = useTheme(); const handleChange = useCallback( e => { if (checked === undefined) setCheckState(c => !c); onChange(e); }, [checked, onChange] ); useEffect(() => { if (checked !== undefined) setCheckState(checked); }, [checked]); return ( {uncheckedIcon} {checkedIcon} ); }; export default Switch;