index.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import type { CSSProperties } from 'react'
  2. import React from 'react'
  3. import { type VariantProps, cva } from 'class-variance-authority'
  4. import classNames from '@/utils/classnames'
  5. enum ActionButtonState {
  6. Destructive = 'destructive',
  7. Active = 'active',
  8. Disabled = 'disabled',
  9. Default = '',
  10. Hover = 'hover',
  11. }
  12. const actionButtonVariants = cva(
  13. 'action-btn',
  14. {
  15. variants: {
  16. size: {
  17. xs: 'action-btn-xs',
  18. m: 'action-btn-m',
  19. l: 'action-btn-l',
  20. xl: 'action-btn-xl',
  21. },
  22. },
  23. defaultVariants: {
  24. size: 'm',
  25. },
  26. },
  27. )
  28. export type ActionButtonProps = {
  29. size?: 'xs' | 's' | 'm' | 'l' | 'xl'
  30. state?: ActionButtonState
  31. styleCss?: CSSProperties
  32. } & React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof actionButtonVariants>
  33. function getActionButtonState(state: ActionButtonState) {
  34. switch (state) {
  35. case ActionButtonState.Destructive:
  36. return 'action-btn-destructive'
  37. case ActionButtonState.Active:
  38. return 'action-btn-active'
  39. case ActionButtonState.Disabled:
  40. return 'action-btn-disabled'
  41. case ActionButtonState.Hover:
  42. return 'action-btn-hover'
  43. default:
  44. return ''
  45. }
  46. }
  47. const ActionButton = React.forwardRef<HTMLButtonElement, ActionButtonProps>(
  48. ({ className, size, state = ActionButtonState.Default, styleCss, children, ...props }, ref) => {
  49. return (
  50. <button
  51. type='button'
  52. className={classNames(
  53. actionButtonVariants({ className, size }),
  54. getActionButtonState(state),
  55. )}
  56. ref={ref}
  57. style={styleCss}
  58. {...props}
  59. >
  60. {children}
  61. </button>
  62. )
  63. },
  64. )
  65. ActionButton.displayName = 'ActionButton'
  66. export default ActionButton
  67. export { ActionButton, ActionButtonState, actionButtonVariants }