Design system.
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.
 
 
 

261 regels
7.2 KiB

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