button.tsx 3.3 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. "group/button inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  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 aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
  13. secondary:
  14. "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
  15. ghost:
  16. "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
  17. destructive:
  18. "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
  19. link: "text-primary underline-offset-4 hover:underline",
  20. },
  21. size: {
  22. default:
  23. "h-8 gap-1.5 px-2.5 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),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg 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-7 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
  26. lg: "h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
  27. icon: "size-8",
  28. "icon-xs":
  29. "size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
  30. "icon-sm":
  31. "size-7 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg",
  32. "icon-lg": "size-9",
  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 }