index.tsx 1.5 KB

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