button.tsx

 1import * as React from 'react'
 2import { Slot } from '@radix-ui/react-slot'
 3import { cva, type VariantProps } from 'class-variance-authority'
 4import { cn } from '@/lib/utils'
 5
 6const 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-1 focus-visible:ring-ring 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-10 rounded-md px-8',
22        icon: 'h-9 w-9',
23      },
24    },
25    defaultVariants: {
26      variant: 'default',
27      size: 'default',
28    },
29  },
30)
31
32export interface ButtonProps
33  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
34    VariantProps<typeof buttonVariants> {
35  asChild?: boolean
36}
37
38const 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)
50Button.displayName = 'Button'
51
52export { Button, buttonVariants }