button.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Slot } from '@radix-ui/react-slot';
  2. import { type VariantProps, cva } from 'class-variance-authority';
  3. import * as React from 'react';
  4. import { cn } from '@/common/helpers/cn';
  5. const buttonVariants = cva(
  6. "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-[color,box-shadow] disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [&_svg]:shrink-0 outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
  7. {
  8. variants: {
  9. variant: {
  10. default: 'bg-primary text-primary-foreground shadow-xs hover:bg-primary/90',
  11. destructive:
  12. 'bg-destructive text-white shadow-xs hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40',
  13. outline: 'border border-input bg-background shadow-xs hover:bg-accent hover:text-accent-foreground',
  14. secondary: 'bg-secondary text-secondary-foreground shadow-xs 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 has-[>svg]:px-3',
  20. sm: 'h-8 rounded-md px-3 has-[>svg]:px-2.5',
  21. lg: 'h-10 rounded-md px-6 has-[>svg]:px-4',
  22. icon: 'size-9',
  23. },
  24. },
  25. defaultVariants: {
  26. variant: 'default',
  27. size: 'default',
  28. },
  29. },
  30. );
  31. function Button({
  32. className,
  33. variant,
  34. size,
  35. asChild = false,
  36. ...props
  37. }: React.ComponentProps<'button'> &
  38. VariantProps<typeof buttonVariants> & {
  39. asChild?: boolean;
  40. }) {
  41. const Comp = asChild ? Slot : 'button';
  42. return <Comp data-slot="button" className={cn(buttonVariants({ variant, size, className }))} {...props} />;
  43. }
  44. export { Button, buttonVariants };