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