@molecule/app-ui-react-native 1.0.10 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/README.md +211 -331
  2. package/dist/components/Button.d.ts +2 -2
  3. package/dist/components/Button.d.ts.map +1 -1
  4. package/dist/components/Button.js +2 -2
  5. package/dist/components/Card.d.ts +3 -2
  6. package/dist/components/Card.d.ts.map +1 -1
  7. package/dist/components/Card.js +11 -3
  8. package/dist/components/Checkbox.d.ts +2 -2
  9. package/dist/components/Checkbox.d.ts.map +1 -1
  10. package/dist/components/Checkbox.js +4 -7
  11. package/dist/components/Input.d.ts +13 -2
  12. package/dist/components/Input.d.ts.map +1 -1
  13. package/dist/components/Input.js +5 -11
  14. package/dist/components/Layout.d.ts +3 -2
  15. package/dist/components/Layout.d.ts.map +1 -1
  16. package/dist/components/Layout.js +2 -2
  17. package/dist/components/Select.d.ts +2 -2
  18. package/dist/components/Select.d.ts.map +1 -1
  19. package/dist/components/Select.js +4 -6
  20. package/dist/components/Switch.d.ts +2 -2
  21. package/dist/components/Switch.d.ts.map +1 -1
  22. package/dist/components/Switch.js +3 -7
  23. package/dist/components/Textarea.d.ts +13 -2
  24. package/dist/components/Textarea.d.ts.map +1 -1
  25. package/dist/components/Textarea.js +2 -6
  26. package/dist/elements/Anchor.d.ts +15 -0
  27. package/dist/elements/Anchor.d.ts.map +1 -0
  28. package/dist/elements/Anchor.js +57 -0
  29. package/dist/elements/Box.d.ts +19 -0
  30. package/dist/elements/Box.d.ts.map +1 -0
  31. package/dist/elements/Box.js +150 -0
  32. package/dist/elements/CheckField.d.ts +18 -0
  33. package/dist/elements/CheckField.d.ts.map +1 -0
  34. package/dist/elements/CheckField.js +103 -0
  35. package/dist/elements/Disclosure.d.ts +11 -0
  36. package/dist/elements/Disclosure.d.ts.map +1 -0
  37. package/dist/elements/Disclosure.js +67 -0
  38. package/dist/elements/Fieldset.d.ts +16 -0
  39. package/dist/elements/Fieldset.d.ts.map +1 -0
  40. package/dist/elements/Fieldset.js +34 -0
  41. package/dist/elements/FileField.d.ts +12 -0
  42. package/dist/elements/FileField.d.ts.map +1 -0
  43. package/dist/elements/FileField.js +111 -0
  44. package/dist/elements/FormArea.d.ts +18 -0
  45. package/dist/elements/FormArea.d.ts.map +1 -0
  46. package/dist/elements/FormArea.js +42 -0
  47. package/dist/elements/GlyphIcon.d.ts +19 -0
  48. package/dist/elements/GlyphIcon.d.ts.map +1 -0
  49. package/dist/elements/GlyphIcon.js +43 -0
  50. package/dist/elements/Image.d.ts +17 -0
  51. package/dist/elements/Image.d.ts.map +1 -0
  52. package/dist/elements/Image.js +26 -0
  53. package/dist/elements/Link.d.ts +20 -0
  54. package/dist/elements/Link.d.ts.map +1 -0
  55. package/dist/elements/Link.js +53 -0
  56. package/dist/elements/Pressable.d.ts +18 -0
  57. package/dist/elements/Pressable.d.ts.map +1 -0
  58. package/dist/elements/Pressable.js +107 -0
  59. package/dist/elements/PulseDots.d.ts +15 -0
  60. package/dist/elements/PulseDots.d.ts.map +1 -0
  61. package/dist/elements/PulseDots.js +78 -0
  62. package/dist/elements/RangeField.d.ts +11 -0
  63. package/dist/elements/RangeField.d.ts.map +1 -0
  64. package/dist/elements/RangeField.js +259 -0
  65. package/dist/elements/RichText.d.ts +23 -0
  66. package/dist/elements/RichText.d.ts.map +1 -0
  67. package/dist/elements/RichText.js +55 -0
  68. package/dist/elements/Rule.d.ts +11 -0
  69. package/dist/elements/Rule.d.ts.map +1 -0
  70. package/dist/elements/Rule.js +19 -0
  71. package/dist/elements/ScrollArea.d.ts +21 -0
  72. package/dist/elements/ScrollArea.d.ts.map +1 -0
  73. package/dist/elements/ScrollArea.js +101 -0
  74. package/dist/elements/SelectAction.d.ts +20 -0
  75. package/dist/elements/SelectAction.d.ts.map +1 -0
  76. package/dist/elements/SelectAction.js +113 -0
  77. package/dist/elements/SelectField.d.ts +11 -0
  78. package/dist/elements/SelectField.d.ts.map +1 -0
  79. package/dist/elements/SelectField.js +220 -0
  80. package/dist/elements/SelectMenu.d.ts +19 -0
  81. package/dist/elements/SelectMenu.d.ts.map +1 -0
  82. package/dist/elements/SelectMenu.js +122 -0
  83. package/dist/elements/Svg.d.ts +155 -0
  84. package/dist/elements/Svg.d.ts.map +1 -0
  85. package/dist/elements/Svg.js +525 -0
  86. package/dist/elements/TableBox.d.ts +19 -0
  87. package/dist/elements/TableBox.d.ts.map +1 -0
  88. package/dist/elements/TableBox.js +87 -0
  89. package/dist/elements/TableCell.d.ts +16 -0
  90. package/dist/elements/TableCell.d.ts.map +1 -0
  91. package/dist/elements/TableCell.js +69 -0
  92. package/dist/elements/Text.d.ts +17 -0
  93. package/dist/elements/Text.d.ts.map +1 -0
  94. package/dist/elements/Text.js +96 -0
  95. package/dist/elements/TextField.d.ts +25 -0
  96. package/dist/elements/TextField.d.ts.map +1 -0
  97. package/dist/elements/TextField.js +257 -0
  98. package/dist/elements/accessibility.d.ts +36 -0
  99. package/dist/elements/accessibility.d.ts.map +1 -0
  100. package/dist/elements/accessibility.js +239 -0
  101. package/dist/elements/className.d.ts +45 -0
  102. package/dist/elements/className.d.ts.map +1 -0
  103. package/dist/elements/className.js +8 -0
  104. package/dist/elements/decimal.d.ts +5 -0
  105. package/dist/elements/decimal.d.ts.map +1 -0
  106. package/dist/elements/decimal.js +11 -0
  107. package/dist/elements/documentUrl.d.ts +35 -0
  108. package/dist/elements/documentUrl.d.ts.map +1 -0
  109. package/dist/elements/documentUrl.js +89 -0
  110. package/dist/elements/drag-drop.d.ts +11 -0
  111. package/dist/elements/drag-drop.d.ts.map +1 -0
  112. package/dist/elements/drag-drop.js +277 -0
  113. package/dist/elements/drag.d.ts +47 -0
  114. package/dist/elements/drag.d.ts.map +1 -0
  115. package/dist/elements/drag.js +362 -0
  116. package/dist/elements/events.d.ts +45 -0
  117. package/dist/elements/events.d.ts.map +1 -0
  118. package/dist/elements/events.js +101 -0
  119. package/dist/elements/fieldBounds.d.ts +5 -0
  120. package/dist/elements/fieldBounds.d.ts.map +1 -0
  121. package/dist/elements/fieldBounds.js +60 -0
  122. package/dist/elements/flow.d.ts +36 -0
  123. package/dist/elements/flow.d.ts.map +1 -0
  124. package/dist/elements/flow.js +47 -0
  125. package/dist/elements/form.d.ts +48 -0
  126. package/dist/elements/form.d.ts.map +1 -0
  127. package/dist/elements/form.js +112 -0
  128. package/dist/elements/grid.d.ts +96 -0
  129. package/dist/elements/grid.d.ts.map +1 -0
  130. package/dist/elements/grid.js +272 -0
  131. package/dist/elements/index.d.ts +36 -0
  132. package/dist/elements/index.d.ts.map +1 -0
  133. package/dist/elements/index.js +35 -0
  134. package/dist/elements/inherited.d.ts +181 -0
  135. package/dist/elements/inherited.d.ts.map +1 -0
  136. package/dist/elements/inherited.js +512 -0
  137. package/dist/elements/labels.d.ts +39 -0
  138. package/dist/elements/labels.d.ts.map +1 -0
  139. package/dist/elements/labels.js +163 -0
  140. package/dist/elements/measure.d.ts +27 -0
  141. package/dist/elements/measure.d.ts.map +1 -0
  142. package/dist/elements/measure.js +24 -0
  143. package/dist/elements/outsidePress.d.ts +31 -0
  144. package/dist/elements/outsidePress.d.ts.map +1 -0
  145. package/dist/elements/outsidePress.js +94 -0
  146. package/dist/elements/paste.d.ts +22 -0
  147. package/dist/elements/paste.d.ts.map +1 -0
  148. package/dist/elements/paste.js +49 -0
  149. package/dist/elements/press.d.ts +41 -0
  150. package/dist/elements/press.d.ts.map +1 -0
  151. package/dist/elements/press.js +131 -0
  152. package/dist/elements/rangeKeyboard.d.ts +41 -0
  153. package/dist/elements/rangeKeyboard.d.ts.map +1 -0
  154. package/dist/elements/rangeKeyboard.js +58 -0
  155. package/dist/elements/rangeValues.d.ts +20 -0
  156. package/dist/elements/rangeValues.d.ts.map +1 -0
  157. package/dist/elements/rangeValues.js +101 -0
  158. package/dist/elements/ref.d.ts +16 -0
  159. package/dist/elements/ref.d.ts.map +1 -0
  160. package/dist/elements/ref.js +22 -0
  161. package/dist/elements/reveal.d.ts +8 -0
  162. package/dist/elements/reveal.d.ts.map +1 -0
  163. package/dist/elements/reveal.js +10 -0
  164. package/dist/elements/scrollReveal.d.ts +19 -0
  165. package/dist/elements/scrollReveal.d.ts.map +1 -0
  166. package/dist/elements/scrollReveal.js +52 -0
  167. package/dist/elements/selectOptions.d.ts +25 -0
  168. package/dist/elements/selectOptions.d.ts.map +1 -0
  169. package/dist/elements/selectOptions.js +60 -0
  170. package/dist/elements/style.d.ts +30 -0
  171. package/dist/elements/style.d.ts.map +1 -0
  172. package/dist/elements/style.js +363 -0
  173. package/dist/elements/svgHover.d.ts +41 -0
  174. package/dist/elements/svgHover.d.ts.map +1 -0
  175. package/dist/elements/svgHover.js +152 -0
  176. package/dist/elements/table.d.ts +52 -0
  177. package/dist/elements/table.d.ts.map +1 -0
  178. package/dist/elements/table.js +104 -0
  179. package/dist/elements/types.d.ts +25 -0
  180. package/dist/elements/types.d.ts.map +1 -0
  181. package/dist/elements/types.js +8 -0
  182. package/dist/elements/viewport.d.ts +53 -0
  183. package/dist/elements/viewport.d.ts.map +1 -0
  184. package/dist/elements/viewport.js +62 -0
  185. package/dist/elements/wheelZoom.d.ts +11 -0
  186. package/dist/elements/wheelZoom.d.ts.map +1 -0
  187. package/dist/elements/wheelZoom.js +7 -0
  188. package/dist/index.d.ts +11 -6
  189. package/dist/index.d.ts.map +1 -1
  190. package/dist/index.js +9 -5
  191. package/dist/platform/focus.d.ts +31 -0
  192. package/dist/platform/focus.d.ts.map +1 -0
  193. package/dist/platform/focus.js +86 -0
  194. package/dist/platform/index.d.ts +31 -0
  195. package/dist/platform/index.d.ts.map +1 -0
  196. package/dist/platform/index.js +179 -0
  197. package/dist/platform/layout.d.ts +13 -0
  198. package/dist/platform/layout.d.ts.map +1 -0
  199. package/dist/platform/layout.js +15 -0
  200. package/dist/platform/position.d.ts +5 -0
  201. package/dist/platform/position.d.ts.map +1 -0
  202. package/dist/platform/position.js +177 -0
  203. package/dist/platform/stack.d.ts +86 -0
  204. package/dist/platform/stack.d.ts.map +1 -0
  205. package/dist/platform/stack.js +318 -0
  206. package/dist/types.d.ts +25 -0
  207. package/dist/types.d.ts.map +1 -0
  208. package/dist/types.js +1 -0
  209. package/package.json +66 -8
