index.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  11. const actionButtonVariants = cva(
  12. 'action-btn',
  13. {
  14. variants: {
  15. size: {
  16. xs: 'action-btn-xs',
  17. m: 'action-btn-m',
  18. l: 'action-btn-l',
  19. xl: 'action-btn-xl',
  20. },
  21. },
  22. defaultVariants: {
  23. size: 'm',
  24. },
  25. },
  26. )
  27. export type ActionButtonProps = {
  28. size?: 'xs' | 'm' | 'l' | 'xl'
  29. state?: ActionButtonState
  30. styleCss?: CSSProperties
  31. } & React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof actionButtonVariants>
  32. function getActionButtonState(state: ActionButtonState) {
  33. switch (state) {
  34. case ActionButtonState.Destructive:
  35. return 'action-btn-destructive'
  36. case ActionButtonState.Active:
  37. return 'action-btn-active'
  38. case ActionButtonState.Disabled:
  39. return 'action-btn-disabled'
  40. default:
  41. return ''
  42. }
  43. }
  44. const ActionButton = React.forwardRef<HTMLButtonElement, ActionButtonProps>(
  45. ({ className, size, state = ActionButtonState.Default, styleCss, children, ...props }, ref) => {
  46. return (
  47. <button
  48. type='button'
  49. className={classNames(
  50. actionButtonVariants({ className, size }),
  51. getActionButtonState(state),
  52. )}
  53. ref={ref}
  54. style={styleCss}
  55. {...props}
  56. >
  57. {children}
  58. </button>
  59. )
  60. },
  61. )
  62. ActionButton.displayName = 'ActionButton'
  63. export default ActionButton
  64. export { ActionButton, ActionButtonState, actionButtonVariants }