switch.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use client";
  2. import * as React from "react";
  3. import { Switch as SwitchPrimitive } from "radix-ui";
  4. import { cn } from "@/lib/utils";
  5. function Switch({
  6. className,
  7. size = "default",
  8. ...props
  9. }: React.ComponentProps<typeof SwitchPrimitive.Root> & {
  10. size?: "sm" | "default";
  11. }) {
  12. return (
  13. <SwitchPrimitive.Root
  14. data-slot="switch"
  15. data-size={size}
  16. className={cn(
  17. "peer data-[state=checked]:bg-primary data-[state=unchecked]:bg-input focus-visible:border-ring focus-visible:ring-ring/50 dark:data-[state=unchecked]:bg-input/80 group/switch inline-flex shrink-0 items-center rounded-full border border-transparent shadow-xs transition-all outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 data-[size=default]:h-[1.15rem] data-[size=default]:w-8 data-[size=sm]:h-3.5 data-[size=sm]:w-6",
  18. className,
  19. )}
  20. {...props}
  21. >
  22. <SwitchPrimitive.Thumb
  23. data-slot="switch-thumb"
  24. className={cn(
  25. "bg-background dark:data-[state=unchecked]:bg-foreground dark:data-[state=checked]:bg-primary-foreground pointer-events-none block rounded-full ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:translate-x-0",
  26. )}
  27. />
  28. </SwitchPrimitive.Root>
  29. );
  30. }
  31. export { Switch };