button.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import * as React from 'react'
  2. import { Slot } from '@radix-ui/react-slot'
  3. import { cva, type VariantProps } from 'class-variance-authority'
  4. import { cn } from '@/lib/utils'
  5. import { IconLoader2 } from '@tabler/icons-react'
  6. const buttonVariants = cva(
  7. 'inline-flex items-center justify-center 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',
  8. {
  9. variants: {
  10. variant: {
  11. default:
  12. 'bg-primary text-primary-foreground shadow hover:bg-primary/90',
  13. destructive:
  14. 'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90',
  15. outline:
  16. 'border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground',
  17. secondary:
  18. 'bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80',
  19. ghost: 'hover:bg-accent hover:text-accent-foreground',
  20. link: 'text-primary underline-offset-4 hover:underline',
  21. },
  22. size: {
  23. default: 'h-9 px-4 py-2',
  24. sm: 'h-8 rounded-md px-3 text-xs',
  25. lg: 'h-10 rounded-md px-8',
  26. icon: 'h-9 w-9',
  27. },
  28. },
  29. defaultVariants: {
  30. variant: 'default',
  31. size: 'default',
  32. },
  33. }
  34. )
  35. interface ButtonPropsBase
  36. extends React.ButtonHTMLAttributes<HTMLButtonElement>,
  37. VariantProps<typeof buttonVariants> {}
  38. type ButtonProps = ButtonPropsBase &
  39. (
  40. | { asChild: true }
  41. | {
  42. asChild?: false
  43. loading?: boolean
  44. leftSection?: JSX.Element
  45. rightSection?: JSX.Element
  46. }
  47. )
  48. const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  49. ({ className, variant, size, children, ...props }, ref) => {
  50. if (props.asChild) {
  51. return (
  52. <Slot
  53. className={cn(buttonVariants({ variant, size, className }))}
  54. ref={ref}
  55. {...props}
  56. >
  57. {children}
  58. </Slot>
  59. )
  60. }
  61. const {
  62. loading = false,
  63. leftSection,
  64. rightSection,
  65. disabled,
  66. ...otherProps
  67. } = props
  68. return (
  69. <button
  70. className={cn(buttonVariants({ variant, size, className }))}
  71. disabled={loading || disabled}
  72. ref={ref}
  73. {...otherProps}
  74. >
  75. {((leftSection && loading) ||
  76. (!leftSection && !rightSection && loading)) && (
  77. <IconLoader2 className='mr-2 h-4 w-4 animate-spin' />
  78. )}
  79. {!loading && leftSection && <div className='mr-2'>{leftSection}</div>}
  80. {children}
  81. {!loading && rightSection && <div className='ml-2'>{rightSection}</div>}
  82. {rightSection && loading && (
  83. <IconLoader2 className='ml-2 h-4 w-4 animate-spin' />
  84. )}
  85. </button>
  86. )
  87. }
  88. )
  89. Button.displayName = 'Button'
  90. // eslint-disable-next-line react-refresh/only-export-components
  91. export { Button, buttonVariants }