button.tsx 2.4 KB

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