Website for showcasing all features of Tesseract Web.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

167 rivejä
3.7 KiB

  1. import * as React from 'react';
  2. import * as ButtonBase from '@tesseract-design/web-base-button';
  3. export enum ActionButtonType {
  4. SUBMIT = 'submit',
  5. RESET = 'reset',
  6. BUTTON = 'button',
  7. }
  8. export type ActionButtonProps = Omit<React.HTMLProps<HTMLButtonElement>, 'size' | 'type' | 'style'> & {
  9. /**
  10. * Size of the component.
  11. */
  12. size?: ButtonBase.ButtonSize,
  13. /**
  14. * Variant of the component.
  15. */
  16. variant?: ButtonBase.ButtonVariant,
  17. /**
  18. * Should the component display a border?
  19. */
  20. border?: boolean,
  21. /**
  22. * Should the component occupy the whole width of its parent?
  23. */
  24. block?: boolean,
  25. /**
  26. * Type of the component.
  27. */
  28. type?: ActionButtonType,
  29. /**
  30. * Style of the component.
  31. */
  32. style?: ButtonBase.ButtonStyle,
  33. /**
  34. * Does the component need to conserve space?
  35. */
  36. compact?: boolean,
  37. /**
  38. * Complementary content of the component.
  39. */
  40. subtext?: React.ReactNode,
  41. /**
  42. * Short complementary content displayed at the edge of the component.
  43. */
  44. badge?: React.ReactNode,
  45. /**
  46. * Is this component part of a menu?
  47. */
  48. menuItem?: boolean,
  49. }
  50. /**
  51. * Component for performing an action upon activation (e.g. when clicked).
  52. *
  53. * This component functions as a regular button.
  54. */
  55. export const ActionButton = React.forwardRef<HTMLButtonElement, ActionButtonProps>(
  56. (
  57. {
  58. size = ButtonBase.ButtonSize.MEDIUM,
  59. variant = ButtonBase.ButtonVariant.OUTLINE,
  60. border = false,
  61. children,
  62. type = ActionButtonType.BUTTON,
  63. block = false,
  64. style = ButtonBase.ButtonStyle.DEFAULT,
  65. disabled = false,
  66. compact = false,
  67. subtext,
  68. badge,
  69. menuItem = false,
  70. className: _className,
  71. as: _as,
  72. ...etcProps
  73. }: ActionButtonProps,
  74. ref,
  75. ) => {
  76. const styleProps = React.useMemo<ButtonBase.ButtonBaseArgs>(() => ({
  77. size,
  78. block,
  79. variant,
  80. border,
  81. style,
  82. compact,
  83. menuItem,
  84. disabled,
  85. }), [size, block, variant, border, style, compact, menuItem]);
  86. return (
  87. <button
  88. {...etcProps}
  89. disabled={disabled}
  90. className={ButtonBase.Button(styleProps)}
  91. ref={ref}
  92. type={type ?? ActionButtonType.BUTTON}
  93. >
  94. <span
  95. className={ButtonBase.Border(styleProps)}
  96. />
  97. <span
  98. className={ButtonBase.Label(styleProps)}
  99. >
  100. <span
  101. className={ButtonBase.MainText()}
  102. >
  103. <span
  104. className={ButtonBase.OverflowText()}
  105. >
  106. {children}
  107. </span>
  108. </span>
  109. {
  110. subtext
  111. && (
  112. <>
  113. {' '}
  114. <span className={ButtonBase.Subtext()}>
  115. <span
  116. className={ButtonBase.OverflowText()}
  117. >
  118. {subtext}
  119. </span>
  120. </span>
  121. </>
  122. )
  123. }
  124. </span>
  125. {
  126. badge
  127. && (
  128. <>
  129. {' '}
  130. <span
  131. className={ButtonBase.BadgeContainer(styleProps)}
  132. >
  133. {badge}
  134. </span>
  135. </>
  136. )
  137. }
  138. {
  139. menuItem
  140. && (
  141. <>
  142. {' '}
  143. <span
  144. className={ButtonBase.IndicatorWrapper(styleProps)}
  145. >
  146. <svg
  147. className={ButtonBase.Indicator()}
  148. viewBox="0 0 24 24"
  149. role="presentation"
  150. >
  151. <polyline points="9 18 15 12 9 6"/>
  152. </svg>
  153. </span>
  154. </>
  155. )
  156. }
  157. </button>
  158. );
  159. },
  160. );
  161. ActionButton.displayName = 'ActionButton';