Website for showcasing all features of Tesseract Web.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

182 linhas
4.8 KiB

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