Common front-end components for Web using the Tesseract design system, written for React. https://make.modal.sh/tesseract/web/react/common
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

288 lines
6.5 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. type Props = PropTypes.InferProps<typeof propTypes>
  186. const Select = React.forwardRef<HTMLSelectElement, Props>(
  187. (
  188. {
  189. label = '',
  190. hint = '',
  191. size = 'medium',
  192. multiple = false,
  193. disabled = false,
  194. name,
  195. children,
  196. },
  197. ref,
  198. ) => {
  199. return (
  200. <ComponentBase
  201. style={{
  202. opacity: disabled ? 0.5 : undefined,
  203. }}
  204. >
  205. <Border />
  206. <CaptureArea>
  207. <LabelWrapper
  208. style={{
  209. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  210. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  211. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '0.5rem',
  212. fontSize: SECONDARY_TEXT_SIZES[size!],
  213. }}
  214. >
  215. {stringify(label)}
  216. </LabelWrapper>
  217. {stringify(label).length > 0 && ' '}
  218. <Input
  219. ref={ref}
  220. disabled={disabled!}
  221. multiple={multiple!}
  222. name={name!}
  223. style={{
  224. verticalAlign: 'top',
  225. fontSize: INPUT_FONT_SIZES[size!],
  226. height: multiple ? undefined : MIN_HEIGHTS[size!],
  227. minHeight: MIN_HEIGHTS[size!],
  228. resize: multiple ? 'vertical' : undefined,
  229. paddingTop: VERTICAL_PADDING_SIZES[size!],
  230. paddingBottom: VERTICAL_PADDING_SIZES[size!],
  231. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '1rem',
  232. }}
  233. >
  234. {children}
  235. </Input>
  236. </CaptureArea>
  237. {stringify(hint).length > 0 && ' '}
  238. {stringify(hint).length > 0 && (
  239. <HintWrapper
  240. style={{
  241. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  242. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  243. paddingRight: MIN_HEIGHTS[size!],
  244. fontSize: SECONDARY_TEXT_SIZES[size!],
  245. }}
  246. >
  247. ({stringify(hint)})
  248. </HintWrapper>
  249. )}
  250. {!multiple && (
  251. <IndicatorWrapper
  252. style={{
  253. width: MIN_HEIGHTS[size!],
  254. }}
  255. >
  256. <Icon name="chevron-down" label="" />
  257. </IndicatorWrapper>
  258. )}
  259. </ComponentBase>
  260. )
  261. },
  262. )
  263. Select.propTypes = propTypes
  264. Select.displayName = 'Select'
  265. export default Select