button.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import * as React from "react"
  2. import { Slot } from "@radix-ui/react-slot"
  3. import { cva, type VariantProps } from "class-variance-authority"
  4. import { cn } from "@/lib/utils"
  5. const buttonVariants = cva(
  6. "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
  7. {
  8. variants: {
  9. variant: {
  10. default: "bg-primary text-primary-foreground hover:bg-primary/90",
  11. destructive:
  12. "bg-destructive text-destructive-foreground hover:bg-destructive/90",
  13. outline:
  14. "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
  15. secondary:
  16. "bg-secondary text-secondary-foreground hover:bg-secondary/80",
  17. ghost: "hover:bg-accent hover:text-accent-foreground",
  18. link: "text-primary underline-offset-4 hover:underline",
  19. },
  20. size: {
  21. default: "h-10 px-4 py-2",
  22. sm: "h-9 rounded-md px-3",
  23. lg: "h-11 rounded-md px-8",
  24. icon: "h-10 w-10",
  25. },
  26. },
  27. defaultVariants: {
  28. variant: "default",
  29. size: "default",
  30. },
  31. }
  32. )
  33. export interface ButtonProps
  34. extends React.ButtonHTMLAttributes<HTMLButtonElement>,
  35. VariantProps<typeof buttonVariants> {
  36. asChild?: boolean
  37. }
  38. const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  39. ({ className, variant, size, asChild = false, ...props }, ref) => {
  40. const Comp = asChild ? Slot : "button"
  41. return (
  42. <Comp
  43. className={cn(buttonVariants({ variant, size, className }))}
  44. ref={ref}
  45. {...props}
  46. />
  47. )
  48. }
  49. )
  50. Button.displayName = "Button"
  51. export { Button, buttonVariants }