toggle.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use client"
  2. import * as React from "react"
  3. import * as TogglePrimitive from "@radix-ui/react-toggle"
  4. import { cva, type VariantProps } from "class-variance-authority"
  5. import { cn } from "@/lib/utils"
  6. const toggleVariants = cva(
  7. "inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors hover:bg-muted hover:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0 gap-2",
  8. {
  9. variants: {
  10. variant: {
  11. default: "bg-transparent",
  12. outline:
  13. "border border-input bg-transparent hover:bg-accent hover:text-accent-foreground",
  14. },
  15. size: {
  16. default: "h-10 px-3 min-w-10",
  17. sm: "h-9 px-2.5 min-w-9",
  18. lg: "h-11 px-5 min-w-11",
  19. },
  20. },
  21. defaultVariants: {
  22. variant: "default",
  23. size: "default",
  24. },
  25. }
  26. )
  27. const Toggle = React.forwardRef<
  28. React.ElementRef<typeof TogglePrimitive.Root>,
  29. React.ComponentPropsWithoutRef<typeof TogglePrimitive.Root> &
  30. VariantProps<typeof toggleVariants>
  31. >(({ className, variant, size, ...props }, ref) => (
  32. <TogglePrimitive.Root
  33. ref={ref}
  34. className={cn(toggleVariants({ variant, size, className }))}
  35. {...props}
  36. />
  37. ))
  38. Toggle.displayName = TogglePrimitive.Root.displayName
  39. export { Toggle, toggleVariants }