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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 { ChangeEvent, InputHTMLAttributes, TextareaHTMLAttributes } from 'react'
  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)',
  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. borderRadius: 'inherit',
  45. overflow: 'hidden',
  46. })
  47. CaptureArea.displayName = 'label'
  48. const LabelWrapper = styled('span')({
  49. color: 'var(--color-accent, blue)',
  50. boxSizing: 'border-box',
  51. position: 'absolute',
  52. top: 0,
  53. left: 0,
  54. paddingLeft: '0.5rem',
  55. fontSize: '0.85em',
  56. maxWidth: '100%',
  57. overflow: 'hidden',
  58. textOverflow: 'ellipsis',
  59. whiteSpace: 'nowrap',
  60. fontWeight: 'bolder',
  61. zIndex: 2,
  62. pointerEvents: 'none',
  63. transitionProperty: 'color',
  64. lineHeight: 1,
  65. userSelect: 'none',
  66. })
  67. LabelWrapper.displayName = 'span'
  68. const Border = styled('span')({
  69. 'borderColor': 'var(--color-accent, blue)',
  70. 'boxSizing': 'border-box',
  71. 'display': 'inline-block',
  72. 'borderWidth': '0.125rem',
  73. 'borderStyle': 'solid',
  74. 'position': 'absolute',
  75. 'top': 0,
  76. 'left': 0,
  77. 'width': '100%',
  78. 'height': '100%',
  79. 'borderRadius': 'inherit',
  80. 'zIndex': 2,
  81. 'pointerEvents': 'none',
  82. 'transitionProperty': 'border-color',
  83. '::before': {
  84. position: 'absolute',
  85. top: 0,
  86. left: 0,
  87. width: '100%',
  88. height: '100%',
  89. content: "''",
  90. borderRadius: '0.125rem',
  91. opacity: 0.5,
  92. pointerEvents: 'none',
  93. },
  94. [`${ComponentBase}:focus-within &::before`]: {
  95. boxShadow: '0 0 0 0.375rem var(--color-accent, blue)',
  96. },
  97. })
  98. const Input = styled('input')({
  99. 'backgroundColor': 'var(--color-bg, white)',
  100. 'color': 'var(--color-fg, black)',
  101. 'verticalAlign': 'top',
  102. 'paddingTop': 0,
  103. 'paddingBottom': 0,
  104. 'boxSizing': 'border-box',
  105. 'position': 'relative',
  106. 'border': 0,
  107. 'borderRadius': 'inherit',
  108. 'paddingLeft': '1rem',
  109. 'margin': 0,
  110. 'font': 'inherit',
  111. 'minHeight': '4rem',
  112. 'minWidth': '16rem',
  113. 'maxWidth': '100%',
  114. 'zIndex': 1,
  115. 'transitionProperty': 'background-color, color',
  116. ':focus': {
  117. outline: 0,
  118. color: 'var(--color-fg, black)',
  119. },
  120. ':disabled': {
  121. cursor: 'not-allowed',
  122. },
  123. })
  124. Input.displayName = 'input'
  125. const TextArea = styled('textarea')({
  126. 'backgroundColor': 'var(--color-bg, white)',
  127. 'color': 'var(--color-fg, black)',
  128. 'verticalAlign': 'top',
  129. 'width': '100%',
  130. 'boxSizing': 'border-box',
  131. 'position': 'relative',
  132. 'border': 0,
  133. 'borderRadius': 'inherit',
  134. 'paddingLeft': '1rem',
  135. 'margin': 0,
  136. 'font': 'inherit',
  137. 'minHeight': '4rem',
  138. 'minWidth': '16rem',
  139. 'maxWidth': '100%',
  140. 'zIndex': 1,
  141. 'transitionProperty': 'background-color, color',
  142. ':focus': {
  143. outline: 0,
  144. },
  145. })
  146. TextArea.displayName = 'textarea'
  147. const HintWrapper = styled('span')({
  148. boxSizing: 'border-box',
  149. position: 'absolute',
  150. bottom: 0,
  151. left: 0,
  152. paddingLeft: '1rem',
  153. fontSize: '0.85em',
  154. opacity: 0.5,
  155. maxWidth: '100%',
  156. overflow: 'hidden',
  157. textOverflow: 'ellipsis',
  158. whiteSpace: 'nowrap',
  159. zIndex: 2,
  160. pointerEvents: 'none',
  161. lineHeight: 1,
  162. userSelect: 'none',
  163. })
  164. const IndicatorWrapper = styled('span')({
  165. color: 'var(--color-accent, blue)',
  166. boxSizing: 'border-box',
  167. position: 'absolute',
  168. bottom: 0,
  169. right: 0,
  170. display: 'grid',
  171. placeContent: 'center',
  172. padding: '0 1rem',
  173. zIndex: 1,
  174. pointerEvents: 'none',
  175. transitionProperty: 'color',
  176. lineHeight: 1,
  177. userSelect: 'none',
  178. })
  179. const propTypes = {
  180. /**
  181. * Short textual description indicating the nature of the component's value.
  182. */
  183. label: PropTypes.any,
  184. /**
  185. * Class name for the component, used for styling.
  186. */
  187. className: PropTypes.string,
  188. /**
  189. * Short textual description as guidelines for valid input values.
  190. */
  191. hint: PropTypes.any,
  192. /**
  193. * Size of the component.
  194. */
  195. size: PropTypes.oneOf<Size>(['small', 'medium', 'large']),
  196. /**
  197. * Additional description, usually graphical, indicating the nature of the component's value.
  198. */
  199. indicator: PropTypes.node,
  200. /**
  201. * Should the component take up the remaining space parallel to the content flow?
  202. */
  203. block: PropTypes.bool,
  204. /**
  205. * Should the component accept multiple lines of input?
  206. */
  207. multiline: PropTypes.bool,
  208. /**
  209. * Is the component active?
  210. */
  211. disabled: PropTypes.bool,
  212. /**
  213. * Should the component resize itself to show all its value?
  214. */
  215. autoResize: PropTypes.bool,
  216. /**
  217. * Placeholder of the component when there is no value.
  218. */
  219. placeholder: PropTypes.string,
  220. }
  221. type Props = PropTypes.InferProps<typeof propTypes>
  222. /**
  223. * Component for inputting textual values.
  224. * @type {React.ComponentType<{readonly label?: string, readonly hint?: string, readonly multiline?: boolean, readonly
  225. * className?: string, readonly indicator?: *, readonly size?: 'small' | 'medium' | 'large', readonly block?:
  226. * boolean} & React.ClassAttributes<unknown>>}
  227. */
  228. const TextInput = React.forwardRef<HTMLInputElement | HTMLTextAreaElement, Props>(
  229. (
  230. {
  231. label = '',
  232. className = '',
  233. hint = '',
  234. indicator = null,
  235. size: sizeProp = 'medium',
  236. block = false,
  237. multiline = false,
  238. disabled = false,
  239. autoResize = false,
  240. placeholder = '',
  241. },
  242. ref,
  243. ) => {
  244. const size: Size = sizeProp as Size
  245. return (
  246. <ComponentBase
  247. style={{
  248. display: block ? 'block' : 'inline-block',
  249. opacity: disabled ? 0.5 : undefined,
  250. }}
  251. >
  252. <Border />
  253. <CaptureArea className={className!}>
  254. <LabelWrapper
  255. style={{
  256. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  257. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  258. paddingRight: indicator ? MIN_HEIGHTS[size!] : '0.5rem',
  259. fontSize: SECONDARY_TEXT_SIZES[size!],
  260. }}
  261. >
  262. {stringify(label)}
  263. </LabelWrapper>
  264. {stringify(label).length > 0 && ' '}
  265. {multiline && (
  266. <TextArea
  267. placeholder={placeholder!}
  268. ref={ref as React.Ref<HTMLTextAreaElement>}
  269. disabled={disabled!}
  270. style={{
  271. display: block ? 'block' : 'inline-block',
  272. fontSize: INPUT_FONT_SIZES[size!],
  273. minHeight: MIN_HEIGHTS[size!],
  274. paddingTop: VERTICAL_PADDING_SIZES[size!],
  275. paddingBottom: VERTICAL_PADDING_SIZES[size!],
  276. paddingRight: indicator ? MIN_HEIGHTS[size!] : '1rem',
  277. }}
  278. />
  279. )}
  280. {!multiline && (
  281. <Input
  282. placeholder={placeholder!}
  283. ref={ref as React.Ref<HTMLInputElement>}
  284. disabled={disabled!}
  285. style={{
  286. display: block ? 'block' : 'inline-block',
  287. fontSize: INPUT_FONT_SIZES[size!],
  288. width: block ? '100%' : undefined,
  289. minHeight: MIN_HEIGHTS[size!],
  290. paddingRight: indicator ? MIN_HEIGHTS[size!] : '1rem',
  291. }}
  292. />
  293. )}
  294. </CaptureArea>
  295. {stringify(hint).length > 0 && ' '}
  296. {stringify(hint).length > 0 && (
  297. <HintWrapper
  298. style={{
  299. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  300. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  301. paddingRight: indicator ? MIN_HEIGHTS[size!] : '1rem',
  302. fontSize: SECONDARY_TEXT_SIZES[size!],
  303. }}
  304. >
  305. ({stringify(hint)})
  306. </HintWrapper>
  307. )}
  308. {(indicator as PropTypes.ReactComponentLike) && (
  309. <IndicatorWrapper
  310. style={{
  311. width: MIN_HEIGHTS[size!],
  312. height: MIN_HEIGHTS[size!],
  313. }}
  314. >
  315. {indicator}
  316. </IndicatorWrapper>
  317. )}
  318. </ComponentBase>
  319. )
  320. },
  321. )
  322. TextInput.propTypes = propTypes
  323. TextInput.displayName = 'TextInput'
  324. export default TextInput