@@ -0,0 +1,69 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /**
3
+ * TableCell: a native cell that shares its column widths with the table.
4
+ *
5
+ * @module
6
+ */
7
+ import { useCallback, useContext, useId, useLayoutEffect, useMemo, useRef, } from 'react';
8
+ import { Pressable as NativePressable, View } from 'react-native';
9
+ import { nativeAccessibility } from './accessibility.js';
10
+ import { dataAttributes, ElementScope, layoutChildren, splitClasses, useInheritedText, useTagClasses, } from './inherited.js';
11
+ import { isPressable, pressProps } from './press.js';
12
+ import { useNativeRef } from './ref.js';
13
+ import { nativeStyle } from './style.js';
14
+ import { cellSpan, fixedLength, needsNaturalWidth, TableColumn, TableLayout } from './table.js';
15
+ /**
16
+ * Lays out the cell at the sum of its spanned column widths, with its header or cell role.
17
+ *
18
+ * @param props - The cell's props and ref.
19
+ * @returns The table cell.
20
+ */
21
+ export function TableCell(props) {
22
+ const { as, className, colSpan, style, webStyle, children, ref, scope } = props;
23
+ const table = useContext(TableLayout);
24
+ const start = useContext(TableColumn);
25
+ const id = useId();
26
+ const classes = useTagClasses(className, as);
27
+ const split = splitClasses(classes, useInheritedText(), dataAttributes(props));
28
+ const paint = nativeStyle(style, webStyle);
29
+ const fixed = fixedLength(paint?.width);
30
+ const span = cellSpan(colSpan);
31
+ // Keep each measurement tied to the content and styles that determine its natural width.
32
+ const textClasses = split.text;
33
+ const track = useMemo(() => start === undefined
34
+ ? undefined
35
+ : {
36
+ column: start,
37
+ span,
38
+ fixed,
39
+ content: { classes, children, style, webStyle, textClasses },
40
+ }, [start, span, fixed, classes, children, style, webStyle, textClasses]);
41
+ const register = table?.register;
42
+ const measure = table?.measure;
43
+ useLayoutEffect(() => track && register?.(id, track), [id, register, track]);
44
+ const width = start === undefined
45
+ ? undefined
46
+ : table?.widths?.slice(start, start + span).reduce((sum, value) => sum + value, 0);
47
+ const measuring = width === undefined && track !== undefined && needsNaturalWidth(track) && measure !== undefined;
48
+ const report = useCallback((natural) => {
49
+ if (track)
50
+ measure?.(id, track, natural);
51
+ }, [id, measure, track]);
52
+ const view = useRef(null);
53
+ const forward = useNativeRef(ref);
54
+ const setNode = useCallback((node) => {
55
+ view.current = node;
56
+ forward?.(node);
57
+ }, [forward]);
58
+ useLayoutEffect(() => {
59
+ if (!measuring)
60
+ return;
61
+ // A remeasurement that happens to retain the same width can omit onLayout.
62
+ const frame = requestAnimationFrame(() => view.current?.measure?.((_x, _y, next) => report(next)));
63
+ return () => cancelAnimationFrame(frame);
64
+ }, [measuring, report]);
65
+ const columnStyle = table && track ? { flexShrink: 0, width: width ?? 'auto' } : undefined;
66
+ const middle = split.verticalAlign === 'middle' ? { justifyContent: 'center' } : undefined;
67
+ const Host = isPressable(props) ? NativePressable : View;
68
+ return (_jsx(ElementScope, { split: split, children: _jsx(Host, { ...nativeAccessibility({ ...props, role: props.role ?? (scope === 'row' ? 'rowheader' : undefined) }, as), ...pressProps(props), ref: setNode, className: split.view || undefined, style: [paint, columnStyle, middle], onLayout: measuring ? (event) => report(event.nativeEvent.layout.width) : undefined, collapsable: !measuring, children: layoutChildren(children, split) }) }));
69
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Text: an element that holds text.
3
+ *
4
+ * @module
5
+ */
6
+ import type { ReactElement } from 'react';
7
+ import type { TextProps } from '@molecule/app-ui';
8
+ import type { ElementNode, ElementRefProps } from './types.js';
9
+ /**
10
+ * Text: a native `Text` (a heading for `h1`–`h6`) with the text classes its container hands down
11
+ * and the defaults a browser gives its tag (`strong` bolder, `code` monospace); it hands its text classes to the text nested in it.
12
+ *
13
+ * @param props - See {@link TextProps}.
14
+ * @returns The element.
15
+ */
16
+ export declare function Text(props: TextProps<ElementNode> & ElementRefProps): ReactElement;
17
+ //# sourceMappingURL=Text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Text.d.ts","sourceRoot":"","sources":["../../src/elements/Text.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAGzC,OAAO,KAAK,EAAc,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAqB7D,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AA2C9D;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,WAAW,CAAC,GAAG,eAAe,GAAG,YAAY,CAuClF"}
@@ -0,0 +1,96 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Text as NativeText } from 'react-native';
3
+ import { useOverlayTarget } from '../platform/focus.js';
4
+ import { nativeAccessibility } from './accessibility.js';
5
+ import { registerInline } from './flow.js';
6
+ import { useGridCell } from './grid.js';
7
+ import { blockInInline, dataAttributes, ElementScope, flowOf, splitClasses, textElementClasses, useInheritedFontStyle, useInheritedText, useLineStart, useTagClasses, } from './inherited.js';
8
+ import { useLabelPress, useLabelText } from './labels.js';
9
+ import { useNativeRef } from './ref.js';
10
+ import { nativeStyle, TEXT_TAG_STYLE } from './style.js';
11
+ /** Text tags that flow inline on the web; the others are blocks (a paragraph, a heading). */
12
+ const INLINE_TAGS = new Set([
13
+ 'span',
14
+ 'strong',
15
+ 'em',
16
+ 'small',
17
+ 'code',
18
+ 'kbd',
19
+ 'mark',
20
+ 'label',
21
+ 'time',
22
+ ]);
23
+ /**
24
+ * CSS `bolder`: the weight a `<strong>` draws at, from the weight it inherits.
25
+ *
26
+ * @param weight - The inherited weight.
27
+ * @returns The bolder weight.
28
+ */
29
+ function bolder(weight) {
30
+ if (weight < 350)
31
+ return '400';
32
+ if (weight < 550)
33
+ return '700';
34
+ return '900';
35
+ }
36
+ /**
37
+ * A `<strong>`'s weight, as a browser's stylesheet gives it (`font-weight: bolder`), unless its
38
+ * own classes set one.
39
+ *
40
+ * @param inherited - The text classes its container hands down.
41
+ * @param className - Its own classes.
42
+ * @returns Its style, or `undefined` when its classes set the weight.
43
+ */
44
+ function strongStyle(inherited, className) {
45
+ if (splitClasses(className, '').weight !== null)
46
+ return undefined;
47
+ return { fontWeight: bolder(splitClasses('', inherited).weight ?? 400) };
48
+ }
49
+ /**
50
+ * Text: a native `Text` (a heading for `h1`–`h6`) with the text classes its container hands down
51
+ * and the defaults a browser gives its tag (`strong` bolder, `code` monospace); it hands its text classes to the text nested in it.
52
+ *
53
+ * @param props - See {@link TextProps}.
54
+ * @returns The element.
55
+ */
56
+ export function Text(props) {
57
+ const { as, style, webStyle, children, ref } = props;
58
+ const className = useTagClasses(props.className, as);
59
+ const inherited = useInheritedText();
60
+ const split = splitClasses(className, inherited, dataAttributes(props));
61
+ const classes = textElementClasses(split);
62
+ const tagStyle = as === 'strong' ? strongStyle(inherited, className) : TEXT_TAG_STYLE[as];
63
+ const own = nativeStyle(style, webStyle);
64
+ const inheritedFontStyle = useInheritedFontStyle();
65
+ const fontStyle = own?.fontStyle ?? tagStyle?.fontStyle ?? inheritedFontStyle;
66
+ const activateLabel = useLabelPress(as === 'label' ? props.htmlFor : undefined, children);
67
+ useLabelText(props.id, children, props['aria-label']);
68
+ const accessibility = nativeAccessibility(props, as);
69
+ const target = useOverlayTarget(useNativeRef(ref), accessibility.role === 'heading' && !accessibility['aria-hidden'] && children != null
70
+ ? 'heading'
71
+ : undefined);
72
+ return (_jsx(NativeText, { ...accessibility, ref: target.ref, className: classes, numberOfLines: split.lines ?? undefined, ellipsizeMode: split.lines === null ? undefined : 'tail', onPress: activateLabel, style: [
73
+ inheritedFontStyle ? { ...tagStyle, fontStyle } : tagStyle,
74
+ own,
75
+ useLineStart(blockInInline(textFlow(props), props.children, true)),
76
+ useGridCell(),
77
+ ], children: _jsx(ElementScope, { split: split, fontStyle: fontStyle, children: children }) }));
78
+ }
79
+ /**
80
+ * How text flows: an inline tag (`span`, `strong`) is inline text, a paragraph or a heading a
81
+ * block, unless its classes say otherwise.
82
+ *
83
+ * @param props - Its props.
84
+ * @returns Its inline kind.
85
+ */
86
+ function textFlow(props) {
87
+ const { webStyle } = props;
88
+ if (typeof webStyle === 'object' &&
89
+ webStyle &&
90
+ 'display' in webStyle &&
91
+ webStyle.display === 'block') {
92
+ return null;
93
+ }
94
+ return flowOf(props.className, typeof props.as === 'string' && INLINE_TAGS.has(props.as) ? 'text' : null, dataAttributes(props));
95
+ }
96
+ registerInline(Text, textFlow);
@@ -0,0 +1,25 @@
1
+ /**
2
+ * TextField: a text input or text area.
3
+ *
4
+ * @module
5
+ */
6
+ import { type ReactElement } from 'react';
7
+ import type { TextFieldProps } from '@molecule/app-ui';
8
+ import type { ElementRefProps } from './types.js';
9
+ /**
10
+ * A text field: a native `TextInput`, multi-line for `textarea`. `onValueChange` receives the new
11
+ * text; `onChange` and `onInput` receive an event whose target's `value` is the new text.
12
+ *
13
+ * Each key the device's keyboard sends fires `onKeyDown`, `onKeyPress` and `onKeyUp` in that
14
+ * order, before the text changes; a handler that calls `preventDefault()` on the first two keeps
15
+ * the change from reaching `onChange`, `onInput` and `onValueChange`, as on the web. `onClick`
16
+ * fires on a press, `title` is the accessibility hint, and `tabIndex` decides whether the field
17
+ * takes keyboard focus. A device has no hover or double click on a text field
18
+ * (`onMouseEnter`, `onMouseLeave`, `onDoubleClick`) and no character columns or line-wrap modes
19
+ * (`cols`, `wrap`). Inside a form, Return in a single-line field can submit it.
20
+ *
21
+ * @param props - See {@link TextFieldProps}.
22
+ * @returns The element.
23
+ */
24
+ export declare function TextField(props: TextFieldProps & ElementRefProps): ReactElement;
25
+ //# sourceMappingURL=TextField.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TextField.d.ts","sourceRoot":"","sources":["../../src/elements/TextField.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,YAAY,EAA6C,MAAM,OAAO,CAAA;AAWpF,OAAO,KAAK,EAAoB,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAoBxE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAyIjD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,eAAe,GAAG,YAAY,CA4K/E"}
@@ -0,0 +1,257 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /**
3
+ * TextField: a text input or text area.
4
+ *
5
+ * @module
6
+ */
7
+ import { useCallback, useContext, useRef, useState } from 'react';
8
+ import { Platform, TextInput, useWindowDimensions, } from 'react-native';
9
+ import { nativeAccessibility } from './accessibility.js';
10
+ import { fieldEvent, focusEvent, keyboardEvent, mouseEvent } from './events.js';
11
+ import { validBoundedField } from './fieldBounds.js';
12
+ import { registerInline } from './flow.js';
13
+ import { DisabledScope, FormScope, useFieldValue, useFormRegistration } from './form.js';
14
+ import { atBreakpoint, useGridCell } from './grid.js';
15
+ import { dataAttributes, replacedFlow, splitClasses, textElementClasses, useInheritedText, useTagClasses, } from './inherited.js';
16
+ import { useLabelable } from './labels.js';
17
+ import { nativePasteProps, PasteScope } from './paste.js';
18
+ import { useNativeRef } from './ref.js';
19
+ import { nativeStyle } from './style.js';
20
+ /** The on-screen keyboard a web input type brings up. */
21
+ const INPUT_MODE = {
22
+ text: undefined,
23
+ password: undefined,
24
+ email: 'email',
25
+ search: 'search',
26
+ number: 'decimal',
27
+ tel: 'tel',
28
+ url: 'url',
29
+ // A device takes a date or a time as text in the web's value format; a picker is not drawn.
30
+ date: undefined,
31
+ time: undefined,
32
+ 'datetime-local': undefined,
33
+ };
34
+ /** The `autoComplete` values a device's text field takes (the web's, with the device's own names). */
35
+ const AUTO_COMPLETE = new Set([
36
+ 'additional-name',
37
+ 'address-line1',
38
+ 'address-line2',
39
+ 'cc-csc',
40
+ 'cc-exp',
41
+ 'cc-exp-month',
42
+ 'cc-exp-year',
43
+ 'cc-number',
44
+ 'cc-name',
45
+ 'cc-given-name',
46
+ 'cc-family-name',
47
+ 'cc-type',
48
+ 'country',
49
+ 'current-password',
50
+ 'email',
51
+ 'family-name',
52
+ 'given-name',
53
+ 'honorific-prefix',
54
+ 'honorific-suffix',
55
+ 'name',
56
+ 'new-password',
57
+ 'nickname',
58
+ 'one-time-code',
59
+ 'organization',
60
+ 'organization-title',
61
+ 'postal-code',
62
+ 'street-address',
63
+ 'tel',
64
+ 'tel-country-code',
65
+ 'tel-national',
66
+ 'url',
67
+ 'username',
68
+ 'off',
69
+ ]);
70
+ /**
71
+ * Whether a web `autoComplete` value is one a device takes.
72
+ *
73
+ * @param value - The web value.
74
+ * @returns Whether a device's text field takes it.
75
+ */
76
+ function isNativeAutoComplete(value) {
77
+ return value !== undefined && AUTO_COMPLETE.has(value);
78
+ }
79
+ /**
80
+ * A text area that grows with its text (`autoGrow`): the height is the text's height, at least
81
+ * `minRows` and at most `maxRows` lines at the current window width.
82
+ *
83
+ * @param autoGrow - The line counts, or `undefined` for a field that does not grow.
84
+ * @param lineHeights - The field's line heights in pixels by breakpoint.
85
+ * @returns The height style and the content-size handler that measures the text.
86
+ */
87
+ function useAutoGrow(autoGrow, lineHeights) {
88
+ const [content, setContent] = useState(0);
89
+ const { width } = useWindowDimensions();
90
+ if (!autoGrow)
91
+ return { style: undefined, onContentSizeChange: undefined };
92
+ // 16px on a 1.5 line is the web default when a ClassMap supplies no metrics.
93
+ const line = (lineHeights && atBreakpoint(lineHeights, width)) ?? 24;
94
+ const min = autoGrow.minRows * line;
95
+ const max = autoGrow.maxRows === undefined ? Infinity : autoGrow.maxRows * line;
96
+ return {
97
+ style: {
98
+ height: Math.min(Math.max(content, min), max),
99
+ minHeight: Math.min(min, max),
100
+ ...(autoGrow.maxRows === undefined ? {} : { maxHeight: max }),
101
+ },
102
+ onContentSizeChange: (event) => setContent(event.nativeEvent.contentSize.height),
103
+ };
104
+ }
105
+ /** The HTML email input grammar for a single address, including single-label domains. */
106
+ const EMAIL = /^[a-z0-9.!#$%&'*+/=?^`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i;
107
+ const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
108
+ const NETWORK_AUTHORITY = /^(?:https?|ftp|wss?):[/\\]*([^/?#\\]*)/i;
109
+ const HOST_AND_PORT = /^(?:\[[0-9a-f:.]+\]|[^\s<>^|:[\]]+)(?::(\d*))?$/i;
110
+ /** Checks an absolute URL, including the authority checks missing from older RN URL constructors. */
111
+ function validUrl(text) {
112
+ if (!SCHEME.test(text))
113
+ return false;
114
+ const authority = NETWORK_AUTHORITY.exec(text)?.[1];
115
+ if (authority !== undefined) {
116
+ const host = HOST_AND_PORT.exec(authority.slice(authority.lastIndexOf('@') + 1));
117
+ if (!host || Number(host[1] ?? 0) > 65535)
118
+ return false;
119
+ }
120
+ try {
121
+ // A fragment cannot supply the scheme or host. Older React Native URL constructors
122
+ // assume a URL with a fragment always has a network host.
123
+ new URL(text.split('#', 1)[0]);
124
+ return true;
125
+ }
126
+ catch (_error) {
127
+ // The URL constructor throws for text that is not a URL; that is the invalid answer, so the
128
+ // error carries nothing to report.
129
+ return false;
130
+ }
131
+ }
132
+ /** Removes the characters the browser strips from single-line input values. */
133
+ function formText(text, singleLine, type) {
134
+ if (!singleLine)
135
+ return text;
136
+ const line = text.replace(/[\r\n]/g, '');
137
+ return type === 'url' || type === 'email' ? line.replace(/^[\t\f ]+|[\t\f ]+$/g, '') : line;
138
+ }
139
+ /**
140
+ * A text field: a native `TextInput`, multi-line for `textarea`. `onValueChange` receives the new
141
+ * text; `onChange` and `onInput` receive an event whose target's `value` is the new text.
142
+ *
143
+ * Each key the device's keyboard sends fires `onKeyDown`, `onKeyPress` and `onKeyUp` in that
144
+ * order, before the text changes; a handler that calls `preventDefault()` on the first two keeps
145
+ * the change from reaching `onChange`, `onInput` and `onValueChange`, as on the web. `onClick`
146
+ * fires on a press, `title` is the accessibility hint, and `tabIndex` decides whether the field
147
+ * takes keyboard focus. A device has no hover or double click on a text field
148
+ * (`onMouseEnter`, `onMouseLeave`, `onDoubleClick`) and no character columns or line-wrap modes
149
+ * (`cols`, `wrap`). Inside a form, Return in a single-line field can submit it.
150
+ *
151
+ * @param props - See {@link TextFieldProps}.
152
+ * @returns The element.
153
+ */
154
+ export function TextField(props) {
155
+ const { as, type, inputMode, value, defaultValue, placeholder, readOnly, name, required, maxLength, rows, autoGrow, autoFocus, autoComplete, spellCheck, autoCorrect, enterKeyHint, selectTextOnFocus, onPasteText, title, tabIndex, className, style, webStyle, onChange, onValueChange, onInput, onFocus, onBlur, onClick, onKeyDown, onKeyPress, onKeyUp, ref, } = props;
156
+ const form = useContext(FormScope);
157
+ const parentPaste = useContext(PasteScope);
158
+ const fieldsetDisabled = useContext(DisabledScope);
159
+ const disabled = props.disabled || fieldsetDisabled;
160
+ const singleLine = as === 'input';
161
+ const split = splitClasses(useTagClasses(className, props.as), useInheritedText(), dataAttributes(props));
162
+ const grow = useAutoGrow(as === 'textarea' ? autoGrow : undefined, split.lineHeight);
163
+ const field = useFieldValue(value, defaultValue ?? '');
164
+ const read = field.read;
165
+ const node = useRef(null);
166
+ const forward = useNativeRef(ref);
167
+ const setNode = useCallback((instance) => {
168
+ node.current = instance;
169
+ if (instance && forward) {
170
+ // Keep the original host for the kit's measurement and focus operations.
171
+ Object.assign(instance, {
172
+ select() {
173
+ if (node.current === instance)
174
+ instance.setSelection(0, read().length);
175
+ },
176
+ });
177
+ }
178
+ forward?.(instance);
179
+ }, [forward, read]);
180
+ const focus = () => {
181
+ if (!disabled)
182
+ node.current?.focus();
183
+ };
184
+ const label = useLabelable(props.id, focus);
185
+ const readEntry = () => formText(field.read(), singleLine, type);
186
+ useFormRegistration(form?.fields, {
187
+ entries: () => (name && !disabled ? [[name, readEntry()]] : []),
188
+ valid: () => {
189
+ if (disabled || readOnly)
190
+ return true;
191
+ const text = readEntry();
192
+ if (!text)
193
+ return !required;
194
+ if (singleLine && type === 'email')
195
+ return EMAIL.test(text);
196
+ if (singleLine && type === 'url')
197
+ return validUrl(text);
198
+ return !singleLine || validBoundedField(props, text);
199
+ },
200
+ focus,
201
+ reset: field.reset,
202
+ singleLine,
203
+ });
204
+ // A key whose handler prevented its default never reaches the text handlers (the web inserts
205
+ // nothing, so no input or change event fires).
206
+ const prevented = useRef(false);
207
+ const hasKeys = onKeyDown || onKeyPress || onKeyUp;
208
+ const handleKey = hasKeys
209
+ ? (event) => {
210
+ const { key } = event.nativeEvent;
211
+ const down = keyboardEvent(key);
212
+ onKeyDown?.(down);
213
+ const press = keyboardEvent(key);
214
+ onKeyPress?.(press);
215
+ onKeyUp?.(keyboardEvent(key));
216
+ prevented.current = down.defaultPrevented || press.defaultPrevented;
217
+ // The key's own text change arrives in the same turn; a key that changes nothing (Enter
218
+ // in a one-line field) must not swallow the next change (a paste).
219
+ if (prevented.current)
220
+ setTimeout(() => {
221
+ prevented.current = false;
222
+ }, 0);
223
+ }
224
+ : undefined;
225
+ const handleText = (text) => {
226
+ if (disabled || readOnly)
227
+ return;
228
+ if (prevented.current) {
229
+ prevented.current = false;
230
+ return;
231
+ }
232
+ field.change(text, () => {
233
+ onInput?.(fieldEvent({ value: text, name, type }));
234
+ onChange?.(fieldEvent({ value: text, name, type }));
235
+ onValueChange?.(text);
236
+ });
237
+ };
238
+ // NativeWind reads the disabled prop; the device's TextInput reads editable.
239
+ const nativeState = { disabled: disabled || undefined, editable: !readOnly && !disabled };
240
+ const submitOnReturn = form && singleLine && !disabled;
241
+ return (_jsx(TextInput, { ...nativeAccessibility({
242
+ ...props,
243
+ 'aria-label': props['aria-label'] ?? label,
244
+ 'aria-disabled': disabled || props['aria-disabled'],
245
+ }), ref: setNode, className: textElementClasses(split), style: [nativeStyle(style, webStyle), useGridCell(), grow.style], multiline: as === 'textarea', numberOfLines: as === 'textarea' ? rows : undefined, secureTextEntry: type === 'password', inputMode: inputMode ?? (type ? INPUT_MODE[type] : undefined), value: field.displayed, placeholder: placeholder, ...nativeState, ...nativePasteProps(nativeState.editable ? (onPasteText ?? parentPaste?.handlePaste) : undefined), maxLength: maxLength, autoFocus: disabled ? false : autoFocus, autoComplete: isNativeAutoComplete(autoComplete) ? autoComplete : undefined, spellCheck: Platform.OS === 'ios' ? spellCheck : undefined, autoCorrect: autoCorrect === undefined ? undefined : autoCorrect === 'on', enterKeyHint: (enterKeyHint === 'previous' && Platform.OS !== 'android') ||
246
+ (enterKeyHint === 'enter' && Platform.OS !== 'ios')
247
+ ? undefined
248
+ : enterKeyHint, selectTextOnFocus: selectTextOnFocus, accessibilityHint: title, tabIndex: tabIndex === undefined ? undefined : tabIndex >= 0 ? 0 : -1, onContentSizeChange: grow.onContentSizeChange, onChangeText: handleText, onKeyPress: handleKey, submitBehavior: submitOnReturn ? 'submit' : undefined, onSubmitEditing: submitOnReturn
249
+ ? () => {
250
+ if (!prevented.current)
251
+ form.submitImplicitly();
252
+ }
253
+ : undefined, onPress: onClick ? (event) => onClick(mouseEvent(event)) : undefined, onFocus: onFocus ? () => onFocus(focusEvent()) : undefined, onBlur: onBlur ? () => onBlur(focusEvent()) : undefined }));
254
+ }
255
+ // A field is an inline box, as `input` and `textarea` are on the web, unless a display class makes
256
+ // it a block (`flex`, `block`): then it takes a line of its own, with no line box around it.
257
+ registerInline(TextField, (props) => replacedFlow(props.className, String(props.as ?? 'input')));
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Maps an element's web accessibility props to React Native's.
3
+ *
4
+ * @module
5
+ */
6
+ import { type ReactNode } from 'react';
7
+ import { type ViewProps } from 'react-native';
8
+ import type { AriaAttributes, DataAttributes, ElementRole } from '@molecule/app-ui';
9
+ /** The accessibility and identity props every native element receives. */
10
+ export type NativeAccessibility = Pick<ViewProps, 'role' | 'id' | 'testID' | 'aria-label' | 'aria-labelledby' | 'aria-hidden' | 'aria-disabled' | 'aria-expanded' | 'aria-selected' | 'aria-checked' | 'aria-busy' | 'aria-live' | 'aria-modal' | 'aria-valuemax' | 'aria-valuemin' | 'aria-valuenow' | 'aria-valuetext' | 'aria-level' | 'accessibilityHint'> & {
11
+ accessibilityCollectionItem?: {
12
+ rowIndex: number;
13
+ columnIndex: number;
14
+ rowSpan: number;
15
+ columnSpan: number;
16
+ heading: boolean;
17
+ };
18
+ };
19
+ /** The element props {@link nativeAccessibility} reads. */
20
+ export type AccessibilitySource = AriaAttributes & DataAttributes & {
21
+ id?: string;
22
+ role?: ElementRole;
23
+ children?: ReactNode;
24
+ };
25
+ /**
26
+ * React Native's accessibility props for an element: its role (explicit, or the one its tag
27
+ * implies), the ARIA attributes React Native applies (`aria-pressed` and `aria-current` read as
28
+ * selected), its id, and `data-mol-id` (else `data-testid`) as its `testID`. Other `data-*` attributes and ARIA
29
+ * attributes React Native has no equivalent for are left out.
30
+ *
31
+ * @param props - The element's props.
32
+ * @param tag - The element's web tag.
33
+ * @returns The native props.
34
+ */
35
+ export declare function nativeAccessibility(props: AccessibilitySource, tag?: string): NativeAccessibility;
36
+ //# sourceMappingURL=accessibility.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accessibility.d.ts","sourceRoot":"","sources":["../../src/elements/accessibility.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAkB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AACtD,OAAO,EAAY,KAAK,SAAS,EAAE,MAAM,cAAc,CAAA;AAGvD,OAAO,KAAK,EAAE,cAAc,EAAe,cAAc,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AA2HhG,0EAA0E;AAC1E,MAAM,MAAM,mBAAmB,GAAG,IAAI,CACpC,SAAS,EACP,MAAM,GACN,IAAI,GACJ,QAAQ,GACR,YAAY,GACZ,iBAAiB,GACjB,aAAa,GACb,eAAe,GACf,eAAe,GACf,eAAe,GACf,cAAc,GACd,WAAW,GACX,WAAW,GACX,YAAY,GACZ,eAAe,GACf,eAAe,GACf,eAAe,GACf,gBAAgB,GAChB,YAAY,GACZ,mBAAmB,CACtB,GAAG;IACF,2BAA2B,CAAC,EAAE;QAC5B,QAAQ,EAAE,MAAM,CAAA;QAChB,WAAW,EAAE,MAAM,CAAA;QACnB,OAAO,EAAE,MAAM,CAAA;QACf,UAAU,EAAE,MAAM,CAAA;QAClB,OAAO,EAAE,OAAO,CAAA;KACjB,CAAA;CACF,CAAA;AAYD,2DAA2D;AAC3D,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAC9C,cAAc,GAAG;IAAE,EAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAE,CAAA;AA8F5E;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,mBAAmB,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,mBAAmB,CA+BjG"}