Common front-end components for Web using the Tesseract design system, written for React. https://make.modal.sh/tesseract/web/react/common
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.
 
 
 
 

313 lines
7.1 KiB

  1. import * as React from 'react'
  2. import * as PropTypes from 'prop-types'
  3. import styled from 'styled-components'
  4. import stringify from '../../services/stringify'
  5. import { Size, SizeMap } from '../../services/utilities'
  6. import Icon from '../Icon/Icon'
  7. const MIN_HEIGHTS: SizeMap<string | number> = {
  8. small: '2.5rem',
  9. medium: '3rem',
  10. large: '4rem',
  11. }
  12. const LABEL_VERTICAL_PADDING_SIZES: SizeMap<string | number> = {
  13. small: '0.125rem',
  14. medium: '0.25rem',
  15. large: '0.5rem',
  16. }
  17. const VERTICAL_PADDING_SIZES: SizeMap<string | number> = {
  18. small: '0.6rem',
  19. medium: '0.85rem',
  20. large: '1.25rem',
  21. }
  22. const INPUT_FONT_SIZES: SizeMap<string | number> = {
  23. small: '0.85em',
  24. medium: '0.85em',
  25. large: '1em',
  26. }
  27. const SECONDARY_TEXT_SIZES: SizeMap<string | number> = {
  28. small: '0.65em',
  29. medium: '0.75em',
  30. large: '0.85em',
  31. }
  32. const ComponentBase = styled('div')({
  33. position: 'relative',
  34. borderRadius: '0.25rem',
  35. fontFamily: 'var(--font-family-base, sans-serif)',
  36. maxWidth: '100%',
  37. ':focus-within': {
  38. '--color-accent': 'var(--color-active, Highlight)',
  39. },
  40. })
  41. ComponentBase.displayName = 'div'
  42. const CaptureArea = styled('label')({
  43. display: 'block',
  44. })
  45. CaptureArea.displayName = 'label'
  46. const LabelWrapper = styled('span')({
  47. color: 'var(--color-accent, blue)',
  48. boxSizing: 'border-box',
  49. position: 'absolute',
  50. top: 0,
  51. left: 0,
  52. paddingLeft: '0.5rem',
  53. fontSize: '0.85em',
  54. maxWidth: '100%',
  55. overflow: 'hidden',
  56. textOverflow: 'ellipsis',
  57. whiteSpace: 'nowrap',
  58. fontWeight: 'bolder',
  59. zIndex: 2,
  60. pointerEvents: 'none',
  61. transitionProperty: 'color',
  62. lineHeight: 1,
  63. userSelect: 'none',
  64. })
  65. LabelWrapper.displayName = 'span'
  66. const Input = styled('select')({
  67. display: 'block',
  68. backgroundColor: 'var(--color-bg, white)',
  69. color: 'var(--color-fg, black)',
  70. appearance: 'none',
  71. boxSizing: 'border-box',
  72. position: 'relative',
  73. border: 0,
  74. paddingLeft: '1rem',
  75. margin: 0,
  76. font: 'inherit',
  77. minHeight: '4rem',
  78. minWidth: '3rem',
  79. maxWidth: '100%',
  80. width: '100%',
  81. zIndex: 1,
  82. cursor: 'pointer',
  83. transitionProperty: 'background-color, color',
  84. ':focus': {
  85. outline: 0,
  86. },
  87. ':disabled': {
  88. cursor: 'not-allowed',
  89. },
  90. '::-moz-focus-inner': {
  91. outline: 0,
  92. border: 0,
  93. },
  94. })
  95. Input.displayName = 'select'
  96. const Border = styled('span')({
  97. borderColor: 'var(--color-accent, blue)',
  98. boxSizing: 'border-box',
  99. display: 'inline-block',
  100. borderWidth: '0.125rem',
  101. borderStyle: 'solid',
  102. position: 'absolute',
  103. top: 0,
  104. left: 0,
  105. width: '100%',
  106. height: '100%',
  107. borderRadius: 'inherit',
  108. zIndex: 2,
  109. pointerEvents: 'none',
  110. transitionProperty: 'border-color',
  111. '::before': {
  112. position: 'absolute',
  113. top: 0,
  114. left: 0,
  115. width: '100%',
  116. height: '100%',
  117. content: "''",
  118. borderRadius: '0.125rem',
  119. opacity: 0.5,
  120. pointerEvents: 'none',
  121. },
  122. [`${ComponentBase}:focus-within &::before`]: {
  123. boxShadow: '0 0 0 0.375rem var(--color-accent, blue)',
  124. },
  125. })
  126. const HintWrapper = styled('span')({
  127. boxSizing: 'border-box',
  128. position: 'absolute',
  129. bottom: 0,
  130. left: 0,
  131. paddingLeft: '1rem',
  132. fontSize: '0.85em',
  133. opacity: 0.5,
  134. maxWidth: '100%',
  135. overflow: 'hidden',
  136. textOverflow: 'ellipsis',
  137. whiteSpace: 'nowrap',
  138. zIndex: 2,
  139. pointerEvents: 'none',
  140. lineHeight: 1,
  141. userSelect: 'none',
  142. })
  143. const IndicatorWrapper = styled('span')({
  144. color: 'var(--color-accent, blue)',
  145. boxSizing: 'border-box',
  146. position: 'absolute',
  147. top: 0,
  148. right: 0,
  149. height: '100%',
  150. display: 'grid',
  151. placeContent: 'center',
  152. padding: '0 1rem',
  153. zIndex: 1,
  154. pointerEvents: 'none',
  155. transitionProperty: 'color',
  156. lineHeight: 1,
  157. userSelect: 'none',
  158. })
  159. const propTypes = {
  160. /**
  161. * Short textual description indicating the nature of the component's value.
  162. */
  163. label: PropTypes.any,
  164. /**
  165. * Short textual description as guidelines for valid input values.
  166. */
  167. hint: PropTypes.any,
  168. /**
  169. * Size of the component.
  170. */
  171. size: PropTypes.oneOf<Size>(['small', 'medium', 'large']),
  172. /**
  173. * Can multiple values be selected?
  174. */
  175. multiple: PropTypes.bool,
  176. /**
  177. * Is the component active?
  178. */
  179. disabled: PropTypes.bool,
  180. /**
  181. * Name of the form field associated with this component.
  182. */
  183. name: PropTypes.string,
  184. /**
  185. * Does the button display a border?
  186. */
  187. border: PropTypes.bool,
  188. /**
  189. * Event handler triggered when the component changes value.
  190. */
  191. onChange: PropTypes.func,
  192. /**
  193. * Event handler triggered when the component receives focus.
  194. */
  195. onFocus: PropTypes.func,
  196. /**
  197. * Event handler triggered when the component loses focus.
  198. */
  199. onBlur: PropTypes.func,
  200. }
  201. type Props = PropTypes.InferProps<typeof propTypes>
  202. const Select = React.forwardRef<HTMLSelectElement, Props>(
  203. (
  204. {
  205. label = '',
  206. hint = '',
  207. size = 'medium',
  208. multiple = false,
  209. disabled = false,
  210. name,
  211. children,
  212. border = false,
  213. onChange,
  214. onFocus,
  215. onBlur,
  216. ...etcProps
  217. },
  218. ref,
  219. ) => {
  220. return (
  221. <ComponentBase
  222. style={{
  223. opacity: disabled ? 0.5 : undefined,
  224. }}
  225. >
  226. {border && <Border />}
  227. <CaptureArea>
  228. <LabelWrapper
  229. style={{
  230. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  231. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  232. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '0.5rem',
  233. fontSize: SECONDARY_TEXT_SIZES[size!],
  234. }}
  235. >
  236. {stringify(label)}
  237. </LabelWrapper>
  238. {stringify(label).length > 0 && ' '}
  239. <Input
  240. {...etcProps}
  241. onChange={onChange as React.ChangeEventHandler}
  242. onFocus={onFocus as React.FocusEventHandler}
  243. onBlur={onBlur as React.FocusEventHandler}
  244. ref={ref}
  245. disabled={disabled!}
  246. multiple={multiple!}
  247. name={name!}
  248. style={{
  249. verticalAlign: 'top',
  250. fontSize: INPUT_FONT_SIZES[size!],
  251. height: multiple ? undefined : MIN_HEIGHTS[size!],
  252. minHeight: MIN_HEIGHTS[size!],
  253. resize: multiple ? 'vertical' : undefined,
  254. paddingTop: VERTICAL_PADDING_SIZES[size!],
  255. paddingBottom: VERTICAL_PADDING_SIZES[size!],
  256. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '1rem',
  257. }}
  258. >
  259. {children}
  260. </Input>
  261. </CaptureArea>
  262. {stringify(hint).length > 0 && ' '}
  263. {stringify(hint).length > 0 && (
  264. <HintWrapper
  265. style={{
  266. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  267. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  268. paddingRight: MIN_HEIGHTS[size!],
  269. fontSize: SECONDARY_TEXT_SIZES[size!],
  270. }}
  271. >
  272. ({stringify(hint)})
  273. </HintWrapper>
  274. )}
  275. {!multiple && (
  276. <IndicatorWrapper
  277. style={{
  278. width: MIN_HEIGHTS[size!],
  279. }}
  280. >
  281. <Icon name="chevron-down" label="" />
  282. </IndicatorWrapper>
  283. )}
  284. </ComponentBase>
  285. )
  286. },
  287. )
  288. Select.propTypes = propTypes
  289. Select.displayName = 'Select'
  290. export default Select