@ng-native/web 0.0.1 → 0.1.1
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/LICENSE +21 -0
- package/README.md +79 -1
- package/dist/browser-engine.d.ts +146 -0
- package/dist/browser-engine.js +585 -0
- package/dist/browser-engine.js.map +1 -0
- package/dist/browser-renderer.d.ts +87 -0
- package/dist/browser-renderer.js +295 -0
- package/dist/browser-renderer.js.map +1 -0
- package/dist/css-units.d.ts +9 -0
- package/dist/css-units.js +98 -0
- package/dist/css-units.js.map +1 -0
- package/dist/device-sources.d.ts +53 -0
- package/dist/device-sources.js +96 -0
- package/dist/device-sources.js.map +1 -0
- package/dist/dom-node.d.ts +63 -0
- package/dist/dom-node.js +103 -0
- package/dist/dom-node.js.map +1 -0
- package/dist/elements.d.ts +79 -0
- package/dist/elements.js +53 -0
- package/dist/elements.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/island.d.ts +46 -0
- package/dist/island.js +109 -0
- package/dist/island.js.map +1 -0
- package/dist/jsdom-env.d.ts +4 -0
- package/dist/jsdom-env.js +67 -0
- package/dist/jsdom-env.js.map +1 -0
- package/dist/mount.d.ts +88 -0
- package/dist/mount.js +240 -0
- package/dist/mount.js.map +1 -0
- package/dist/native-style.d.ts +8 -0
- package/dist/native-style.js +108 -0
- package/dist/native-style.js.map +1 -0
- package/dist/props.d.ts +54 -0
- package/dist/props.js +608 -0
- package/dist/props.js.map +1 -0
- package/dist/reset-css.d.ts +26 -0
- package/dist/reset-css.js +163 -0
- package/dist/reset-css.js.map +1 -0
- package/dist/reset.css +282 -0
- package/dist/responder.d.ts +55 -0
- package/dist/responder.js +137 -0
- package/dist/responder.js.map +1 -0
- package/dist/web-view.d.ts +46 -0
- package/dist/web-view.js +93 -0
- package/dist/web-view.js.map +1 -0
- package/package.json +92 -3
- package/vite.d.mts +11 -0
- package/vite.mjs +41 -0
package/dist/props.js
ADDED
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import { cssValue, dashCase } from "./css-units.js";
|
|
2
|
+
import { writeStyleKey } from "./native-style.js";
|
|
3
|
+
function el(node) {
|
|
4
|
+
return node.el;
|
|
5
|
+
}
|
|
6
|
+
function setOrRemove(node, attr, value, clear) {
|
|
7
|
+
if (clear)
|
|
8
|
+
el(node).removeAttribute(attr);
|
|
9
|
+
else
|
|
10
|
+
el(node).setAttribute(attr, String(value));
|
|
11
|
+
}
|
|
12
|
+
function setStyleProp(node, prop, value, clear) {
|
|
13
|
+
const style = el(node).style;
|
|
14
|
+
if (clear)
|
|
15
|
+
style.removeProperty(prop);
|
|
16
|
+
else
|
|
17
|
+
style.setProperty(prop, cssValue(prop, value));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* `accessibilityRole`/`role` values with a direct ARIA `role`. Not every RN role has one -
|
|
21
|
+
* `pager`, `scrollview`, `horizontalscrollview`, `webview`, `drawerlayout`, `slidingdrawer`,
|
|
22
|
+
* `keyboardkey` and `text` name platform widgets ARIA has no vocabulary for - those fall through
|
|
23
|
+
* to the literal value, an unrecognised `role` an assistive technology ignores rather than a wrong
|
|
24
|
+
* one it acts on. `element-props.test.ts` holds this list against `view-base.ts`'s own.
|
|
25
|
+
*/
|
|
26
|
+
export const ROLE_MAP = {
|
|
27
|
+
none: 'presentation',
|
|
28
|
+
button: 'button',
|
|
29
|
+
togglebutton: 'button',
|
|
30
|
+
link: 'link',
|
|
31
|
+
search: 'searchbox',
|
|
32
|
+
image: 'img',
|
|
33
|
+
adjustable: 'slider',
|
|
34
|
+
imagebutton: 'button',
|
|
35
|
+
header: 'heading',
|
|
36
|
+
// A summary of the screen, as a region with its own name; `contentinfo` is the page's footer.
|
|
37
|
+
summary: 'region',
|
|
38
|
+
alert: 'alert',
|
|
39
|
+
checkbox: 'checkbox',
|
|
40
|
+
combobox: 'combobox',
|
|
41
|
+
menu: 'menu',
|
|
42
|
+
menubar: 'menubar',
|
|
43
|
+
menuitem: 'menuitem',
|
|
44
|
+
progressbar: 'progressbar',
|
|
45
|
+
radio: 'radio',
|
|
46
|
+
radiogroup: 'radiogroup',
|
|
47
|
+
scrollbar: 'scrollbar',
|
|
48
|
+
spinbutton: 'spinbutton',
|
|
49
|
+
switch: 'switch',
|
|
50
|
+
tab: 'tab',
|
|
51
|
+
tabbar: 'tablist',
|
|
52
|
+
tablist: 'tablist',
|
|
53
|
+
timer: 'timer',
|
|
54
|
+
list: 'list',
|
|
55
|
+
toolbar: 'toolbar',
|
|
56
|
+
grid: 'grid',
|
|
57
|
+
viewgroup: 'group',
|
|
58
|
+
iconmenu: 'menu',
|
|
59
|
+
};
|
|
60
|
+
function applyRole(node, value, clear) {
|
|
61
|
+
const role = typeof value === 'string' ? (ROLE_MAP[value] ?? value) : undefined;
|
|
62
|
+
setOrRemove(node, 'role', role, clear || role === undefined);
|
|
63
|
+
// Which attribute `checked` goes to depends on the role, and the two arrive in either order.
|
|
64
|
+
const state = node.props['accessibilityState'];
|
|
65
|
+
applyAccessibilityState(node, state, state === undefined || state === null);
|
|
66
|
+
}
|
|
67
|
+
function applyAccessibilityState(node, value, clear) {
|
|
68
|
+
const state = clear ? undefined : value;
|
|
69
|
+
setOrRemove(node, 'aria-disabled', state?.['disabled'], state?.['disabled'] !== true);
|
|
70
|
+
setOrRemove(node, 'aria-busy', state?.['busy'], state?.['busy'] !== true);
|
|
71
|
+
setOrRemove(node, 'aria-selected', state?.['selected'], state?.['selected'] === undefined);
|
|
72
|
+
setOrRemove(node, 'aria-expanded', state?.['expanded'], state?.['expanded'] === undefined);
|
|
73
|
+
applyChecked(node, state?.['checked']);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A button is pressed rather than checked: ARIA gives `aria-checked` no meaning on one, so a
|
|
77
|
+
* screen reader said nothing of a checked `togglebutton`, where a device says "on".
|
|
78
|
+
*/
|
|
79
|
+
function applyChecked(node, checked) {
|
|
80
|
+
const pressed = el(node).getAttribute('role') === 'button';
|
|
81
|
+
setOrRemove(node, 'aria-pressed', checked, checked === undefined || !pressed);
|
|
82
|
+
setOrRemove(node, 'aria-checked', checked, checked === undefined || pressed);
|
|
83
|
+
}
|
|
84
|
+
function applyAccessibilityValue(node, value, clear) {
|
|
85
|
+
const v = clear ? undefined : value;
|
|
86
|
+
setOrRemove(node, 'aria-valuemin', v?.['min'], v?.['min'] === undefined);
|
|
87
|
+
setOrRemove(node, 'aria-valuemax', v?.['max'], v?.['max'] === undefined);
|
|
88
|
+
setOrRemove(node, 'aria-valuenow', v?.['now'], v?.['now'] === undefined);
|
|
89
|
+
setOrRemove(node, 'aria-valuetext', v?.['text'], v?.['text'] === undefined);
|
|
90
|
+
}
|
|
91
|
+
function joinIfArray(value) {
|
|
92
|
+
return Array.isArray(value) ? value.join(' ') : value;
|
|
93
|
+
}
|
|
94
|
+
/** For a prop that genuinely has nothing to do on the web; see the file doc comment. */
|
|
95
|
+
const noop = () => { };
|
|
96
|
+
/** The keys the last whole `style` object wrote, so the next one can remove what it leaves out. */
|
|
97
|
+
const writtenStyles = new WeakMap();
|
|
98
|
+
/**
|
|
99
|
+
* A static `style="flex: 1; margin-top: 4px"`, which Angular writes whole through `setAttribute`,
|
|
100
|
+
* as the object it declares. Parsed by a scratch declaration rather than split on `;` and `:`,
|
|
101
|
+
* because on the web a value can hold both: `url(data:image/png;base64,...)`.
|
|
102
|
+
*/
|
|
103
|
+
function declarations(node, css) {
|
|
104
|
+
const scratch = el(node).ownerDocument.createElement('div').style;
|
|
105
|
+
scratch.cssText = css;
|
|
106
|
+
const out = {};
|
|
107
|
+
for (const name of Array.from(scratch))
|
|
108
|
+
out[name] = scratch.getPropertyValue(name);
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A whole style object as the `style` prop: what `[animatedStyle]` writes each frame. Angular's
|
|
113
|
+
* own `[style]` bindings arrive one key at a time through the renderer's `setStyle` instead, and
|
|
114
|
+
* a static `style` attribute arrives here too, as a string.
|
|
115
|
+
*/
|
|
116
|
+
function applyStyleObject(node, value, clear) {
|
|
117
|
+
const style = el(node).style;
|
|
118
|
+
const next = new Set();
|
|
119
|
+
// Kept as the object too, so a later `setStyle` mirrors into declarations rather than a string.
|
|
120
|
+
if (typeof value === 'string')
|
|
121
|
+
value = node.props['style'] = declarations(node, value);
|
|
122
|
+
if (!clear && typeof value === 'object') {
|
|
123
|
+
const keyed = {};
|
|
124
|
+
for (const [name, v] of Object.entries(value)) {
|
|
125
|
+
if (v !== null && v !== undefined)
|
|
126
|
+
keyed[dashCase(name)] = v;
|
|
127
|
+
}
|
|
128
|
+
for (const key of Object.keys(keyed)) {
|
|
129
|
+
for (const written of writeStyleKey(style, key, keyed))
|
|
130
|
+
next.add(written);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
for (const key of writtenStyles.get(node) ?? [])
|
|
134
|
+
if (!next.has(key))
|
|
135
|
+
style.removeProperty(key);
|
|
136
|
+
writtenStyles.set(node, next);
|
|
137
|
+
}
|
|
138
|
+
/** `ViewBase`'s own props: identical meaning on every element, whatever it committed as. */
|
|
139
|
+
const VIEW_BASE_HANDLERS = {
|
|
140
|
+
style: applyStyleObject,
|
|
141
|
+
accessibilityLabel: (n, v, c) => setOrRemove(n, 'aria-label', v, c),
|
|
142
|
+
accessibilityHint: (n, v, c) => setOrRemove(n, 'aria-description', v, c),
|
|
143
|
+
accessibilityLabelledBy: (n, v, c) => setOrRemove(n, 'aria-labelledby', joinIfArray(v), c),
|
|
144
|
+
accessibilityRole: applyRole,
|
|
145
|
+
role: applyRole,
|
|
146
|
+
accessibilityState: applyAccessibilityState,
|
|
147
|
+
accessibilityValue: applyAccessibilityValue,
|
|
148
|
+
accessibilityLiveRegion: (n, v, c) => setOrRemove(n, 'aria-live', v === 'none' ? 'off' : v, c),
|
|
149
|
+
accessibilityElementsHidden: (n, v, c) => setOrRemove(n, 'aria-hidden', v, c || v !== true),
|
|
150
|
+
// `no` and `no-hide-descendants` both hide the subtree on the web: ARIA has no way to hide a
|
|
151
|
+
// node from the accessibility tree while keeping its descendants individually reachable, which
|
|
152
|
+
// is the one place `no` and `no-hide-descendants` differ on Android.
|
|
153
|
+
importantForAccessibility: (n, v, c) => {
|
|
154
|
+
const hide = v === 'no' || v === 'no-hide-descendants';
|
|
155
|
+
setOrRemove(n, 'aria-hidden', true, c || !hide);
|
|
156
|
+
},
|
|
157
|
+
accessibilityViewIsModal: (n, v, c) => setOrRemove(n, 'aria-modal', v, c || v !== true),
|
|
158
|
+
accessibilityLanguage: (n, v, c) => setOrRemove(n, 'lang', v, c),
|
|
159
|
+
nativeID: (n, v, c) => setOrRemove(n, 'id', v, c),
|
|
160
|
+
id: (n, v, c) => setOrRemove(n, 'id', v, c),
|
|
161
|
+
testID: (n, v, c) => setOrRemove(n, 'data-testid', v, c),
|
|
162
|
+
focusable: (n, v, c) => setOrRemove(n, 'tabindex', v ? '0' : '-1', c),
|
|
163
|
+
pointerEvents: (n, v, c) => {
|
|
164
|
+
// `none` and `auto` map onto the CSS property directly. `box-none` and `box-only` have no
|
|
165
|
+
// exact CSS equivalent - native's version lets a *box* opt out of hit-testing while every
|
|
166
|
+
// descendant keeps receiving touches regardless of its own props; CSS `pointer-events` only
|
|
167
|
+
// has inheritance, which an explicit `auto` on a descendant can override but a plain,
|
|
168
|
+
// untouched descendant cannot escape. Approximated rather than left as a no-op: `box-none`
|
|
169
|
+
// sets `none` on the node itself, `box-only` sets `auto`, which is the more useful default in
|
|
170
|
+
// both directions: a full-window layer that is `box-none` lets touches through to the app
|
|
171
|
+
// under it, and a card inside that sets its own `pointerEvents: 'auto'` opts back in, which
|
|
172
|
+
// is precisely CSS's override rule and needs no web-specific code of its own to work.
|
|
173
|
+
//
|
|
174
|
+
// What this does not do: it does not make an *untouched* descendant of a `box-none` node
|
|
175
|
+
// clickable the way native does automatically - a component that never had to say
|
|
176
|
+
// `pointerEvents="auto"` on native because `box-none` only ever excluded the box itself will
|
|
177
|
+
// need to say so here too. `overlay-host`'s own content is the only place this project has
|
|
178
|
+
// exercised the gap.
|
|
179
|
+
if (v === 'none' || v === 'box-none')
|
|
180
|
+
setStyleProp(n, 'pointer-events', 'none', c);
|
|
181
|
+
else if (v === 'auto' || v === 'box-only')
|
|
182
|
+
setStyleProp(n, 'pointer-events', 'auto', c);
|
|
183
|
+
else
|
|
184
|
+
setStyleProp(n, 'pointer-events', '', true);
|
|
185
|
+
},
|
|
186
|
+
// Object/array props with no scalar form the generic fallback could write, and iOS-only
|
|
187
|
+
// accessibility surfaces with no ARIA equivalent at all. See the file doc comment's second
|
|
188
|
+
// section for why these are named rather than left to fall through.
|
|
189
|
+
hitSlop: noop,
|
|
190
|
+
accessibilityActions: noop,
|
|
191
|
+
nativeBackgroundAndroid: noop,
|
|
192
|
+
nativeForegroundAndroid: noop,
|
|
193
|
+
accessibilityIgnoresInvertColors: noop,
|
|
194
|
+
accessibilityLargeContentTitle: noop,
|
|
195
|
+
accessibilityShowsLargeContentViewer: noop,
|
|
196
|
+
accessibilityRespondsToUserInteraction: noop,
|
|
197
|
+
};
|
|
198
|
+
/** `text-input.ts`'s own props, applied to the `<textarea>` `elements.ts` creates for it. */
|
|
199
|
+
const TEXT_INPUT_HANDLERS = {
|
|
200
|
+
text: (n, v, c) => {
|
|
201
|
+
const textarea = el(n);
|
|
202
|
+
const next = c ? '' : String(v);
|
|
203
|
+
// Only when it actually differs: the caret and scroll position reset on every write, even
|
|
204
|
+
// one that sets the value to what it already was, and `TextInput`'s own echo write does
|
|
205
|
+
// exactly that on every keystroke unless this guard is here.
|
|
206
|
+
if (textarea.value !== next)
|
|
207
|
+
textarea.value = next;
|
|
208
|
+
},
|
|
209
|
+
placeholder: (n, v, c) => setOrRemove(n, 'placeholder', v, c),
|
|
210
|
+
// No inline way to style a pseudo-element, so the colour travels as a custom property and
|
|
211
|
+
// `reset.css` reads it back with `::placeholder`.
|
|
212
|
+
placeholderTextColor: (n, v, c) => setStyleProp(n, '--rn-placeholder-color', v, c),
|
|
213
|
+
keyboardType: (n, v, c) => {
|
|
214
|
+
const map = {
|
|
215
|
+
numeric: 'numeric',
|
|
216
|
+
'number-pad': 'numeric',
|
|
217
|
+
'decimal-pad': 'decimal',
|
|
218
|
+
'email-address': 'email',
|
|
219
|
+
'phone-pad': 'tel',
|
|
220
|
+
url: 'url',
|
|
221
|
+
};
|
|
222
|
+
const mode = typeof v === 'string' ? map[v] : undefined;
|
|
223
|
+
setOrRemove(n, 'inputmode', mode, c || mode === undefined);
|
|
224
|
+
},
|
|
225
|
+
returnKeyType: (n, v, c) => {
|
|
226
|
+
const known = new Set(['done', 'go', 'next', 'search', 'send', 'previous']);
|
|
227
|
+
const hint = typeof v === 'string' && known.has(v) ? v : undefined;
|
|
228
|
+
setOrRemove(n, 'enterkeyhint', hint, c || hint === undefined);
|
|
229
|
+
},
|
|
230
|
+
secureTextEntry: (n, v, c) => setStyleProp(n, '-webkit-text-security', v ? 'disc' : '', c || !v),
|
|
231
|
+
autoCapitalize: (n, v, c) => setOrRemove(n, 'autocapitalize', v === 'none' ? 'off' : v, c),
|
|
232
|
+
autoCorrect: (n, v, c) => setOrRemove(n, 'autocorrect', v ? 'on' : 'off', c),
|
|
233
|
+
autoFocus: (n, v, c) => {
|
|
234
|
+
if (!c && v)
|
|
235
|
+
el(n).focus();
|
|
236
|
+
},
|
|
237
|
+
maxLength: (n, v, c) => setOrRemove(n, 'maxlength', v, c),
|
|
238
|
+
multiline: (n, v, c) => setOrRemove(n, 'data-multiline', '', c || v !== true),
|
|
239
|
+
numberOfLines: (n, v, c) => setOrRemove(n, 'rows', v, c || n.props['multiline'] !== true),
|
|
240
|
+
editable: (n, v, c) => {
|
|
241
|
+
el(n).readOnly = !c && v === false;
|
|
242
|
+
},
|
|
243
|
+
selection: (n, v, c) => {
|
|
244
|
+
if (c)
|
|
245
|
+
return;
|
|
246
|
+
const range = v;
|
|
247
|
+
el(n).setSelectionRange(range.start, range.end ?? range.start);
|
|
248
|
+
},
|
|
249
|
+
selectTextOnFocus: (n, v, c) => setOrRemove(n, 'data-select-on-focus', '', c || v !== true),
|
|
250
|
+
textAlign: (n, v, c) => setStyleProp(n, 'text-align', v, c),
|
|
251
|
+
spellCheck: (n, v, c) => setOrRemove(n, 'spellcheck', v ? 'true' : 'false', c),
|
|
252
|
+
autoComplete: (n, v, c) => setOrRemove(n, 'autocomplete', v, c),
|
|
253
|
+
};
|
|
254
|
+
/** `switch.ts`'s own props, applied to the `<input type="checkbox">` `elements.ts` creates. */
|
|
255
|
+
const SWITCH_HANDLERS = {
|
|
256
|
+
on: (n, v, c) => {
|
|
257
|
+
el(n).checked = !c && v === true;
|
|
258
|
+
},
|
|
259
|
+
value: (n, v, c) => {
|
|
260
|
+
el(n).checked = !c && v === true;
|
|
261
|
+
},
|
|
262
|
+
disabled: (n, v, c) => {
|
|
263
|
+
el(n).disabled = !c && v === true;
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
/** `image.ts`'s own props. `image` paints with `background-image`; see `elements.ts`. */
|
|
267
|
+
const IMAGE_HANDLERS = {
|
|
268
|
+
source: (n, v, c) => {
|
|
269
|
+
const uri = sourceUri(c ? undefined : v);
|
|
270
|
+
setStyleProp(n, 'background-image', uri ? `url("${cssEscape(uri)}")` : '', !uri);
|
|
271
|
+
if (uri)
|
|
272
|
+
watchImageLoad(n, uri);
|
|
273
|
+
},
|
|
274
|
+
resizeMode: (n, v, c) => {
|
|
275
|
+
const size = {
|
|
276
|
+
cover: 'cover',
|
|
277
|
+
contain: 'contain',
|
|
278
|
+
stretch: '100% 100%',
|
|
279
|
+
};
|
|
280
|
+
const mode = typeof v === 'string' ? v : undefined;
|
|
281
|
+
setStyleProp(n, 'background-size', mode ? (size[mode] ?? 'auto') : '', c || !mode);
|
|
282
|
+
setStyleProp(n, 'background-repeat', mode === 'repeat' ? 'repeat' : 'no-repeat', c);
|
|
283
|
+
setStyleProp(n, 'background-position', 'center', c);
|
|
284
|
+
},
|
|
285
|
+
blurRadius: (n, v, c) => setStyleProp(n, 'filter', typeof v === 'number' ? `blur(${v}px)` : '', c),
|
|
286
|
+
/**
|
|
287
|
+
* The picture's own size, which on native is a default the cascade can override. Written as
|
|
288
|
+
* custom properties that `reset.css` reads at the weakest specificity there is, since an inline
|
|
289
|
+
* `width` would beat the app's own classes here exactly as it would on a device.
|
|
290
|
+
*/
|
|
291
|
+
intrinsicSize: (n, v, c) => {
|
|
292
|
+
const size = c ? undefined : v;
|
|
293
|
+
const px = (value) => (typeof value === 'number' ? `${value}px` : '');
|
|
294
|
+
setStyleProp(n, '--rn-intrinsic-width', px(size?.width), !px(size?.width));
|
|
295
|
+
setStyleProp(n, '--rn-intrinsic-height', px(size?.height), !px(size?.height));
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Says whether a `background-image` loaded, which the property itself will not.
|
|
300
|
+
*
|
|
301
|
+
* An `<image>` paints with `background-image` here (see `elements.ts` for why), and a background
|
|
302
|
+
* fires no `load` or `error` event - only a real `<img>` does. React Native's `Image` reports
|
|
303
|
+
* both, and things depend on it: an avatar-style component that swaps to a fallback when the
|
|
304
|
+
* image errors needs exactly this signal. Without it a broken image URL sat blank forever,
|
|
305
|
+
* showing neither a picture nor a fallback.
|
|
306
|
+
*
|
|
307
|
+
* A detached `Image()` is the standard way to ask. It shares the browser's cache with the
|
|
308
|
+
* background the element is already painting, so this costs a cache lookup rather than a second
|
|
309
|
+
* download.
|
|
310
|
+
*
|
|
311
|
+
* The answer is dispatched as an ordinary DOM event on the element itself, rather than pushed
|
|
312
|
+
* through the engine, so it reaches `(load)` and `(error)` by the same path every other plain
|
|
313
|
+
* binding takes. That is also why `topLoad` and `topError` are not in `browser-engine.ts`'s
|
|
314
|
+
* reserved set: there is a real DOM event of each name here, and it is this one.
|
|
315
|
+
*
|
|
316
|
+
* The uri is remembered so a source that changes mid-flight cannot have its old answer arrive
|
|
317
|
+
* late and overwrite the new one, which is the stale-response guard any fetch needs.
|
|
318
|
+
*/
|
|
319
|
+
const IMAGE_LOADS = new WeakMap();
|
|
320
|
+
function watchImageLoad(node, uri) {
|
|
321
|
+
IMAGE_LOADS.set(node, uri);
|
|
322
|
+
const el = node.el;
|
|
323
|
+
const view = el.ownerDocument.defaultView;
|
|
324
|
+
if (!view)
|
|
325
|
+
return;
|
|
326
|
+
const probe = new view.Image();
|
|
327
|
+
const settle = (type) => () => {
|
|
328
|
+
if (IMAGE_LOADS.get(node) === uri)
|
|
329
|
+
el.dispatchEvent(new view.Event(type));
|
|
330
|
+
};
|
|
331
|
+
probe.addEventListener('load', settle('load'));
|
|
332
|
+
probe.addEventListener('error', settle('error'));
|
|
333
|
+
probe.src = uri;
|
|
334
|
+
}
|
|
335
|
+
function sourceUri(value) {
|
|
336
|
+
if (!value)
|
|
337
|
+
return undefined;
|
|
338
|
+
if (typeof value === 'object' && 'uri' in value) {
|
|
339
|
+
return value.uri;
|
|
340
|
+
}
|
|
341
|
+
if (Array.isArray(value) && value.length > 0)
|
|
342
|
+
return sourceUri(value[0]);
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
function cssEscape(value) {
|
|
346
|
+
return value.replace(/["\\]/g, (c) => `\\${c}`);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* `safe-area-view.ts`'s `edges`/`mode`. Applied on either changing, from both current values,
|
|
350
|
+
* because native takes all four edges together and this follows the same rule (see that file's
|
|
351
|
+
* own `resolvedEdges` comment).
|
|
352
|
+
*
|
|
353
|
+
* `env(safe-area-inset-*)` is the real CSS mechanism for this - a phone with a notch reports a
|
|
354
|
+
* non-zero inset through it, the same number `react-native-safe-area-context` reads on iOS. What
|
|
355
|
+
* is not reproduced: `additive` mode is documented as adding the inset to whatever padding a
|
|
356
|
+
* caller's own class already put there, and there is no way to read that back out of a
|
|
357
|
+
* stylesheet this engine cannot inspect - so `additive` and `maximum` both resolve to `env()`
|
|
358
|
+
* alone here, which is `maximum` against a base of zero. A caller relying on `additive` stacking
|
|
359
|
+
* with its own padding will see less padding on the web than on native.
|
|
360
|
+
*/
|
|
361
|
+
const EDGE_PROP = {
|
|
362
|
+
top: 'top',
|
|
363
|
+
right: 'right',
|
|
364
|
+
bottom: 'bottom',
|
|
365
|
+
left: 'left',
|
|
366
|
+
};
|
|
367
|
+
function applySafeAreaInsets(node) {
|
|
368
|
+
const edges = node.props['edges'];
|
|
369
|
+
const mode = node.props['mode'] ?? 'padding';
|
|
370
|
+
const family = mode === 'margin' ? 'margin' : 'padding';
|
|
371
|
+
// The family not in use is cleared too, or switching mode left the old insets behind.
|
|
372
|
+
const other = family === 'margin' ? 'padding' : 'margin';
|
|
373
|
+
for (const edge of Object.keys(EDGE_PROP)) {
|
|
374
|
+
const edgeMode = edges?.[edge];
|
|
375
|
+
const prop = `${family}-${EDGE_PROP[edge]}`;
|
|
376
|
+
setStyleProp(node, `${other}-${EDGE_PROP[edge]}`, '', true);
|
|
377
|
+
if (!edgeMode || edgeMode === 'off')
|
|
378
|
+
setStyleProp(node, prop, '', true);
|
|
379
|
+
else
|
|
380
|
+
setStyleProp(node, prop, `env(safe-area-inset-${edge})`, false);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const SAFE_AREA_HANDLERS = {
|
|
384
|
+
edges: (n) => applySafeAreaInsets(n),
|
|
385
|
+
mode: (n) => applySafeAreaInsets(n),
|
|
386
|
+
};
|
|
387
|
+
/**
|
|
388
|
+
* `modal.ts`'s own props. `visible` defaults to `true` there specifically so a mounted-but-hidden
|
|
389
|
+
* host does not swallow touches over nothing (see that file's doc comment); on the web the same
|
|
390
|
+
* concern is a `hidden` attribute, which `reset.css` gives higher specificity than the layout
|
|
391
|
+
* reset's `display: flex` so it actually hides rather than being immediately overridden by it.
|
|
392
|
+
*/
|
|
393
|
+
const MODAL_HANDLERS = {
|
|
394
|
+
visible: (n, v, c) => setOrRemove(n, 'hidden', '', c || v !== false),
|
|
395
|
+
transparent: (n, v, c) => setStyleProp(n, 'background-color', 'transparent', c || v !== true),
|
|
396
|
+
};
|
|
397
|
+
/**
|
|
398
|
+
* `packages/icons/src/svg-props.ts`'s translation, read back the other way.
|
|
399
|
+
*
|
|
400
|
+
* `fill`/`stroke` arrive as a *brush*, not a colour string: `null` for "paint nothing", a real
|
|
401
|
+
* `{type: 0, payload}` object for an actual colour (`payload` already run through
|
|
402
|
+
* `BrowserEngine.color`, which passes a normal CSS colour string straight through), `{type: 2}`
|
|
403
|
+
* for `currentColor`, `{type: 1, brushRef}` for a gradient reference. `strokeLinecap`/
|
|
404
|
+
* `strokeLinejoin`/`fillRule`/`clipRule` arrive as the integers `svg-props.ts` encodes SVG's
|
|
405
|
+
* keywords as - native prop values, not the strings a `<svg-path stroke-linecap="round">` was
|
|
406
|
+
* written with - so the tables below are `svg-props.ts`'s own `LINECAP`/`LINEJOIN`/`FILL_RULE`
|
|
407
|
+
* read backwards. `strokeDasharray` arrives as a string array, already split on commas/whitespace
|
|
408
|
+
* rather than the original string; `matrix` as the six numbers of a 2x3 matrix rather than the
|
|
409
|
+
* `transform` string they came from. All are reconstructed here into the attribute form a browser
|
|
410
|
+
* wants.
|
|
411
|
+
*
|
|
412
|
+
* `propList` gets nothing: it is native prop names this element declared for itself, so that
|
|
413
|
+
* *its* `RNSVGGroup`/`RNSVGPath` inherits nothing it did not ask to inherit - a fake for
|
|
414
|
+
* inheritance real SVG attributes already do on their own, which is the entire reason
|
|
415
|
+
* `svg-props.ts` had to invent it.
|
|
416
|
+
*
|
|
417
|
+
* Geometry (`d`, `cx`, `cy`, `r`, `rx`, `ry`, `x`, `y`, `width`, `height`, `x1`, `y1`, `x2`, `y2`)
|
|
418
|
+
* gets no handler either, on purpose: those prop names are already the SVG attribute names,
|
|
419
|
+
* untouched by `nativeProps`, so `applyProp`'s generic string/number fallback at the bottom of
|
|
420
|
+
* this file writes them correctly with nothing said here.
|
|
421
|
+
*/
|
|
422
|
+
function brushAttr(value) {
|
|
423
|
+
const brush = value;
|
|
424
|
+
if (brush.type === 2)
|
|
425
|
+
return 'currentColor';
|
|
426
|
+
if (brush.type === 1)
|
|
427
|
+
return `url(#${brush.brushRef})`;
|
|
428
|
+
if (brush.type === 0)
|
|
429
|
+
return typeof brush.payload === 'string' ? brush.payload : undefined;
|
|
430
|
+
return undefined;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* `fill`/`stroke`. Reads `value` itself rather than the `clear` flag `applyProp` would otherwise
|
|
434
|
+
* pass every other handler, because `clear` alone cannot tell apart the two things a cleared
|
|
435
|
+
* brush prop means: `undefined` ("never declared - inherit from the ancestor group, same as real
|
|
436
|
+
* SVG's own cascade") and the brush `null` ("declared as `none` - paint nothing, even if an
|
|
437
|
+
* ancestor would otherwise supply a paint"). Only the second needs an attribute written at all.
|
|
438
|
+
* `dom-node.ts`'s `SVG_BRUSH_ELEMENTS` seeding is what makes sure this handler is even reached for
|
|
439
|
+
* a shape's first `fill="none"`/`stroke="none"` - see that file's own doc comment for why a
|
|
440
|
+
* `null` brush needs help getting here in the first place.
|
|
441
|
+
*/
|
|
442
|
+
function applyBrush(node, attr, value) {
|
|
443
|
+
if (value === undefined) {
|
|
444
|
+
el(node).removeAttribute(attr);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
if (value === null) {
|
|
448
|
+
el(node).setAttribute(attr, 'none');
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const resolved = brushAttr(value);
|
|
452
|
+
if (resolved === undefined)
|
|
453
|
+
el(node).removeAttribute(attr);
|
|
454
|
+
else
|
|
455
|
+
el(node).setAttribute(attr, resolved);
|
|
456
|
+
}
|
|
457
|
+
const LINECAP_KEYWORD = { 0: 'butt', 1: 'round', 2: 'square' };
|
|
458
|
+
const LINEJOIN_KEYWORD = { 0: 'miter', 1: 'round', 2: 'bevel' };
|
|
459
|
+
const FILL_RULE_KEYWORD = { 0: 'evenodd', 1: 'nonzero' };
|
|
460
|
+
function applyKeyword(node, attr, map, value, clear) {
|
|
461
|
+
const resolved = typeof value === 'number' ? map[value] : undefined;
|
|
462
|
+
setOrRemove(node, attr, resolved, clear || resolved === undefined);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Every shape `ng-icon.ts` parses out of an icon's markup - `svg-path`, `svg-rect`, `svg-circle`,
|
|
466
|
+
* `svg-line`, plus `svg-g` and `svg-ellipse` - shares this one set of presentation props, the
|
|
467
|
+
* same names `svg-props.ts`'s own `PRESENTATION` table lists.
|
|
468
|
+
*/
|
|
469
|
+
const SVG_PRESENTATION_HANDLERS = {
|
|
470
|
+
fill: (n, v) => applyBrush(n, 'fill', v),
|
|
471
|
+
stroke: (n, v) => applyBrush(n, 'stroke', v),
|
|
472
|
+
fillOpacity: (n, v, c) => setOrRemove(n, 'fill-opacity', v, c),
|
|
473
|
+
strokeOpacity: (n, v, c) => setOrRemove(n, 'stroke-opacity', v, c),
|
|
474
|
+
strokeWidth: (n, v, c) => setOrRemove(n, 'stroke-width', v, c),
|
|
475
|
+
strokeLinecap: (n, v, c) => applyKeyword(n, 'stroke-linecap', LINECAP_KEYWORD, v, c),
|
|
476
|
+
strokeLinejoin: (n, v, c) => applyKeyword(n, 'stroke-linejoin', LINEJOIN_KEYWORD, v, c),
|
|
477
|
+
fillRule: (n, v, c) => applyKeyword(n, 'fill-rule', FILL_RULE_KEYWORD, v, c),
|
|
478
|
+
clipRule: (n, v, c) => applyKeyword(n, 'clip-rule', FILL_RULE_KEYWORD, v, c),
|
|
479
|
+
strokeDasharray: (n, v, c) => {
|
|
480
|
+
const joined = Array.isArray(v) ? v.join(',') : undefined;
|
|
481
|
+
setOrRemove(n, 'stroke-dasharray', joined, c || joined === undefined);
|
|
482
|
+
},
|
|
483
|
+
strokeDashoffset: (n, v, c) => setOrRemove(n, 'stroke-dashoffset', v, c),
|
|
484
|
+
strokeMiterlimit: (n, v, c) => setOrRemove(n, 'stroke-miterlimit', v, c),
|
|
485
|
+
opacity: (n, v, c) => setOrRemove(n, 'opacity', v, c),
|
|
486
|
+
matrix: (n, v, c) => {
|
|
487
|
+
const matrix = Array.isArray(v) ? `matrix(${v.join(',')})` : undefined;
|
|
488
|
+
setOrRemove(n, 'transform', matrix, c || matrix === undefined);
|
|
489
|
+
},
|
|
490
|
+
propList: noop,
|
|
491
|
+
};
|
|
492
|
+
/**
|
|
493
|
+
* The SVG root's own sizing props - `ng-icon.ts`'s host bindings, none of which go through
|
|
494
|
+
* `svg-props.ts`'s `nativeProps` (they are written directly, not parsed off markup).
|
|
495
|
+
* `minX`/`minY`/`vbWidth`/`vbHeight` build one `viewBox` attribute together, and
|
|
496
|
+
* `align`/`meetOrSlice` build one `preserveAspectRatio` together, so each handler rereads every
|
|
497
|
+
* prop in its group off `node.props` rather than trusting the single value it was called with -
|
|
498
|
+
* the same pattern `applySafeAreaInsets` above uses for `edges`/`mode`, needed for the same
|
|
499
|
+
* reason: Angular commits host bindings one at a time, so whichever of a group changes first would
|
|
500
|
+
* otherwise write an attribute built from the rest still being stale (`undefined`, before their
|
|
501
|
+
* own first commit) or from a previous render's values.
|
|
502
|
+
*/
|
|
503
|
+
function applySvgViewBox(node) {
|
|
504
|
+
const minX = node.props['minX'];
|
|
505
|
+
const minY = node.props['minY'];
|
|
506
|
+
const vbWidth = node.props['vbWidth'];
|
|
507
|
+
const vbHeight = node.props['vbHeight'];
|
|
508
|
+
const complete = typeof minX === 'number' &&
|
|
509
|
+
typeof minY === 'number' &&
|
|
510
|
+
typeof vbWidth === 'number' &&
|
|
511
|
+
typeof vbHeight === 'number';
|
|
512
|
+
setOrRemove(node, 'viewBox', complete ? `${minX} ${minY} ${vbWidth} ${vbHeight}` : undefined, !complete);
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* `bbWidth`/`bbHeight` are the root's own rendered size in points - `NgIcon`'s `size()` input -
|
|
516
|
+
* kept as real `width`/`height` attributes alongside the `[style]` host binding that sizes the
|
|
517
|
+
* box for layout, so an inspector reading the SVG element in isolation sees the same size CSS is
|
|
518
|
+
* about to give it.
|
|
519
|
+
*/
|
|
520
|
+
function applySvgSize(node) {
|
|
521
|
+
const bbWidth = node.props['bbWidth'];
|
|
522
|
+
const bbHeight = node.props['bbHeight'];
|
|
523
|
+
setOrRemove(node, 'width', bbWidth, typeof bbWidth !== 'number');
|
|
524
|
+
setOrRemove(node, 'height', bbHeight, typeof bbHeight !== 'number');
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* `0`/`1`/`2` are `RNSVGSvgView`'s own encoding of `meet`/`slice`/`none` - see `svg-props.ts`'s
|
|
528
|
+
* `viewBoxProps`, which always sends `0` for both roots today. `slice`/`none` are translated on
|
|
529
|
+
* the chance a future caller sets one directly, not because either root emits them yet; that
|
|
530
|
+
* would be a divergence worth noting if it ever mattered, but nothing in this codebase exercises
|
|
531
|
+
* it.
|
|
532
|
+
*/
|
|
533
|
+
const MEET_OR_SLICE = { 0: 'meet', 1: 'slice', 2: 'none' };
|
|
534
|
+
function applySvgAspectRatio(node) {
|
|
535
|
+
const align = node.props['align'];
|
|
536
|
+
const meetOrSlice = node.props['meetOrSlice'];
|
|
537
|
+
if (typeof align !== 'string') {
|
|
538
|
+
el(node).removeAttribute('preserveAspectRatio');
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
const slice = typeof meetOrSlice === 'number' ? (MEET_OR_SLICE[meetOrSlice] ?? 'meet') : 'meet';
|
|
542
|
+
el(node).setAttribute('preserveAspectRatio', `${align} ${slice}`);
|
|
543
|
+
}
|
|
544
|
+
const SVG_ROOT_HANDLERS = {
|
|
545
|
+
minX: (n) => applySvgViewBox(n),
|
|
546
|
+
minY: (n) => applySvgViewBox(n),
|
|
547
|
+
vbWidth: (n) => applySvgViewBox(n),
|
|
548
|
+
vbHeight: (n) => applySvgViewBox(n),
|
|
549
|
+
bbWidth: (n) => applySvgSize(n),
|
|
550
|
+
bbHeight: (n) => applySvgSize(n),
|
|
551
|
+
align: (n) => applySvgAspectRatio(n),
|
|
552
|
+
meetOrSlice: (n) => applySvgAspectRatio(n),
|
|
553
|
+
// `NgIcon`'s own `[color]` host binding - what a shape's `currentColor` brush (`brushAttr`'s
|
|
554
|
+
// `{type: 2}` case, written as the literal attribute value `currentColor`) resolves against.
|
|
555
|
+
// Real SVG's `currentColor` already means "this element's computed CSS `color`", so setting it
|
|
556
|
+
// here as an inline style *is* the whole translation - no downstream shape needs to know the
|
|
557
|
+
// actual value, exactly as native's brush stays unresolved until the view paints.
|
|
558
|
+
color: (n, v, c) => setStyleProp(n, 'color', v, c || typeof v !== 'string'),
|
|
559
|
+
};
|
|
560
|
+
/**
|
|
561
|
+
* `activity-indicator.ts`'s colour, as the `color` its spinner in `reset.css` paints with. The
|
|
562
|
+
* spinner is drawn by the stylesheet, and `animating` and `hidesWhenStopped` reach it as the
|
|
563
|
+
* plain attributes the fallback writes.
|
|
564
|
+
*/
|
|
565
|
+
const ACTIVITY_INDICATOR_HANDLERS = {
|
|
566
|
+
color: (n, v, c) => setStyleProp(n, 'color', v, c || typeof v !== 'string'),
|
|
567
|
+
};
|
|
568
|
+
const BY_ELEMENT = {
|
|
569
|
+
'activity-indicator': ACTIVITY_INDICATOR_HANDLERS,
|
|
570
|
+
'text-input': TEXT_INPUT_HANDLERS,
|
|
571
|
+
switch: SWITCH_HANDLERS,
|
|
572
|
+
image: IMAGE_HANDLERS,
|
|
573
|
+
'safe-area-view': SAFE_AREA_HANDLERS,
|
|
574
|
+
modal: MODAL_HANDLERS,
|
|
575
|
+
'svg-g': SVG_PRESENTATION_HANDLERS,
|
|
576
|
+
'svg-path': SVG_PRESENTATION_HANDLERS,
|
|
577
|
+
'svg-circle': SVG_PRESENTATION_HANDLERS,
|
|
578
|
+
'svg-ellipse': SVG_PRESENTATION_HANDLERS,
|
|
579
|
+
'svg-rect': SVG_PRESENTATION_HANDLERS,
|
|
580
|
+
'svg-line': SVG_PRESENTATION_HANDLERS,
|
|
581
|
+
'ng-icon': SVG_ROOT_HANDLERS,
|
|
582
|
+
};
|
|
583
|
+
/**
|
|
584
|
+
* Apply one prop to its node. `clear` is `value === undefined || value === null`.
|
|
585
|
+
*
|
|
586
|
+
* A key with a handler - either element-specific or from `ViewBase`'s own set - runs exactly
|
|
587
|
+
* that translation. Everything else falls to the generic attribute write the file doc comment
|
|
588
|
+
* describes, which is why a `noop` entry (`hitSlop`, `accessibilityActions`, ...) has to be a
|
|
589
|
+
* real entry rather than an absent one: an absent key falls through and gets written as a raw
|
|
590
|
+
* attribute, a `noop` key is claimed and genuinely does nothing.
|
|
591
|
+
*/
|
|
592
|
+
export function applyProp(node, key, value, clear) {
|
|
593
|
+
const elementHandler = BY_ELEMENT[node.name]?.[key];
|
|
594
|
+
const baseHandler = VIEW_BASE_HANDLERS[key];
|
|
595
|
+
if (elementHandler || baseHandler) {
|
|
596
|
+
elementHandler?.(node, value, clear);
|
|
597
|
+
baseHandler?.(node, value, clear);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (clear) {
|
|
601
|
+
el(node).removeAttribute(key);
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
605
|
+
el(node).setAttribute(key, String(value));
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
//# sourceMappingURL=props.js.map
|