button.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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-all duration-300 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 shadow-sm hover:shadow-md hover:scale-105 active:scale-95",
  7. {
  8. variants: {
  9. variant: {
  10. default: "bg-primary text-primary-foreground hover:bg-primary/90 shadow-md hover:shadow-lg",
  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 };