button.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import * as React from "react"
  2. import { cva, type VariantProps } from "class-variance-authority"
  3. import { cn } from "@/lib/utils/client"
  4. const buttonVariants = cva(
  5. "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",
  6. {
  7. variants: {
  8. variant: {
  9. default:
  10. "bg-primary text-primary-foreground shadow hover:bg-primary/90",
  11. destructive:
  12. "bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
  13. outline:
  14. "border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
  15. secondary:
  16. "bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
  17. ghost: "hover:bg-accent hover:text-accent-foreground",
  18. link: "text-primary underline-offset-4 hover:underline",
  19. },
  20. size: {
  21. default: "h-9 px-4 py-2",
  22. sm: "h-8 rounded-md px-3 text-xs",
  23. lg: "h-10 rounded-md px-8",
  24. icon: "h-9 w-9",
  25. },
  26. },
  27. defaultVariants: {
  28. variant: "default",
  29. size: "default",
  30. },
  31. }
  32. )
  33. export interface ButtonProps
  34. extends React.ButtonHTMLAttributes<HTMLButtonElement>,
  35. VariantProps<typeof buttonVariants> {
  36. asChild?: boolean
  37. }
  38. const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  39. ({ className, variant, size, asChild = false, ...props }, ref) => {
  40. if (asChild) {
  41. const child = React.Children.only(props.children) as React.ReactElement<Record<string, unknown>>
  42. return React.cloneElement(child, {
  43. className: cn(buttonVariants({ variant, size, className }), child.props.className as string),
  44. ref,
  45. ...props,
  46. children: child.props.children,
  47. })
  48. }
  49. return (
  50. <button
  51. className={cn(buttonVariants({ variant, size, className }))}
  52. ref={ref}
  53. {...props}
  54. />
  55. )
  56. }
  57. )
  58. Button.displayName = "Button"
  59. export { Button, buttonVariants }