Design system.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

184 líneas
4.2 KiB

  1. import * as React from 'react';
  2. import * as ButtonBase from '@tesseract-design/web-base-button';
  3. type LinkButtonElement = HTMLAnchorElement | HTMLSpanElement;
  4. export type LinkButtonProps = Omit<React.HTMLProps<LinkButtonElement>, 'size' | '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. * Is this component part of a menu?
  35. */
  36. menuItem?: boolean,
  37. };
  38. /**
  39. * Component for performing an action upon activation (e.g. when clicked).
  40. *
  41. * This component functions as a hyperlink.
  42. */
  43. export const LinkButton = React.forwardRef<LinkButtonElement, LinkButtonProps>(
  44. (
  45. {
  46. size = ButtonBase.ButtonSize.MEDIUM,
  47. variant = ButtonBase.ButtonVariant.OUTLINE,
  48. border = false,
  49. children,
  50. block = false,
  51. disabled = false,
  52. onClick,
  53. href,
  54. target,
  55. rel,
  56. compact = false,
  57. subtext,
  58. badge,
  59. menuItem = false,
  60. className: _className,
  61. as: _as,
  62. ...etcProps
  63. }: LinkButtonProps,
  64. ref,
  65. ) => {
  66. const styleProps = React.useMemo<ButtonBase.ButtonBaseArgs>(() => ({
  67. size,
  68. block,
  69. variant,
  70. border,
  71. disabled,
  72. compact,
  73. menuItem,
  74. }), [size, block, variant, border, disabled, compact, menuItem]);
  75. const commonChildren = (
  76. <>
  77. <span
  78. className={ButtonBase.Border(styleProps)}
  79. />
  80. <span
  81. className={ButtonBase.Label(styleProps)}
  82. >
  83. <span
  84. className={ButtonBase.MainText()}
  85. data-testid="children"
  86. >
  87. <span
  88. className={ButtonBase.OverflowText()}
  89. >
  90. {children}
  91. </span>
  92. </span>
  93. {
  94. subtext
  95. && (
  96. <>
  97. {' '}
  98. <span
  99. className={ButtonBase.Subtext()}
  100. data-testid="subtext"
  101. >
  102. <span
  103. className={ButtonBase.OverflowText()}
  104. >
  105. {subtext}
  106. </span>
  107. </span>
  108. </>
  109. )
  110. }
  111. </span>
  112. {
  113. badge
  114. && (
  115. <>
  116. {' '}
  117. <span
  118. className={ButtonBase.BadgeContainer(styleProps)}
  119. data-testid="badge"
  120. >
  121. {badge}
  122. </span>
  123. </>
  124. )
  125. }
  126. {
  127. menuItem
  128. && (
  129. <>
  130. {' '}
  131. <span
  132. className={ButtonBase.IndicatorWrapper(styleProps)}
  133. data-testid="menuItemIndicator"
  134. >
  135. <svg
  136. className={ButtonBase.Indicator()}
  137. viewBox="0 0 24 24"
  138. role="presentation"
  139. >
  140. <polyline points="9 18 15 12 9 6"/>
  141. </svg>
  142. </span>
  143. </>
  144. )
  145. }
  146. </>
  147. );
  148. if (disabled) {
  149. return (
  150. // eslint-disable-next-line jsx-a11y/click-events-have-key-events
  151. <span
  152. {...etcProps}
  153. className={ButtonBase.Button(styleProps)}
  154. ref={ref as React.ForwardedRef<HTMLSpanElement>}
  155. onClick={undefined}
  156. >
  157. {commonChildren}
  158. </span>
  159. );
  160. }
  161. return (
  162. <a
  163. {...etcProps}
  164. className={ButtonBase.Button(styleProps)}
  165. ref={ref as React.ForwardedRef<HTMLAnchorElement>}
  166. href={href}
  167. target={target}
  168. rel={rel}
  169. onClick={onClick}
  170. >
  171. {commonChildren}
  172. </a>
  173. );
  174. },
  175. );
  176. LinkButton.displayName = 'LinkButton';