2022-07-22 08:40:53 -07:00
|
|
|
import { FunctionComponent, ReactNode, memo } from 'react'
|
2022-07-17 04:48:33 -07:00
|
|
|
|
|
|
|
interface SwitchProps {
|
|
|
|
checked: boolean
|
|
|
|
className?: string
|
|
|
|
onChange: (x: boolean) => void
|
2022-08-15 04:52:17 -07:00
|
|
|
children?: ReactNode
|
2022-07-25 20:59:46 -07:00
|
|
|
disabled?: boolean
|
2022-07-17 04:48:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const Switch: FunctionComponent<SwitchProps> = ({
|
|
|
|
checked = false,
|
|
|
|
className = '',
|
|
|
|
children,
|
|
|
|
onChange,
|
2022-07-25 20:59:46 -07:00
|
|
|
disabled,
|
2022-07-17 04:48:33 -07:00
|
|
|
}) => {
|
|
|
|
const handleClick = () => {
|
|
|
|
onChange(!checked)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={`flex items-center ${className}`}>
|
2023-01-22 03:27:04 -08:00
|
|
|
<span className="mr-2 text-xs text-th-fgd-2">{children}</span>
|
2022-07-17 04:48:33 -07:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={`${
|
2022-11-30 19:32:32 -08:00
|
|
|
checked ? 'bg-th-success' : 'bg-th-bkg-4'
|
2022-08-01 12:23:46 -07:00
|
|
|
} relative inline-flex h-5 w-10 flex-shrink-0 cursor-pointer rounded-full
|
2022-07-17 04:48:33 -07:00
|
|
|
border-2 border-transparent transition-colors duration-200 ease-in-out
|
2022-07-25 20:59:46 -07:00
|
|
|
focus:outline-none ${disabled ? 'opacity-60' : ''}`}
|
2022-07-17 04:48:33 -07:00
|
|
|
role="switch"
|
|
|
|
aria-checked={checked}
|
|
|
|
onClick={handleClick}
|
2022-07-25 20:59:46 -07:00
|
|
|
disabled={disabled}
|
2022-07-17 04:48:33 -07:00
|
|
|
>
|
|
|
|
<span className="sr-only">{children}</span>
|
|
|
|
<span
|
|
|
|
aria-hidden="true"
|
|
|
|
className={`${
|
|
|
|
checked ? 'translate-x-5' : 'translate-x-0'
|
2022-08-16 15:58:03 -07:00
|
|
|
} pointer-events-none inline-block h-4 w-4 rounded-full
|
2022-07-17 04:48:33 -07:00
|
|
|
bg-white shadow ring-0 transition duration-200 ease-in-out`}
|
|
|
|
></span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-22 08:40:53 -07:00
|
|
|
export default memo(Switch)
|