@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.
- package/README.md +211 -331
- package/dist/components/Button.d.ts +2 -2
- package/dist/components/Button.d.ts.map +1 -1
- package/dist/components/Button.js +2 -2
- package/dist/components/Card.d.ts +3 -2
- package/dist/components/Card.d.ts.map +1 -1
- package/dist/components/Card.js +11 -3
- package/dist/components/Checkbox.d.ts +2 -2
- package/dist/components/Checkbox.d.ts.map +1 -1
- package/dist/components/Checkbox.js +4 -7
- package/dist/components/Input.d.ts +13 -2
- package/dist/components/Input.d.ts.map +1 -1
- package/dist/components/Input.js +5 -11
- package/dist/components/Layout.d.ts +3 -2
- package/dist/components/Layout.d.ts.map +1 -1
- package/dist/components/Layout.js +2 -2
- package/dist/components/Select.d.ts +2 -2
- package/dist/components/Select.d.ts.map +1 -1
- package/dist/components/Select.js +4 -6
- package/dist/components/Switch.d.ts +2 -2
- package/dist/components/Switch.d.ts.map +1 -1
- package/dist/components/Switch.js +3 -7
- package/dist/components/Textarea.d.ts +13 -2
- package/dist/components/Textarea.d.ts.map +1 -1
- package/dist/components/Textarea.js +2 -6
- package/dist/elements/Anchor.d.ts +15 -0
- package/dist/elements/Anchor.d.ts.map +1 -0
- package/dist/elements/Anchor.js +57 -0
- package/dist/elements/Box.d.ts +19 -0
- package/dist/elements/Box.d.ts.map +1 -0
- package/dist/elements/Box.js +150 -0
- package/dist/elements/CheckField.d.ts +18 -0
- package/dist/elements/CheckField.d.ts.map +1 -0
- package/dist/elements/CheckField.js +103 -0
- package/dist/elements/Disclosure.d.ts +11 -0
- package/dist/elements/Disclosure.d.ts.map +1 -0
- package/dist/elements/Disclosure.js +67 -0
- package/dist/elements/Fieldset.d.ts +16 -0
- package/dist/elements/Fieldset.d.ts.map +1 -0
- package/dist/elements/Fieldset.js +34 -0
- package/dist/elements/FileField.d.ts +12 -0
- package/dist/elements/FileField.d.ts.map +1 -0
- package/dist/elements/FileField.js +111 -0
- package/dist/elements/FormArea.d.ts +18 -0
- package/dist/elements/FormArea.d.ts.map +1 -0
- package/dist/elements/FormArea.js +42 -0
- package/dist/elements/GlyphIcon.d.ts +19 -0
- package/dist/elements/GlyphIcon.d.ts.map +1 -0
- package/dist/elements/GlyphIcon.js +43 -0
- package/dist/elements/Image.d.ts +17 -0
- package/dist/elements/Image.d.ts.map +1 -0
- package/dist/elements/Image.js +26 -0
- package/dist/elements/Link.d.ts +20 -0
- package/dist/elements/Link.d.ts.map +1 -0
- package/dist/elements/Link.js +53 -0
- package/dist/elements/Pressable.d.ts +18 -0
- package/dist/elements/Pressable.d.ts.map +1 -0
- package/dist/elements/Pressable.js +107 -0
- package/dist/elements/PulseDots.d.ts +15 -0
- package/dist/elements/PulseDots.d.ts.map +1 -0
- package/dist/elements/PulseDots.js +78 -0
- package/dist/elements/RangeField.d.ts +11 -0
- package/dist/elements/RangeField.d.ts.map +1 -0
- package/dist/elements/RangeField.js +259 -0
- package/dist/elements/RichText.d.ts +23 -0
- package/dist/elements/RichText.d.ts.map +1 -0
- package/dist/elements/RichText.js +55 -0
- package/dist/elements/Rule.d.ts +11 -0
- package/dist/elements/Rule.d.ts.map +1 -0
- package/dist/elements/Rule.js +19 -0
- package/dist/elements/ScrollArea.d.ts +21 -0
- package/dist/elements/ScrollArea.d.ts.map +1 -0
- package/dist/elements/ScrollArea.js +101 -0
- package/dist/elements/SelectAction.d.ts +20 -0
- package/dist/elements/SelectAction.d.ts.map +1 -0
- package/dist/elements/SelectAction.js +113 -0
- package/dist/elements/SelectField.d.ts +11 -0
- package/dist/elements/SelectField.d.ts.map +1 -0
- package/dist/elements/SelectField.js +220 -0
- package/dist/elements/SelectMenu.d.ts +19 -0
- package/dist/elements/SelectMenu.d.ts.map +1 -0
- package/dist/elements/SelectMenu.js +122 -0
- package/dist/elements/Svg.d.ts +155 -0
- package/dist/elements/Svg.d.ts.map +1 -0
- package/dist/elements/Svg.js +525 -0
- package/dist/elements/TableBox.d.ts +19 -0
- package/dist/elements/TableBox.d.ts.map +1 -0
- package/dist/elements/TableBox.js +87 -0
- package/dist/elements/TableCell.d.ts +16 -0
- package/dist/elements/TableCell.d.ts.map +1 -0
- package/dist/elements/TableCell.js +69 -0
- package/dist/elements/Text.d.ts +17 -0
- package/dist/elements/Text.d.ts.map +1 -0
- package/dist/elements/Text.js +96 -0
- package/dist/elements/TextField.d.ts +25 -0
- package/dist/elements/TextField.d.ts.map +1 -0
- package/dist/elements/TextField.js +257 -0
- package/dist/elements/accessibility.d.ts +36 -0
- package/dist/elements/accessibility.d.ts.map +1 -0
- package/dist/elements/accessibility.js +239 -0
- package/dist/elements/className.d.ts +45 -0
- package/dist/elements/className.d.ts.map +1 -0
- package/dist/elements/className.js +8 -0
- package/dist/elements/decimal.d.ts +5 -0
- package/dist/elements/decimal.d.ts.map +1 -0
- package/dist/elements/decimal.js +11 -0
- package/dist/elements/documentUrl.d.ts +35 -0
- package/dist/elements/documentUrl.d.ts.map +1 -0
- package/dist/elements/documentUrl.js +89 -0
- package/dist/elements/drag-drop.d.ts +11 -0
- package/dist/elements/drag-drop.d.ts.map +1 -0
- package/dist/elements/drag-drop.js +277 -0
- package/dist/elements/drag.d.ts +47 -0
- package/dist/elements/drag.d.ts.map +1 -0
- package/dist/elements/drag.js +362 -0
- package/dist/elements/events.d.ts +45 -0
- package/dist/elements/events.d.ts.map +1 -0
- package/dist/elements/events.js +101 -0
- package/dist/elements/fieldBounds.d.ts +5 -0
- package/dist/elements/fieldBounds.d.ts.map +1 -0
- package/dist/elements/fieldBounds.js +60 -0
- package/dist/elements/flow.d.ts +36 -0
- package/dist/elements/flow.d.ts.map +1 -0
- package/dist/elements/flow.js +47 -0
- package/dist/elements/form.d.ts +48 -0
- package/dist/elements/form.d.ts.map +1 -0
- package/dist/elements/form.js +112 -0
- package/dist/elements/grid.d.ts +96 -0
- package/dist/elements/grid.d.ts.map +1 -0
- package/dist/elements/grid.js +272 -0
- package/dist/elements/index.d.ts +36 -0
- package/dist/elements/index.d.ts.map +1 -0
- package/dist/elements/index.js +35 -0
- package/dist/elements/inherited.d.ts +181 -0
- package/dist/elements/inherited.d.ts.map +1 -0
- package/dist/elements/inherited.js +512 -0
- package/dist/elements/labels.d.ts +39 -0
- package/dist/elements/labels.d.ts.map +1 -0
- package/dist/elements/labels.js +163 -0
- package/dist/elements/measure.d.ts +27 -0
- package/dist/elements/measure.d.ts.map +1 -0
- package/dist/elements/measure.js +24 -0
- package/dist/elements/outsidePress.d.ts +31 -0
- package/dist/elements/outsidePress.d.ts.map +1 -0
- package/dist/elements/outsidePress.js +94 -0
- package/dist/elements/paste.d.ts +22 -0
- package/dist/elements/paste.d.ts.map +1 -0
- package/dist/elements/paste.js +49 -0
- package/dist/elements/press.d.ts +41 -0
- package/dist/elements/press.d.ts.map +1 -0
- package/dist/elements/press.js +131 -0
- package/dist/elements/rangeKeyboard.d.ts +41 -0
- package/dist/elements/rangeKeyboard.d.ts.map +1 -0
- package/dist/elements/rangeKeyboard.js +58 -0
- package/dist/elements/rangeValues.d.ts +20 -0
- package/dist/elements/rangeValues.d.ts.map +1 -0
- package/dist/elements/rangeValues.js +101 -0
- package/dist/elements/ref.d.ts +16 -0
- package/dist/elements/ref.d.ts.map +1 -0
- package/dist/elements/ref.js +22 -0
- package/dist/elements/reveal.d.ts +8 -0
- package/dist/elements/reveal.d.ts.map +1 -0
- package/dist/elements/reveal.js +10 -0
- package/dist/elements/scrollReveal.d.ts +19 -0
- package/dist/elements/scrollReveal.d.ts.map +1 -0
- package/dist/elements/scrollReveal.js +52 -0
- package/dist/elements/selectOptions.d.ts +25 -0
- package/dist/elements/selectOptions.d.ts.map +1 -0
- package/dist/elements/selectOptions.js +60 -0
- package/dist/elements/style.d.ts +30 -0
- package/dist/elements/style.d.ts.map +1 -0
- package/dist/elements/style.js +363 -0
- package/dist/elements/svgHover.d.ts +41 -0
- package/dist/elements/svgHover.d.ts.map +1 -0
- package/dist/elements/svgHover.js +152 -0
- package/dist/elements/table.d.ts +52 -0
- package/dist/elements/table.d.ts.map +1 -0
- package/dist/elements/table.js +104 -0
- package/dist/elements/types.d.ts +25 -0
- package/dist/elements/types.d.ts.map +1 -0
- package/dist/elements/types.js +8 -0
- package/dist/elements/viewport.d.ts +53 -0
- package/dist/elements/viewport.d.ts.map +1 -0
- package/dist/elements/viewport.js +62 -0
- package/dist/elements/wheelZoom.d.ts +11 -0
- package/dist/elements/wheelZoom.d.ts.map +1 -0
- package/dist/elements/wheelZoom.js +7 -0
- package/dist/index.d.ts +11 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/platform/focus.d.ts +31 -0
- package/dist/platform/focus.d.ts.map +1 -0
- package/dist/platform/focus.js +86 -0
- package/dist/platform/index.d.ts +31 -0
- package/dist/platform/index.d.ts.map +1 -0
- package/dist/platform/index.js +179 -0
- package/dist/platform/layout.d.ts +13 -0
- package/dist/platform/layout.d.ts.map +1 -0
- package/dist/platform/layout.js +15 -0
- package/dist/platform/position.d.ts +5 -0
- package/dist/platform/position.d.ts.map +1 -0
- package/dist/platform/position.js +177 -0
- package/dist/platform/stack.d.ts +86 -0
- package/dist/platform/stack.d.ts.map +1 -0
- package/dist/platform/stack.js +318 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +66 -8
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* What the CSS cascade does on the web, for native elements: text classes a container hands to its
|
|
4
|
+
* text, and the line boxes that lay out inline children side by side.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { Children, cloneElement, createContext, Fragment, isValidElement, useContext, useMemo, } from 'react';
|
|
9
|
+
import { Text as NativeText, View } from 'react-native';
|
|
10
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
11
|
+
import { displaysContents, isRegistered, registeredKind } from './flow.js';
|
|
12
|
+
import { NoGridCell } from './grid.js';
|
|
13
|
+
/** The text classes the nearest container hands down (CSS inheritance); none above the first. */
|
|
14
|
+
const InheritedText = createContext(null);
|
|
15
|
+
/** A container's inherited inline font style, resolved before reaching native text. */
|
|
16
|
+
const InheritedFontStyle = createContext(undefined);
|
|
17
|
+
/** Reads the font style the nearest address block or text run supplies. */
|
|
18
|
+
export function useInheritedFontStyle() {
|
|
19
|
+
return useContext(InheritedFontStyle);
|
|
20
|
+
}
|
|
21
|
+
/** Anonymous native text still inherits the address font style from its containing view. */
|
|
22
|
+
function InheritedTextRun({ style, ...props }) {
|
|
23
|
+
const fontStyle = useInheritedFontStyle();
|
|
24
|
+
return _jsx(NativeText, { ...props, style: fontStyle ? [{ fontStyle }, style] : style });
|
|
25
|
+
}
|
|
26
|
+
/** Descendant classes by tag ({@link NativeClassSplit.descendants}) that no container gives. */
|
|
27
|
+
const NO_RULES = Object.freeze({});
|
|
28
|
+
/**
|
|
29
|
+
* The classes the containers around an element give their descendants, by tag (a CSS descendant
|
|
30
|
+
* selector: `[&_p]:leading-relaxed` on a container reaches every `p` inside it).
|
|
31
|
+
*/
|
|
32
|
+
const DescendantRules = createContext(NO_RULES);
|
|
33
|
+
/**
|
|
34
|
+
* An element's classes with the ones its containers give descendants with its tag after its own,
|
|
35
|
+
* as a descendant selector applies to the element on the web.
|
|
36
|
+
*
|
|
37
|
+
* @param className - The element's own classes.
|
|
38
|
+
* @param tag - Its web tag.
|
|
39
|
+
* @param position - Whether the element starts or ends its sibling list.
|
|
40
|
+
* @returns The classes it applies.
|
|
41
|
+
*/
|
|
42
|
+
export function useTagClasses(className, tag, position) {
|
|
43
|
+
const rules = useContext(DescendantRules);
|
|
44
|
+
return ([
|
|
45
|
+
className,
|
|
46
|
+
rules[tag],
|
|
47
|
+
position?.first && rules[`${tag}:first-child`],
|
|
48
|
+
position?.last && rules[`${tag}:last-child`],
|
|
49
|
+
]
|
|
50
|
+
.filter(Boolean)
|
|
51
|
+
.join(' ') || undefined);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The descendant classes after an element's own are added to its containers' ones.
|
|
55
|
+
*
|
|
56
|
+
* @param above - Its containers' ones.
|
|
57
|
+
* @param own - Its own ({@link NativeClassSplit.descendants}).
|
|
58
|
+
* @returns Both, by tag.
|
|
59
|
+
*/
|
|
60
|
+
function withRules(above, own) {
|
|
61
|
+
const tags = own ? Object.keys(own) : [];
|
|
62
|
+
if (!own || tags.length === 0)
|
|
63
|
+
return above;
|
|
64
|
+
const out = { ...above };
|
|
65
|
+
for (const tag of tags)
|
|
66
|
+
out[tag] = [above[tag], own[tag]].filter(Boolean).join(' ');
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Hands an element's text classes and descendant classes to the elements inside it, as the CSS
|
|
71
|
+
* cascade does.
|
|
72
|
+
*
|
|
73
|
+
* @param props - The element's split classes (`text` and `descendants`), its text classes when
|
|
74
|
+
* they differ from the split's (a button's), and the children.
|
|
75
|
+
* @returns The children inside both.
|
|
76
|
+
*/
|
|
77
|
+
export function ElementScope({ split, text = split.text, fontStyle, children, }) {
|
|
78
|
+
const above = useContext(DescendantRules);
|
|
79
|
+
const rules = useMemo(() => withRules(above, split.descendants), [above, split.descendants]);
|
|
80
|
+
const inheritedFontStyle = useInheritedFontStyle();
|
|
81
|
+
return (_jsx(InheritedText.Provider, { value: text, children: _jsx(DescendantRules.Provider, { value: rules, children: _jsx(InheritedFontStyle.Provider, { value: fontStyle ?? inheritedFontStyle, children: _jsx(NoGridCell, { children: children }) }) }) }));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Whether a block container laid the nearest element out as a block of its own: `true` for each
|
|
85
|
+
* of a block container's children that does not sit in a line, `false` inside a line or a flex
|
|
86
|
+
* container, `null` outside every container.
|
|
87
|
+
*/
|
|
88
|
+
const BlockSlot = createContext(null);
|
|
89
|
+
/** A lone inline box at the start of its line: as wide as its content. */
|
|
90
|
+
const AT_LINE_START = { alignSelf: 'flex-start' };
|
|
91
|
+
/**
|
|
92
|
+
* The style an inline element needs when a block container laid it out as a block, which happens
|
|
93
|
+
* when the component rendering it never said how it flows (`registerInline`): a web line would
|
|
94
|
+
* make it as wide as its content, a native column stretches it across the container.
|
|
95
|
+
*
|
|
96
|
+
* @param kind - How the element flows by its own tag and classes.
|
|
97
|
+
* @returns `{ alignSelf: 'flex-start' }` for an inline element in a block slot, else `undefined`.
|
|
98
|
+
*/
|
|
99
|
+
export function useLineStart(kind) {
|
|
100
|
+
return useContext(BlockSlot) === true && kind !== null ? AT_LINE_START : undefined;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The text classes the nearest container hands down; outside every container, the ones the web
|
|
104
|
+
* gives the document (the ClassMap's `nativeRootText`).
|
|
105
|
+
*
|
|
106
|
+
* @returns The inherited text classes.
|
|
107
|
+
*/
|
|
108
|
+
export function useInheritedText() {
|
|
109
|
+
return useContext(InheritedText) ?? getClassMap().nativeRootText ?? '';
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Splits an element's classes for a native view through the ClassMap (`splitForNative`); a
|
|
113
|
+
* ClassMap without it keeps every class on the view and hands the inherited text classes on.
|
|
114
|
+
*
|
|
115
|
+
* @param className - The element's classes.
|
|
116
|
+
* @param inheritedText - The text classes in effect for its container's text.
|
|
117
|
+
* @returns The view classes, the text classes in effect for its own text, and its display.
|
|
118
|
+
*/
|
|
119
|
+
export function splitClasses(className, inheritedText = '', data) {
|
|
120
|
+
const cm = getClassMap();
|
|
121
|
+
return (cm.splitForNative?.(className ?? '', inheritedText, data) ?? {
|
|
122
|
+
view: className ?? '',
|
|
123
|
+
text: inheritedText,
|
|
124
|
+
display: null,
|
|
125
|
+
lines: null,
|
|
126
|
+
weight: null,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The text classes a native button hands down before its own: the inherited ones under what a
|
|
131
|
+
* browser's stylesheet gives a `<button>` (the ClassMap's `nativeButtonText`).
|
|
132
|
+
*
|
|
133
|
+
* @param inherited - The text classes its container hands down.
|
|
134
|
+
* @returns The text classes in effect beneath the button's own.
|
|
135
|
+
*/
|
|
136
|
+
export function buttonText(inherited) {
|
|
137
|
+
const defaults = getClassMap().nativeButtonText;
|
|
138
|
+
return defaults ? splitClasses(defaults, inherited).text : inherited;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The classes of a native element that draws text itself (a `Text`, a `TextInput`): the text
|
|
142
|
+
* classes in effect for it, then its own other classes.
|
|
143
|
+
*
|
|
144
|
+
* @param split - Its split classes.
|
|
145
|
+
* @returns The class string.
|
|
146
|
+
*/
|
|
147
|
+
export function textElementClasses(split) {
|
|
148
|
+
return [split.text, split.view].filter(Boolean).join(' ') || undefined;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The inline kind of a child.
|
|
152
|
+
*
|
|
153
|
+
* @param child - A child node.
|
|
154
|
+
* @returns Its kind, or `null` for a block.
|
|
155
|
+
*/
|
|
156
|
+
function inlineKind(child) {
|
|
157
|
+
if (typeof child === 'string' || typeof child === 'number')
|
|
158
|
+
return 'text';
|
|
159
|
+
if (!isValidElement(child))
|
|
160
|
+
return null;
|
|
161
|
+
if (displaysContents(child.type, child.props))
|
|
162
|
+
return inlineKind(child.props.children);
|
|
163
|
+
const kind = registeredKind(child.type, child.props) ?? null;
|
|
164
|
+
// Text-level content is inline; an element drawn as a box says itself whether it is inline
|
|
165
|
+
// (its flow applies blockInInline) or an inline box.
|
|
166
|
+
return kind === 'text' ? blockInInline(kind, child.props.children, true) : kind;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* How an element flows once the blocks among its children are counted: the web splits an inline
|
|
170
|
+
* element around a block inside it (block-in-inline), so the block stacks, the inline element
|
|
171
|
+
* puts no line of its own around it, and its box spans the block's width. An inline box
|
|
172
|
+
* (`inline-block`, a button) holds its blocks inside it and stays on the line.
|
|
173
|
+
*
|
|
174
|
+
* @param kind - How the element flows by its own tag and classes.
|
|
175
|
+
* @param children - Its children.
|
|
176
|
+
* @param inline - Whether the element is inline on the web ({@link isCssInline}).
|
|
177
|
+
* @returns `null` for an inline element holding a block, else `kind`.
|
|
178
|
+
*/
|
|
179
|
+
export function blockInInline(kind, children, inline) {
|
|
180
|
+
return kind !== null && inline && containsBlock(children) ? null : kind;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Whether an element is inline on the web, rather than an inline box or a block: its display
|
|
184
|
+
* class is `inline`, or it has none and its tag is inline (`span`, `a`). A button is an inline
|
|
185
|
+
* box by the browser's own stylesheet.
|
|
186
|
+
*
|
|
187
|
+
* @param className - Its classes.
|
|
188
|
+
* @param inlineTag - Whether its tag is inline.
|
|
189
|
+
* @param data - Its data attributes.
|
|
190
|
+
* @returns Whether it is inline.
|
|
191
|
+
*/
|
|
192
|
+
export function isCssInline(className, inlineTag, data) {
|
|
193
|
+
const { display } = splitClasses(typeof className === 'string' ? className : undefined, '', data);
|
|
194
|
+
return display === 'inline' || (display === null && inlineTag);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Whether children hold a block: an element that is not inline.
|
|
198
|
+
*
|
|
199
|
+
* @param children - The children.
|
|
200
|
+
* @returns Whether one of them is a block.
|
|
201
|
+
*/
|
|
202
|
+
function containsBlock(children) {
|
|
203
|
+
if (children === undefined || children === null || typeof children !== 'object')
|
|
204
|
+
return false;
|
|
205
|
+
return flatten(children).some((child) => isValidElement(child) && inlineKind(child) === null);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* How a replaced element (an image, a drawing) flows: a block when the web's stylesheet makes its
|
|
209
|
+
* tag one (the ClassMap's `nativeBlockTags`: Tailwind's reset makes `svg` and `img` blocks),
|
|
210
|
+
* otherwise an inline box, unless a display class says otherwise.
|
|
211
|
+
*
|
|
212
|
+
* @param className - Its classes.
|
|
213
|
+
* @param tag - Its web tag.
|
|
214
|
+
* @returns Its inline kind.
|
|
215
|
+
*/
|
|
216
|
+
export function replacedFlow(className, tag) {
|
|
217
|
+
const { display } = splitClasses(typeof className === 'string' ? className : undefined);
|
|
218
|
+
if (isBlockDisplay(display))
|
|
219
|
+
return null;
|
|
220
|
+
if (display === 'inline' || isInlineBoxDisplay(display))
|
|
221
|
+
return 'box';
|
|
222
|
+
return getClassMap().nativeBlockTags?.includes(tag) ? null : 'box';
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* A container's children with fragments and boxless fieldsets opened. Each fieldset child
|
|
226
|
+
* keeps its own copy of the provider for inherited classes and disabled state.
|
|
227
|
+
*
|
|
228
|
+
* @param children - The children.
|
|
229
|
+
* @param prefix - The key of the fragment they came from.
|
|
230
|
+
* @returns The children, keyed.
|
|
231
|
+
*/
|
|
232
|
+
export function flatten(children, prefix = '') {
|
|
233
|
+
const out = [];
|
|
234
|
+
for (const child of Children.toArray(children)) {
|
|
235
|
+
if (isValidElement(child)) {
|
|
236
|
+
const key = `${prefix}${String(child.key)}`;
|
|
237
|
+
if (child.type === Fragment) {
|
|
238
|
+
out.push(...flatten(child.props.children, `${key}/`));
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (displaysContents(child.type, child.props)) {
|
|
242
|
+
for (const [index, item] of flatten(child.props.children).entries()) {
|
|
243
|
+
const itemKey = isValidElement(item) ? item.key : index;
|
|
244
|
+
out.push(cloneElement(child, { key: `${key}/${String(itemKey)}` }, item));
|
|
245
|
+
}
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
out.push(prefix ? cloneElement(child, { key }) : child);
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
out.push(child);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* A component's name, for a message.
|
|
258
|
+
*
|
|
259
|
+
* @param type - The element type.
|
|
260
|
+
* @returns Its display name or function name.
|
|
261
|
+
*/
|
|
262
|
+
function componentName(type) {
|
|
263
|
+
if (typeof type === 'function' || (typeof type === 'object' && type !== null)) {
|
|
264
|
+
if ('displayName' in type && typeof type.displayName === 'string')
|
|
265
|
+
return type.displayName;
|
|
266
|
+
if ('name' in type && typeof type.name === 'string' && type.name)
|
|
267
|
+
return type.name;
|
|
268
|
+
}
|
|
269
|
+
return 'A component';
|
|
270
|
+
}
|
|
271
|
+
/** Components already reported by {@link reportUnregistered}. */
|
|
272
|
+
const REPORTED = new Set();
|
|
273
|
+
/**
|
|
274
|
+
* Reports, in development, a component laid out as a block beside inline content only because it
|
|
275
|
+
* never said how it flows: on the web it may sit in the line.
|
|
276
|
+
*
|
|
277
|
+
* @param children - A block container's children.
|
|
278
|
+
* @param kinds - Their inline kinds.
|
|
279
|
+
*/
|
|
280
|
+
function reportUnregistered(children, kinds) {
|
|
281
|
+
if (typeof __DEV__ === 'undefined' || !__DEV__)
|
|
282
|
+
return;
|
|
283
|
+
children.forEach((child, i) => {
|
|
284
|
+
if (kinds[i] !== null || !isValidElement(child) || typeof child.type === 'string')
|
|
285
|
+
return;
|
|
286
|
+
if (isRegistered(child.type) || REPORTED.has(child.type))
|
|
287
|
+
return;
|
|
288
|
+
if (!kinds[i - 1] && !kinds[i + 1])
|
|
289
|
+
return;
|
|
290
|
+
REPORTED.add(child.type);
|
|
291
|
+
console.error(`${componentName(child.type)} sits beside inline content in a container that is not a flex container, and React Native lays it out on a line of its own because it never said how it flows. If it renders an inline element at its root (a button, a badge, an icon), call registerInline(${componentName(child.type)}, () => 'box') from @molecule/app-ui-react/elements; otherwise give the container a flex class.`);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Whether an element's classes make it a flex container.
|
|
296
|
+
*
|
|
297
|
+
* @param split - The element's split classes.
|
|
298
|
+
* @returns Whether its children are flex items.
|
|
299
|
+
*/
|
|
300
|
+
export function isFlex(split) {
|
|
301
|
+
return split.display === 'flex' || split.display === 'inline-flex';
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* The display classes that take an element out of a line: a block, a flex or grid container, or
|
|
305
|
+
* no box at all.
|
|
306
|
+
*
|
|
307
|
+
* @param display - The split's display.
|
|
308
|
+
* @returns Whether the element is block-level.
|
|
309
|
+
*/
|
|
310
|
+
function isBlockDisplay(display) {
|
|
311
|
+
return display === 'block' || display === 'flex' || display === 'grid' || display === 'none';
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* The display classes that put an element in a line as one box.
|
|
315
|
+
*
|
|
316
|
+
* @param display - The split's display.
|
|
317
|
+
* @returns Whether the element is an inline box.
|
|
318
|
+
*/
|
|
319
|
+
function isInlineBoxDisplay(display) {
|
|
320
|
+
return display === 'inline-block' || display === 'inline-flex' || display === 'inline-grid';
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* How an element flows by its classes, given how its tag flows: a `block` or `flex` class takes an
|
|
324
|
+
* inline element out of the line, an `inline*` class puts a block one in it.
|
|
325
|
+
*
|
|
326
|
+
* @param className - The element's classes.
|
|
327
|
+
* @param tagKind - How its tag flows without classes.
|
|
328
|
+
* @returns Its inline kind.
|
|
329
|
+
*/
|
|
330
|
+
export function flowOf(className, tagKind, data) {
|
|
331
|
+
const { display } = splitClasses(typeof className === 'string' ? className : undefined, '', data);
|
|
332
|
+
if (isBlockDisplay(display))
|
|
333
|
+
return null;
|
|
334
|
+
if (display === 'inline' && tagKind === null)
|
|
335
|
+
return 'text';
|
|
336
|
+
if (isInlineBoxDisplay(display))
|
|
337
|
+
return 'box';
|
|
338
|
+
return tagKind;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* An element's `data-*` attributes by name without the prefix, which its `data-[…]:` classes
|
|
342
|
+
* match (the ClassMap's `splitForNative`). `data-mol-id` and `data-testid` name the element and
|
|
343
|
+
* style nothing.
|
|
344
|
+
*
|
|
345
|
+
* @param props - The element's props.
|
|
346
|
+
* @returns The attributes, or `undefined` when it has none.
|
|
347
|
+
*/
|
|
348
|
+
export function dataAttributes(props) {
|
|
349
|
+
let out;
|
|
350
|
+
for (const [name, value] of Object.entries(props)) {
|
|
351
|
+
if (!name.startsWith('data-') || name === 'data-mol-id' || name === 'data-testid')
|
|
352
|
+
continue;
|
|
353
|
+
if (value === undefined || value === null || value === false)
|
|
354
|
+
continue;
|
|
355
|
+
out ??= {};
|
|
356
|
+
out[name.slice('data-'.length)] = value === true ? '' : String(value);
|
|
357
|
+
}
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
360
|
+
/** A line of inline boxes: they sit side by side, wrap, and share a baseline. */
|
|
361
|
+
const LINE = { flexDirection: 'row', flexWrap: 'wrap', alignItems: 'baseline' };
|
|
362
|
+
/**
|
|
363
|
+
* The text of a CSS strut: a zero-width space. Each web line box starts with an empty inline box in
|
|
364
|
+
* its container's font and line height, so a line of small boxes is still one line tall.
|
|
365
|
+
*/
|
|
366
|
+
const STRUT = '\u200B';
|
|
367
|
+
/**
|
|
368
|
+
* A line of inline boxes as native children: runs of bare text in a `Text` with the container's
|
|
369
|
+
* text classes (white space collapsed, and taken off where the line starts and ends), after the
|
|
370
|
+
* line's strut.
|
|
371
|
+
*
|
|
372
|
+
* @param run - The line's children.
|
|
373
|
+
* @param text - The text classes the container hands down.
|
|
374
|
+
* @returns The children of the line's row.
|
|
375
|
+
*/
|
|
376
|
+
function lineItems(run, text) {
|
|
377
|
+
const className = text || undefined;
|
|
378
|
+
const out = [
|
|
379
|
+
_jsx(InheritedTextRun, { className: className, "aria-hidden": true, accessible: false, children: STRUT }, "strut"),
|
|
380
|
+
];
|
|
381
|
+
let words = [];
|
|
382
|
+
const flush = (last) => {
|
|
383
|
+
if (words.length === 0)
|
|
384
|
+
return;
|
|
385
|
+
let value = words.join('').replace(/\s+/g, ' ');
|
|
386
|
+
if (out.length === 1)
|
|
387
|
+
value = value.trimStart();
|
|
388
|
+
if (last)
|
|
389
|
+
value = value.trimEnd();
|
|
390
|
+
words = [];
|
|
391
|
+
if (value === '')
|
|
392
|
+
return;
|
|
393
|
+
out.push(_jsx(InheritedTextRun, { className: className, children: value }, `text-${out.length}`));
|
|
394
|
+
};
|
|
395
|
+
for (const child of run) {
|
|
396
|
+
if (typeof child === 'string' || typeof child === 'number')
|
|
397
|
+
words.push(String(child));
|
|
398
|
+
else {
|
|
399
|
+
flush(false);
|
|
400
|
+
out.push(child);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
flush(true);
|
|
404
|
+
return out;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Lays out a block container's children the way the web does: runs of inline children share a
|
|
408
|
+
* line (text in one `Text`, inline boxes in a wrapping row), and block children stack.
|
|
409
|
+
*
|
|
410
|
+
* @param children - The container's children.
|
|
411
|
+
* @param text - The text classes the container hands down, for a line of text.
|
|
412
|
+
* @param lines - The lines the container clamps its text to (`truncate`), or `null`.
|
|
413
|
+
* @returns The native children.
|
|
414
|
+
*/
|
|
415
|
+
export function groupInline(children, text, lines = null) {
|
|
416
|
+
const items = flatten(children);
|
|
417
|
+
const kinds = items.map(inlineKind);
|
|
418
|
+
reportUnregistered(items, kinds);
|
|
419
|
+
const out = [];
|
|
420
|
+
let run = [];
|
|
421
|
+
let boxes = false;
|
|
422
|
+
const flush = () => {
|
|
423
|
+
if (run.length === 0)
|
|
424
|
+
return;
|
|
425
|
+
const key = `line-${out.length}`;
|
|
426
|
+
out.push(boxes ? (_jsx(View, { style: run.every((child) => isValidElement(child) && child.props.hidden)
|
|
427
|
+
? [LINE, { display: 'none' }]
|
|
428
|
+
: LINE, children: lineItems(run, text) }, key)) : (_jsx(InheritedTextRun, { className: text || undefined, numberOfLines: lines ?? undefined, ellipsizeMode: lines === null ? undefined : 'tail', children: run }, key)));
|
|
429
|
+
run = [];
|
|
430
|
+
boxes = false;
|
|
431
|
+
};
|
|
432
|
+
items.forEach((child, i) => {
|
|
433
|
+
const kind = kinds[i] ?? null;
|
|
434
|
+
if (kind === null) {
|
|
435
|
+
flush();
|
|
436
|
+
out.push(_jsx(BlockSlot.Provider, { value: true, children: child }, blockKey(child, i)));
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
if (kind === 'box')
|
|
440
|
+
boxes = true;
|
|
441
|
+
run.push(child);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
flush();
|
|
445
|
+
return _jsx(BlockSlot.Provider, { value: false, children: out });
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* The key of a block child's slot: its own key, so a keyed list keeps each child's state.
|
|
449
|
+
*
|
|
450
|
+
* @param child - The child.
|
|
451
|
+
* @param index - Its position among its siblings.
|
|
452
|
+
* @returns The key.
|
|
453
|
+
*/
|
|
454
|
+
function blockKey(child, index) {
|
|
455
|
+
return isValidElement(child) && child.key !== null ? `block-${child.key}` : `block-${index}`;
|
|
456
|
+
}
|
|
457
|
+
/** Text classes that keep white space as written (`whitespace-pre`, `whitespace-pre-wrap`). */
|
|
458
|
+
const PRESERVES_SPACE = /(?:^|\s)(?:[\w-]+:)*whitespace-(?:pre|pre-wrap|break-spaces)(?:\s|$)/;
|
|
459
|
+
/**
|
|
460
|
+
* A flex item's run of text as the web draws it: white space collapsed to single spaces and taken
|
|
461
|
+
* off both ends (an anonymous flex item starts and ends a line), unless the text classes keep it.
|
|
462
|
+
*
|
|
463
|
+
* @param value - The text.
|
|
464
|
+
* @param text - The text classes in effect.
|
|
465
|
+
* @returns The text, or `''` when only white space remains.
|
|
466
|
+
*/
|
|
467
|
+
function flexItemText(value, text) {
|
|
468
|
+
return PRESERVES_SPACE.test(text) ? value : value.replace(/\s+/g, ' ').trim();
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Wraps each run of bare strings and numbers among a flex container's children in one native
|
|
472
|
+
* `Text` with the text classes it hands down, as the web makes each run of adjacent text one
|
|
473
|
+
* anonymous flex item (`{value}%` is one item, not two). A run of only white space makes none.
|
|
474
|
+
*
|
|
475
|
+
* @param children - The children.
|
|
476
|
+
* @param text - The text classes.
|
|
477
|
+
* @returns The native children.
|
|
478
|
+
*/
|
|
479
|
+
export function wrapStrings(children, text) {
|
|
480
|
+
const out = [];
|
|
481
|
+
let run = [];
|
|
482
|
+
const flush = () => {
|
|
483
|
+
if (run.length === 0)
|
|
484
|
+
return;
|
|
485
|
+
const value = flexItemText(run.join(''), text);
|
|
486
|
+
run = [];
|
|
487
|
+
if (value === '')
|
|
488
|
+
return;
|
|
489
|
+
out.push(_jsx(InheritedTextRun, { className: text || undefined, children: value }, `text-${out.length}`));
|
|
490
|
+
};
|
|
491
|
+
for (const child of flatten(children)) {
|
|
492
|
+
if (typeof child === 'string' || typeof child === 'number')
|
|
493
|
+
run.push(String(child));
|
|
494
|
+
else {
|
|
495
|
+
flush();
|
|
496
|
+
out.push(child);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
flush();
|
|
500
|
+
return out;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* A container's children laid out as the web lays them out: flex items (bare text in an
|
|
504
|
+
* anonymous `Text`) in a flex container, lines and blocks in any other.
|
|
505
|
+
*
|
|
506
|
+
* @param children - The children.
|
|
507
|
+
* @param split - The container's split classes.
|
|
508
|
+
* @returns The native children.
|
|
509
|
+
*/
|
|
510
|
+
export function layoutChildren(children, split) {
|
|
511
|
+
return isFlex(split) ? (_jsx(BlockSlot.Provider, { value: false, children: wrapStrings(children, split.text) })) : (groupInline(children, split.text, split.lines));
|
|
512
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native label associations. A form owns its id lookup, and a wrapping label
|
|
3
|
+
* activates its first labelable descendant even if that control has no id.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { type ReactElement, type ReactNode } from 'react';
|
|
8
|
+
interface LabelControl {
|
|
9
|
+
activate(): void;
|
|
10
|
+
addName(text: string): () => void;
|
|
11
|
+
}
|
|
12
|
+
interface LabelDirectory {
|
|
13
|
+
first(id: string): LabelControl | undefined;
|
|
14
|
+
text(id: string): string | undefined;
|
|
15
|
+
subscribe(listener: () => void): () => void;
|
|
16
|
+
register(id: string, control: LabelControl): () => void;
|
|
17
|
+
registerText(id: string, text: string): () => void;
|
|
18
|
+
}
|
|
19
|
+
/** Creates an id lookup for labelable controls and notifies labels when the lookup changes. */
|
|
20
|
+
export declare function createLabelDirectory(): LabelDirectory;
|
|
21
|
+
/** The nearest form's id lookup. Controls outside a form use the root lookup. */
|
|
22
|
+
export declare const FormLabels: import("react").Context<LabelDirectory>;
|
|
23
|
+
/** Reads text already present in a label's children, including text in styled spans. */
|
|
24
|
+
export declare function labelText(children: ReactNode): string;
|
|
25
|
+
/** Registers the text a label or description relationship can address by id. */
|
|
26
|
+
export declare function useLabelText(id: string | undefined, children: ReactNode, name?: string): void;
|
|
27
|
+
/** Reads a space-separated id list, preferring the nearest form and then the root. */
|
|
28
|
+
export declare function useReferencedLabelText(ids: string | undefined): string | undefined;
|
|
29
|
+
/** Connects a text label with the control named by its `htmlFor`. */
|
|
30
|
+
export declare function useLabelPress(id: string | undefined, children: ReactNode): (() => void) | undefined;
|
|
31
|
+
/** Provides the association for a label that wraps its control. */
|
|
32
|
+
export declare function WrappingLabel({ children, render, }: {
|
|
33
|
+
children: ReactNode;
|
|
34
|
+
render: (activate: () => void) => ReactElement;
|
|
35
|
+
}): ReactElement;
|
|
36
|
+
/** Registers a labelable element and returns the text of its associated labels. */
|
|
37
|
+
export declare function useLabelable(id: string | undefined, activate: (() => void) | undefined): string | undefined;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=labels.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"labels.d.ts","sourceRoot":"","sources":["../../src/elements/labels.tsx"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,SAAS,EAOf,MAAM,OAAO,CAAA;AAId,UAAU,YAAY;IACpB,QAAQ,IAAI,IAAI,CAAA;IAChB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,IAAI,CAAA;CAClC;AAED,UAAU,cAAc;IACtB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAAA;IAC3C,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;IACpC,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAA;IAC3C,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,IAAI,CAAA;IACvD,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,IAAI,CAAA;CACnD;AAED,+FAA+F;AAC/F,wBAAgB,oBAAoB,IAAI,cAAc,CAwCrD;AAKD,iFAAiF;AACjF,eAAO,MAAM,UAAU,yCAA4B,CAAA;AAEnD,wFAAwF;AACxF,wBAAgB,SAAS,CAAC,QAAQ,EAAE,SAAS,GAAG,MAAM,CAgBrD;AAED,gFAAgF;AAChF,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAO7F;AAED,sFAAsF;AACtF,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAyBlF;AAgCD,qEAAqE;AACrE,wBAAgB,aAAa,CAC3B,EAAE,EAAE,MAAM,GAAG,SAAS,EACtB,QAAQ,EAAE,SAAS,GAClB,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAK1B;AAED,mEAAmE;AACnE,wBAAgB,aAAa,CAAC,EAC5B,QAAQ,EACR,MAAM,GACP,EAAE;IACD,QAAQ,EAAE,SAAS,CAAA;IACnB,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,YAAY,CAAA;CAC/C,GAAG,YAAY,CASf;AAED,mFAAmF;AACnF,wBAAgB,YAAY,CAC1B,EAAE,EAAE,MAAM,GAAG,SAAS,EACtB,QAAQ,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,GACjC,MAAM,GAAG,SAAS,CAkCpB"}
|