cal.pub0.org/components/ui/Switch.tsx

47 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2021-10-12 09:35:44 +00:00
import { useId } from "@radix-ui/react-id";
import * as Label from "@radix-ui/react-label";
import * as PrimitiveSwitch from "@radix-ui/react-switch";
2021-10-18 07:02:25 +00:00
import React, { useState } from "react";
2021-10-18 07:02:25 +00:00
import classNames from "@lib/classNames";
2021-10-18 07:02:25 +00:00
type SwitchProps = React.ComponentProps<typeof PrimitiveSwitch.Root> & {
label: string;
};
export default function Switch(props: SwitchProps) {
const { label, onCheckedChange, ...primitiveProps } = props;
const [checked, setChecked] = useState(props.defaultChecked || false);
const onPrimitiveCheckedChange = (change: boolean) => {
if (onCheckedChange) {
onCheckedChange(change);
}
setChecked(change);
};
2021-10-12 09:35:44 +00:00
const id = useId();
return (
<div className="flex items-center h-[20px]">
<PrimitiveSwitch.Root
className={classNames(checked ? "bg-gray-900" : "bg-gray-400", "rounded-sm w-[36px] p-0.5 h-[20px]")}
checked={checked}
onCheckedChange={onPrimitiveCheckedChange}
{...primitiveProps}>
<PrimitiveSwitch.Thumb
2021-10-12 09:35:44 +00:00
id={id}
className={classNames(
"bg-white w-[16px] h-[16px] block transition-transform",
checked ? "translate-x-[16px]" : "translate-x-0"
)}
/>
</PrimitiveSwitch.Root>
{label && (
2021-10-12 09:35:44 +00:00
<Label.Root
htmlFor={id}
className="text-neutral-700 text-sm align-text-top ml-3 font-medium cursor-pointer">
{label}
</Label.Root>
)}
</div>
);
}