index.tsx 1.4 KB

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