button.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Slot } from '@radix-ui/react-slot';
  2. import { cva, type VariantProps } from 'class-variance-authority';
  3. import * as React from 'react';
  4. import { cn } from '@/vdb/lib/utils.js';
  5. const buttonVariants = cva(
  6. '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',
  7. {
  8. variants: {
  9. variant: {
  10. default: 'bg-primary text-primary-foreground shadow hover:bg-primary/90',
  11. destructive: 'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90',
  12. outline:
  13. 'border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground',
  14. secondary: 'bg-secondary text-secondary-foreground shadow-sm 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',
  20. sm: 'h-8 rounded-md px-3 text-xs',
  21. lg: 'h-10 rounded-md px-8',
  22. icon: 'h-9 w-9',
  23. xs: 'h-5 rounded-md px-2 text-xs',
  24. 'icon-sm': 'h-8 w-8 text-xs',
  25. 'icon-xs': 'h-5 w-5 text-xs',
  26. },
  27. },
  28. defaultVariants: {
  29. variant: 'default',
  30. size: 'default',
  31. },
  32. },
  33. );
  34. export interface ButtonProps
  35. extends React.ButtonHTMLAttributes<HTMLButtonElement>,
  36. VariantProps<typeof buttonVariants> {
  37. asChild?: boolean;
  38. }
  39. const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  40. ({ className, variant, size, asChild = false, ...props }, ref) => {
  41. const Comp = asChild ? Slot : 'button';
  42. return (
  43. <Comp
  44. className={cn(buttonVariants({ variant, size, className }))}
  45. type={props.type ?? 'button'}
  46. ref={ref}
  47. {...props}
  48. />
  49. );
  50. },
  51. );
  52. Button.displayName = 'Button';
  53. export { Button, buttonVariants };