button.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import * as React from "react";
  2. import { cva, type VariantProps } from "class-variance-authority";
  3. import { Slot } from "radix-ui";
  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. interface ButtonProps
  34. extends
  35. React.ButtonHTMLAttributes<HTMLButtonElement>,
  36. VariantProps<typeof buttonVariants> {
  37. asChild?: boolean;
  38. }
  39. const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  40. ({ className, variant, size, asChild = false, ...props }, ref) => {
  41. const Comp = asChild ? Slot.Root : "button";
  42. return (
  43. <Comp
  44. className={cn(buttonVariants({ variant, size, className }))}
  45. ref={ref}
  46. {...props}
  47. />
  48. );
  49. },
  50. );
  51. Button.displayName = "Button";
  52. export { Button, buttonVariants };
  53. export type { ButtonProps };