button.tsx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import * as React from 'react';
  2. import { cva, type VariantProps } from 'class-variance-authority';
  3. import { Slot } from 'radix-ui';
  4. import { cn } from '@/lib/utils';
  5. const buttonVariants = cva(
  6. "focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 rounded-md border border-transparent bg-clip-padding text-sm font-medium focus-visible:ring-[3px] aria-invalid:ring-[3px] [&_svg:not([class*='size-'])]:size-4 inline-flex items-center justify-center whitespace-nowrap transition-all disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none shrink-0 [&_svg]:shrink-0 outline-none group/button select-none",
  7. {
  8. variants: {
  9. variant: {
  10. default: 'bg-primary text-primary-foreground hover:bg-primary/80',
  11. outline:
  12. 'border-border bg-background hover:bg-muted hover:text-foreground dark:bg-input/30 dark:border-input dark:hover:bg-input/50 aria-expanded:bg-muted aria-expanded:text-foreground shadow-xs',
  13. secondary:
  14. 'bg-secondary text-secondary-foreground hover:bg-secondary/80 aria-expanded:bg-secondary aria-expanded:text-secondary-foreground',
  15. ghost:
  16. 'hover:bg-muted hover:text-foreground dark:hover:bg-muted/50 aria-expanded:bg-muted aria-expanded:text-foreground',
  17. destructive:
  18. 'bg-destructive/10 hover:bg-destructive/20 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/20 text-destructive focus-visible:border-destructive/40 dark:hover:bg-destructive/30',
  19. link: 'text-primary underline-offset-4 hover:underline',
  20. },
  21. size: {
  22. default:
  23. 'h-9 gap-1.5 px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2',
  24. xs: "h-6 gap-1 rounded-[min(var(--radius-md),8px)] px-2 text-xs in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
  25. sm: 'h-8 gap-1 rounded-[min(var(--radius-md),10px)] px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5',
  26. lg: 'h-10 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-3 has-data-[icon=inline-start]:pl-3',
  27. icon: 'size-9',
  28. 'icon-xs':
  29. "size-6 rounded-[min(var(--radius-md),8px)] in-data-[slot=button-group]:rounded-md [&_svg:not([class*='size-'])]:size-3",
  30. 'icon-sm':
  31. 'size-8 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-md',
  32. 'icon-lg': 'size-10',
  33. },
  34. },
  35. defaultVariants: {
  36. variant: 'default',
  37. size: 'default',
  38. },
  39. },
  40. );
  41. function Button({
  42. className,
  43. variant = 'default',
  44. size = 'default',
  45. asChild = false,
  46. ...props
  47. }: React.ComponentProps<'button'> &
  48. VariantProps<typeof buttonVariants> & {
  49. asChild?: boolean;
  50. }) {
  51. const Comp = asChild ? Slot.Root : 'button';
  52. return (
  53. <Comp
  54. data-slot="button"
  55. data-variant={variant}
  56. data-size={size}
  57. className={cn(buttonVariants({ variant, size, className }))}
  58. {...props}
  59. />
  60. );
  61. }
  62. export { Button, buttonVariants };