import type { ChangeEvent, ReactNode } from 'react'; import { useMemo } from 'react'; import { nanoid } from 'nanoid'; import styled from 'styled-components'; import tw from 'twin.macro'; import Label from '@/components/elements/Label'; import Input from '@/components/elements/Input'; const ToggleContainer = styled.div` ${tw`relative select-none w-12 leading-normal`}; & > input[type='checkbox'] { ${tw`hidden`}; &:checked + label { ${tw`bg-primary-500 border-primary-700 shadow-none`}; } &:checked + label:before { right: 0.125rem; } } & > label { ${tw`mb-0 block overflow-hidden cursor-pointer bg-neutral-400 border border-neutral-700 rounded-full h-6 shadow-inner`}; transition: all 75ms linear; &::before { ${tw`absolute block bg-white border h-5 w-5 rounded-full`}; top: 0.125rem; right: calc(50% + 0.125rem); //width: 1.25rem; //height: 1.25rem; content: ''; transition: all 75ms ease-in; } } `; export interface SwitchProps { name: string; label?: string; description?: string; defaultChecked?: boolean; readOnly?: boolean; onChange?: (e: ChangeEvent) => void; children?: ReactNode; } const Switch = ({ name, label, description, defaultChecked, readOnly, onChange, children }: SwitchProps) => { const uuid = useMemo(() => nanoid(), []); return (
{children || ( onChange && onChange(e)} defaultChecked={defaultChecked} disabled={readOnly} /> )} {(label || description) && (
{label && ( )} {description &&

{description}

}
)}
); }; export default Switch;