Design system.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

266 Zeilen
7.4 KiB

  1. import * as React from 'react';
  2. import { TextControl, tailwind } from '@tesseract-design/web-base';
  3. import { useFallbackId } from '@modal-sh/react-utils';
  4. import { PluginCreator } from 'tailwindcss/types/config';
  5. const { tw } = tailwind;
  6. const MenuSelectDerivedElementComponent = 'select' as const;
  7. /**
  8. * Derived HTML element of the {@link MenuSelect} component.
  9. */
  10. export type MenuSelectDerivedElement = HTMLElementTagNameMap[
  11. typeof MenuSelectDerivedElementComponent
  12. ];
  13. /**
  14. * Props of the {@link MenuSelect} component.
  15. */
  16. export interface MenuSelectProps extends Omit<React.HTMLProps<MenuSelectDerivedElement>, 'size' | 'style' | 'label' | 'multiple'> {
  17. /**
  18. * Short textual description indicating the nature of the component's value.
  19. */
  20. label?: React.ReactNode,
  21. /**
  22. * Short textual description as guidelines for valid input values.
  23. */
  24. hint?: React.ReactNode,
  25. /**
  26. * Size of the component.
  27. */
  28. size?: TextControl.Size,
  29. /**
  30. * Additional description, usually graphical, indicating the nature of the component's value.
  31. */
  32. indicator?: React.ReactNode,
  33. /**
  34. * Should the component display a border?
  35. */
  36. border?: boolean,
  37. /**
  38. * Should the component occupy the whole width of its parent?
  39. */
  40. block?: boolean,
  41. /**
  42. * Style of the component.
  43. */
  44. variant?: TextControl.Variant,
  45. /**
  46. * Is the label hidden?
  47. */
  48. hiddenLabel?: boolean,
  49. /**
  50. * Starting height of the component.
  51. */
  52. startingHeight?: number | string,
  53. }
  54. export const menuSelectPlugin: PluginCreator = ({ addComponents }) => {
  55. addComponents({
  56. '.menu-select': {
  57. '& optgroup': {
  58. 'color': 'rgb(var(--color-positive) / 50%)',
  59. 'text-transform': 'uppercase',
  60. 'font-size': '0.75em',
  61. 'margin-top': '0.5rem',
  62. 'user-select': 'none',
  63. },
  64. '& optgroup > option': {
  65. 'color': 'rgb(var(--color-positive))',
  66. 'text-transform': 'none',
  67. 'font-size': '1.333333em',
  68. },
  69. '& option': {
  70. 'user-select': 'none',
  71. },
  72. },
  73. });
  74. };
  75. /**
  76. * Component for selecting a single value from a menu.
  77. */
  78. export const MenuSelect = React.forwardRef<MenuSelectDerivedElement, MenuSelectProps>((
  79. {
  80. label,
  81. hint,
  82. indicator,
  83. size = 'medium' as const,
  84. border = false,
  85. block = false,
  86. variant = 'default' as const,
  87. hiddenLabel = false,
  88. className,
  89. startingHeight = '15rem',
  90. id: idProp,
  91. ...etcProps
  92. }: MenuSelectProps,
  93. forwardedRef,
  94. ) => {
  95. const labelId = React.useId();
  96. const id = useFallbackId(idProp);
  97. return (
  98. <div
  99. className={tw(
  100. 'relative rounded ring-secondary/50 group has-[:disabled]:opacity-50',
  101. 'focus-within:ring-4',
  102. {
  103. 'block': block,
  104. 'inline-block align-middle': !block,
  105. },
  106. className,
  107. )}
  108. data-testid="base"
  109. >
  110. {label && (
  111. <>
  112. <label
  113. data-testid="label"
  114. htmlFor={id}
  115. id={labelId}
  116. className={tw(
  117. 'absolute z-[1] w-full top-0.5 left-0 pointer-events-none pl-1 text-xxs font-bold group-focus-within:text-secondary text-primary leading-none bg-negative select-none',
  118. {
  119. 'sr-only': hiddenLabel,
  120. },
  121. {
  122. 'pr-1': !indicator,
  123. },
  124. {
  125. 'pr-10': indicator && size === 'small',
  126. 'pr-12': indicator && size === 'medium',
  127. 'pr-16': indicator && size === 'large',
  128. },
  129. )}
  130. >
  131. <span className="block w-full whitespace-nowrap h-[1.1em] overflow-hidden text-ellipsis">
  132. {label}
  133. </span>
  134. </label>
  135. {' '}
  136. </>
  137. )}
  138. <MenuSelectDerivedElementComponent
  139. {...etcProps}
  140. ref={forwardedRef}
  141. id={id}
  142. aria-labelledby={labelId}
  143. data-testid="input"
  144. size={2}
  145. style={{
  146. height: startingHeight,
  147. }}
  148. className={tw(
  149. 'menu-select bg-negative rounded-inherit w-full peer block overflow-auto cursor-pointer font-inherit',
  150. 'focus:outline-0',
  151. 'disabled:opacity-50 disabled:cursor-not-allowed',
  152. {
  153. 'resize': !block,
  154. 'resize-y': block,
  155. },
  156. {
  157. 'text-xxs': size === 'small',
  158. 'text-xs': size === 'medium',
  159. },
  160. {
  161. 'pl-4': variant === 'default',
  162. 'pl-1.5': variant === 'alternate',
  163. },
  164. {
  165. 'pt-4': variant === 'alternate' && size === 'small',
  166. 'pt-5': variant === 'alternate' && size === 'medium',
  167. 'pt-8': variant === 'alternate' && size === 'large',
  168. },
  169. {
  170. 'py-2.5': variant === 'default' && size === 'small',
  171. 'py-3': variant === 'default' && size === 'medium',
  172. 'py-5': variant === 'default' && size === 'large',
  173. },
  174. {
  175. 'pr-4': variant === 'default' && !indicator,
  176. 'pr-1.5': variant === 'alternate' && !indicator,
  177. },
  178. {
  179. 'pr-10': indicator && size === 'small',
  180. 'pr-12': indicator && size === 'medium',
  181. 'pr-16': indicator && size === 'large',
  182. },
  183. {
  184. 'min-h-10': size === 'small',
  185. 'min-h-12': size === 'medium',
  186. 'min-h-16': size === 'large',
  187. },
  188. )}
  189. />
  190. {hint && (
  191. <div
  192. data-testid="hint"
  193. className={tw(
  194. 'absolute left-0 px-1 pointer-events-none text-xxs leading-none w-full bg-negative select-none',
  195. {
  196. 'bottom-0 pl-4 pb-1': variant === 'default',
  197. 'top-0.5': variant === 'alternate',
  198. },
  199. {
  200. 'pt-2': variant === 'alternate' && size === 'small',
  201. 'pt-3': variant === 'alternate' && size !== 'small',
  202. },
  203. {
  204. 'pr-4': !indicator && variant === 'default',
  205. 'pr-1': !indicator && variant === 'alternate',
  206. },
  207. {
  208. 'pr-10': indicator && size === 'small',
  209. 'pr-12': indicator && size === 'medium',
  210. 'pr-16': indicator && size === 'large',
  211. },
  212. )}
  213. >
  214. <div
  215. className="opacity-50 whitespace-nowrap w-full h-[1.1em] overflow-hidden text-ellipsis"
  216. >
  217. {hint}
  218. </div>
  219. </div>
  220. )}
  221. {indicator && (
  222. <div
  223. data-testid="indicator"
  224. className={tw(
  225. 'text-center flex items-center justify-center aspect-square absolute bottom-0 right-0 pointer-events-none select-none',
  226. {
  227. 'w-10': size === 'small',
  228. 'w-12': size === 'medium',
  229. 'w-16': size === 'large',
  230. },
  231. )}
  232. >
  233. {indicator}
  234. </div>
  235. )}
  236. {border && (
  237. <span
  238. data-testid="border"
  239. className="absolute z-[1] inset-0 rounded-inherit border-2 border-primary pointer-events-none peer-focus:border-secondary"
  240. />
  241. )}
  242. </div>
  243. );
  244. });
  245. MenuSelect.displayName = 'MenuSelect' as const;
  246. MenuSelect.defaultProps = {
  247. label: undefined,
  248. hint: undefined,
  249. indicator: undefined,
  250. size: 'medium' as const,
  251. border: false as const,
  252. block: false as const,
  253. variant: 'default' as const,
  254. hiddenLabel: false as const,
  255. startingHeight: '15rem' as const,
  256. };