index.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React from 'react'
  2. import { type VariantProps, cva } from 'class-variance-authority'
  3. import classNames from 'classnames'
  4. import Spinner from '../spinner'
  5. const buttonVariants = cva(
  6. 'btn disabled:btn-disabled',
  7. {
  8. variants: {
  9. variant: {
  10. 'primary': 'btn-primary',
  11. 'warning': 'btn-warning',
  12. 'secondary': 'btn-secondary',
  13. 'secondary-accent': 'btn-secondary-accent',
  14. 'ghost': 'btn-ghost',
  15. },
  16. size: {
  17. small: 'btn-small',
  18. medium: 'btn-medium',
  19. large: 'btn-large',
  20. },
  21. },
  22. defaultVariants: {
  23. variant: 'secondary',
  24. size: 'medium',
  25. },
  26. },
  27. )
  28. export type ButtonProps = {
  29. loading?: boolean
  30. } & React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof buttonVariants>
  31. const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  32. ({ className, variant, size, loading, children, ...props }, ref) => {
  33. return (
  34. <button
  35. type='button'
  36. className={classNames(buttonVariants({ variant, size, className }))}
  37. ref={ref}
  38. {...props}
  39. >
  40. {children}
  41. <Spinner loading={loading} className='!text-white !h-3 !w-3 !border-2 !ml-1' />
  42. </button>
  43. )
  44. },
  45. )
  46. Button.displayName = 'Button'
  47. export default Button
  48. export { Button, buttonVariants }