Common front-end components for Web using the Tesseract design system, written for React. https://make.modal.sh/tesseract/web/react/common
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

332 righe
7.7 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. const MIN_HEIGHTS: SizeMap<string | number> = {
  7. small: '2.5rem',
  8. medium: '3rem',
  9. large: '4rem',
  10. }
  11. const LABEL_VERTICAL_PADDING_SIZES: SizeMap<string | number> = {
  12. small: '0.125rem',
  13. medium: '0.25rem',
  14. large: '0.5rem',
  15. }
  16. const VERTICAL_PADDING_SIZES: SizeMap<string | number> = {
  17. small: '0.6rem',
  18. medium: '0.85rem',
  19. large: '1.25rem',
  20. }
  21. const INPUT_FONT_SIZES: SizeMap<string | number> = {
  22. small: '0.85em',
  23. medium: '0.85em',
  24. large: '1em',
  25. }
  26. const SECONDARY_TEXT_SIZES: SizeMap<string | number> = {
  27. small: '0.65em',
  28. medium: '0.75em',
  29. large: '0.85em',
  30. }
  31. const ComponentBase = styled('div')({
  32. position: 'relative',
  33. borderRadius: '0.25rem',
  34. fontFamily: 'var(--font-family-base, sans-serif)',
  35. maxWidth: '100%',
  36. ':focus-within': {
  37. '--color-accent': 'var(--color-active, Highlight)',
  38. },
  39. })
  40. ComponentBase.displayName = 'div'
  41. const CaptureArea = styled('label')({
  42. display: 'block',
  43. borderRadius: 'inherit',
  44. overflow: 'hidden',
  45. })
  46. CaptureArea.displayName = 'label'
  47. const LabelWrapper = styled('span')({
  48. color: 'var(--color-accent, blue)',
  49. boxSizing: 'border-box',
  50. position: 'absolute',
  51. top: 0,
  52. left: 0,
  53. paddingLeft: '0.5rem',
  54. fontSize: '0.85em',
  55. maxWidth: '100%',
  56. overflow: 'hidden',
  57. textOverflow: 'ellipsis',
  58. whiteSpace: 'nowrap',
  59. fontWeight: 'bolder',
  60. zIndex: 2,
  61. pointerEvents: 'none',
  62. transitionProperty: 'color',
  63. lineHeight: 1,
  64. userSelect: 'none',
  65. })
  66. LabelWrapper.displayName = 'span'
  67. const Border = styled('span')({
  68. borderColor: 'var(--color-accent, blue)',
  69. boxSizing: 'border-box',
  70. display: 'inline-block',
  71. borderWidth: '0.125rem',
  72. borderStyle: 'solid',
  73. position: 'absolute',
  74. top: 0,
  75. left: 0,
  76. width: '100%',
  77. height: '100%',
  78. borderRadius: 'inherit',
  79. zIndex: 2,
  80. pointerEvents: 'none',
  81. transitionProperty: 'border-color',
  82. '::before': {
  83. position: 'absolute',
  84. top: 0,
  85. left: 0,
  86. width: '100%',
  87. height: '100%',
  88. content: "''",
  89. borderRadius: '0.125rem',
  90. opacity: 0.5,
  91. pointerEvents: 'none',
  92. },
  93. [`${ComponentBase}:focus-within &::before`]: {
  94. boxShadow: '0 0 0 0.375rem var(--color-accent, blue)',
  95. },
  96. })
  97. const Input = styled('input')({
  98. display: 'block',
  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: '8rem',
  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. display: 'block',
  127. backgroundColor: 'var(--color-bg, white)',
  128. color: 'var(--color-fg, black)',
  129. verticalAlign: 'top',
  130. width: '100%',
  131. boxSizing: 'border-box',
  132. position: 'relative',
  133. border: 0,
  134. borderRadius: 'inherit',
  135. paddingLeft: '1rem',
  136. margin: 0,
  137. font: 'inherit',
  138. minHeight: '4rem',
  139. minWidth: '16rem',
  140. maxWidth: '100%',
  141. zIndex: 1,
  142. transitionProperty: 'background-color, color',
  143. ':focus': {
  144. outline: 0,
  145. },
  146. })
  147. TextArea.displayName = 'textarea'
  148. const HintWrapper = styled('span')({
  149. boxSizing: 'border-box',
  150. position: 'absolute',
  151. bottom: 0,
  152. left: 0,
  153. paddingLeft: '1rem',
  154. fontSize: '0.85em',
  155. opacity: 0.5,
  156. maxWidth: '100%',
  157. overflow: 'hidden',
  158. textOverflow: 'ellipsis',
  159. whiteSpace: 'nowrap',
  160. zIndex: 2,
  161. pointerEvents: 'none',
  162. lineHeight: 1,
  163. userSelect: 'none',
  164. })
  165. const IndicatorWrapper = styled('span')({
  166. color: 'var(--color-accent, blue)',
  167. boxSizing: 'border-box',
  168. position: 'absolute',
  169. bottom: 0,
  170. right: 0,
  171. display: 'grid',
  172. placeContent: 'center',
  173. padding: '0 1rem',
  174. zIndex: 1,
  175. pointerEvents: 'none',
  176. transitionProperty: 'color',
  177. lineHeight: 1,
  178. userSelect: 'none',
  179. })
  180. const propTypes = {
  181. /**
  182. * Short textual description indicating the nature of the component's value.
  183. */
  184. label: PropTypes.any,
  185. /**
  186. * Class name for the component, used for styling.
  187. */
  188. className: PropTypes.string,
  189. /**
  190. * Short textual description as guidelines for valid input values.
  191. */
  192. hint: PropTypes.any,
  193. /**
  194. * Size of the component.
  195. */
  196. size: PropTypes.oneOf<Size>(['small', 'medium', 'large']),
  197. /**
  198. * Additional description, usually graphical, indicating the nature of the component's value.
  199. */
  200. indicator: PropTypes.node,
  201. /**
  202. * Should the component accept multiple lines of input?
  203. */
  204. multiline: PropTypes.bool,
  205. /**
  206. * Is the component active?
  207. */
  208. disabled: PropTypes.bool,
  209. /**
  210. * Should the component resize itself to show all its value?
  211. */
  212. autoResize: PropTypes.bool,
  213. /**
  214. * Placeholder of the component when there is no value.
  215. */
  216. placeholder: PropTypes.string,
  217. }
  218. type Props = PropTypes.InferProps<typeof propTypes>
  219. const TextInput = React.forwardRef<HTMLInputElement | HTMLTextAreaElement, Props>(
  220. (
  221. {
  222. label = '',
  223. className = '',
  224. hint = '',
  225. indicator = null,
  226. size = 'medium',
  227. multiline = false,
  228. disabled = false,
  229. autoResize = false,
  230. placeholder = '',
  231. },
  232. ref,
  233. ) => (
  234. <ComponentBase
  235. style={{
  236. opacity: disabled ? 0.5 : undefined,
  237. }}
  238. >
  239. <Border />
  240. <CaptureArea className={className!}>
  241. <LabelWrapper
  242. style={{
  243. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  244. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  245. paddingRight: indicator ? MIN_HEIGHTS[size!] : '0.5rem',
  246. fontSize: SECONDARY_TEXT_SIZES[size!],
  247. }}
  248. >
  249. {stringify(label)}
  250. </LabelWrapper>
  251. {stringify(label).length > 0 && ' '}
  252. {multiline && (
  253. <TextArea
  254. placeholder={placeholder!}
  255. ref={ref as React.Ref<HTMLTextAreaElement>}
  256. disabled={disabled!}
  257. style={{
  258. fontSize: INPUT_FONT_SIZES[size!],
  259. minHeight: MIN_HEIGHTS[size!],
  260. paddingTop: VERTICAL_PADDING_SIZES[size!],
  261. paddingBottom: VERTICAL_PADDING_SIZES[size!],
  262. paddingRight: indicator ? MIN_HEIGHTS[size!] : '1rem',
  263. }}
  264. />
  265. )}
  266. {!multiline && (
  267. <Input
  268. placeholder={placeholder!}
  269. ref={ref as React.Ref<HTMLInputElement>}
  270. disabled={disabled!}
  271. style={{
  272. fontSize: INPUT_FONT_SIZES[size!],
  273. minHeight: MIN_HEIGHTS[size!],
  274. paddingRight: indicator ? MIN_HEIGHTS[size!] : '1rem',
  275. }}
  276. />
  277. )}
  278. </CaptureArea>
  279. {stringify(hint).length > 0 && ' '}
  280. {stringify(hint).length > 0 && (
  281. <HintWrapper
  282. style={{
  283. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  284. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  285. paddingRight: indicator ? MIN_HEIGHTS[size!] : '1rem',
  286. fontSize: SECONDARY_TEXT_SIZES[size!],
  287. }}
  288. >
  289. ({stringify(hint)})
  290. </HintWrapper>
  291. )}
  292. {(indicator as PropTypes.ReactComponentLike) && (
  293. <IndicatorWrapper
  294. style={{
  295. width: MIN_HEIGHTS[size!],
  296. height: MIN_HEIGHTS[size!],
  297. }}
  298. >
  299. {indicator}
  300. </IndicatorWrapper>
  301. )}
  302. </ComponentBase>
  303. ),
  304. )
  305. TextInput.propTypes = propTypes
  306. TextInput.displayName = 'TextInput'
  307. export default TextInput