embedded-react 0.21.0 → 0.22.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 (99) hide show
  1. package/README.md +2 -2
  2. package/aot/compile.mjs +1 -1
  3. package/aot/compiler/alerts.mjs +604 -0
  4. package/aot/compiler/check.mjs +1 -1
  5. package/aot/compiler/collect.mjs +1 -1
  6. package/aot/compiler/elements/button.mjs +3 -3
  7. package/aot/compiler/elements/image-background.mjs +1 -1
  8. package/aot/compiler/elements/props.mjs +10 -6
  9. package/aot/compiler/elements/section-list.mjs +1 -1
  10. package/aot/compiler/expressions.mjs +29 -2
  11. package/aot/compiler/handlers.mjs +73 -16
  12. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  13. package/aot/compiler/keyboard.mjs +1 -1
  14. package/aot/compiler/lower.mjs +12 -0
  15. package/aot/compiler/nodes.mjs +17 -9
  16. package/aot/compiler/out.mjs +4 -0
  17. package/aot/compiler/package/codegen.mjs +36 -2
  18. package/aot/compiler/package/format.mjs +5 -3
  19. package/aot/compiler/package/lower.mjs +53 -19
  20. package/aot/compiler/scroll-to.mjs +112 -0
  21. package/aot/compiler/style-text.mjs +2 -2
  22. package/aot/compiler/svg.mjs +6 -3
  23. package/aot/compiler/updaters.mjs +1 -0
  24. package/aot/style-map.mjs +44 -1
  25. package/assets/bake-font.mjs +187 -244
  26. package/assets/bake-image.mjs +47 -71
  27. package/assets/bake-svg.mjs +465 -533
  28. package/assets/build-builtin-font.mjs +14 -27
  29. package/assets/emit-c.mjs +124 -204
  30. package/assets/emit-container.mjs +129 -197
  31. package/assets/emit-pack.mjs +92 -136
  32. package/assets/font-config.mjs +19 -27
  33. package/assets/font-sizes.mjs +432 -507
  34. package/assets/glyph-coverage.mjs +170 -214
  35. package/assets/index.mjs +52 -97
  36. package/assets/rasterize.mjs +173 -194
  37. package/assets/svg-loader.mjs +42 -48
  38. package/build.mjs +84 -101
  39. package/cli.mjs +482 -621
  40. package/compile-bin.mjs +24 -36
  41. package/flash.mjs +297 -401
  42. package/hotreload/dev-device.mjs +229 -270
  43. package/hotreload/pack-app.mjs +118 -159
  44. package/hotreload/pack-split.mjs +144 -200
  45. package/hotreload/package-transfer.mjs +128 -131
  46. package/hotreload/protocol.mjs +118 -118
  47. package/hotreload/uploader.mjs +132 -159
  48. package/hotreload/watch-tree.mjs +56 -65
  49. package/pack-container.mjs +93 -123
  50. package/package.json +19 -16
  51. package/persist-transform.mjs +89 -105
  52. package/qjsc-wasm.mjs +47 -72
  53. package/sim/embedded-react.wasm +0 -0
  54. package/sim/index.html +1 -1
  55. package/sim-server.mjs +260 -354
  56. package/src/embedded-react/Alert.mjs +112 -0
  57. package/src/embedded-react/Animated.mjs +412 -0
  58. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  59. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  60. package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
  61. package/src/embedded-react/Easing.mjs +34 -0
  62. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  63. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  64. package/src/embedded-react/Keyboard.mjs +82 -0
  65. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  66. package/src/embedded-react/PanResponder.mjs +256 -0
  67. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  68. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  69. package/src/embedded-react/SectionList.mjs +103 -0
  70. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  71. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  72. package/src/embedded-react/background-image.mjs +321 -0
  73. package/src/embedded-react/{components.js → components.mjs} +1 -3
  74. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  75. package/src/embedded-react/index.d.ts +155 -2
  76. package/src/embedded-react/index.js +26 -19
  77. package/src/embedded-react/layout-anim-config.mjs +85 -0
  78. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  79. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  80. package/src/embedded-react/nativeModule.mjs +371 -0
  81. package/src/embedded-react/split-style.mjs +57 -0
  82. package/src/embedded-react/svg-ops.mjs +1075 -0
  83. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  84. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  85. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  86. package/src/host-config.mjs +512 -0
  87. package/src/{native-ui.js → native-ui.mjs} +1 -8
  88. package/src/props.mjs +341 -0
  89. package/src/{renderer.js → renderer.mjs} +30 -44
  90. package/src/embedded-react/Animated.js +0 -440
  91. package/src/embedded-react/Easing.js +0 -39
  92. package/src/embedded-react/PanResponder.js +0 -330
  93. package/src/embedded-react/SectionList.js +0 -146
  94. package/src/embedded-react/layout-anim-config.js +0 -101
  95. package/src/embedded-react/nativeModule.js +0 -462
  96. package/src/embedded-react/split-style.js +0 -58
  97. package/src/embedded-react/svg-ops.js +0 -1295
  98. package/src/host-config.js +0 -548
  99. package/src/props.js +0 -306
@@ -0,0 +1,256 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /** Per-instance gesture id, so two responders on screen at once are told apart. */
17
+ let s_nextStateID = 1;
18
+ /** Unsupported config keys already warned about (each unique key warns once per process). */
19
+ const s_warnedKeys = new Set();
20
+ /** Config keys this module acts on. Everything else is either RN-Android-specific or a typo. */
21
+ const SUPPORTED = [
22
+ 'onStartShouldSetPanResponder',
23
+ 'onStartShouldSetPanResponderCapture',
24
+ 'onMoveShouldSetPanResponder',
25
+ 'onMoveShouldSetPanResponderCapture',
26
+ 'onPanResponderGrant',
27
+ 'onPanResponderMove',
28
+ 'onPanResponderRelease',
29
+ 'onPanResponderTerminate',
30
+ 'onPanResponderTerminationRequest',
31
+ 'onPanResponderReject',
32
+ 'onPanResponderStart',
33
+ 'onPanResponderEnd',
34
+ ];
35
+ /**
36
+ * The engine's monotonic millisecond clock, used for velocity. `performance.now()` is installed by the
37
+ * QuickJS runtime on every context (it is React's scheduler clock) and exists in the simulator and in
38
+ * Node; `Date.now()` jumps whenever the host sets the wall clock, so it is never used here.
39
+ * The clock only advances when the host ticks, so an elapsed time of 0 is normal and leaves velocity
40
+ * untouched.
41
+ */
42
+ const now = () => typeof performance !== 'undefined' && typeof performance.now === 'function'
43
+ ? performance.now()
44
+ : 0;
45
+ /**
46
+ * Coerces whatever was passed to a usable config object. A default parameter only covers `undefined`,
47
+ * so `create(null)` would throw on Object.keys, and a primitive would enumerate its INDICES as unknown
48
+ * keys ("ignores 0, 1, 2, 3"). Neither is a useful failure for a public API on a device.
49
+ */
50
+ function asConfig(config) {
51
+ return config !== null && typeof config === 'object'
52
+ ? config
53
+ : {};
54
+ }
55
+ /** Warns ONCE PER KEY about config keys this module does not act on. */
56
+ function warnUnsupportedConfig(config) {
57
+ const names = Object.keys(config).filter(key => !SUPPORTED.includes(key) && !s_warnedKeys.has(key));
58
+ if (names.length === 0)
59
+ return;
60
+ for (const key of names)
61
+ s_warnedKeys.add(key);
62
+ console.warn(`embedded-react: PanResponder ignores ${names.join(', ')} — ` +
63
+ `supported: ${SUPPORTED.join(', ')}.`);
64
+ }
65
+ /**
66
+ * Clears the travel/velocity fields between gestures. `numberActiveTouches` is deliberately left alone:
67
+ * it counts fingers on the panel, which outlives the gesture when a second finger is still down.
68
+ */
69
+ function resetGestureState(gestureState) {
70
+ gestureState.moveX = 0;
71
+ gestureState.moveY = 0;
72
+ gestureState.x0 = 0;
73
+ gestureState.y0 = 0;
74
+ gestureState.dx = 0;
75
+ gestureState.dy = 0;
76
+ gestureState.vx = 0;
77
+ gestureState.vy = 0;
78
+ }
79
+ /**
80
+ * Builds a gesture recogniser. Call it ONCE per component and keep the result in a ref — the returned
81
+ * handlers close over one mutable gestureState, so re-creating it per render throws the drag away.
82
+ *
83
+ * `userConfig` holds RN-shaped callbacks; see SUPPORTED. Every one is optional, and a non-object (null, a
84
+ * primitive) is treated as an empty config. Spread the returned `panHandlers` onto a component.
85
+ */
86
+ export function create(userConfig = {}) {
87
+ const config = asConfig(userConfig);
88
+ warnUnsupportedConfig(config);
89
+ // The single mutable object handed to every callback — same identity for the responder's lifetime,
90
+ // as in RN, so a handler may stash it but must not expect a snapshot.
91
+ const gestureState = {
92
+ /** Identifies this responder; stable for its lifetime. */
93
+ stateID: s_nextStateID++,
94
+ /** Latest touch point. */
95
+ moveX: 0,
96
+ moveY: 0,
97
+ /** Where the gesture was granted — the anchor dx/dy are measured from. */
98
+ x0: 0,
99
+ y0: 0,
100
+ /** Travel since the grant, in pixels. */
101
+ dx: 0,
102
+ dy: 0,
103
+ /** Velocity in pixels per millisecond. */
104
+ vx: 0,
105
+ vy: 0,
106
+ /** Fingers currently on the panel (within this responder's subtree). */
107
+ numberActiveTouches: 0,
108
+ };
109
+ let granted = false;
110
+ let lastMoveTime = 0;
111
+ // The engine measures event.dx/event.dy from TOUCH-DOWN; RN (and this module) measure
112
+ // gestureState.dx/dy from the GRANT — a responder that needed 10px of slop to claim starts its own
113
+ // travel at zero, so the slop never shows up as a jump. The base is what event.dx read at the moment
114
+ // of the grant.
115
+ let baseDx = 0;
116
+ let baseDy = 0;
117
+ const fire = (name, event) => {
118
+ if (typeof config[name] === 'function')
119
+ config[name](event, gestureState);
120
+ };
121
+ /** Folds one engine payload (event.x/y absolute, event.dx/dy from touch-down) into the travel fields. */
122
+ const advance = (event) => {
123
+ gestureState.moveX = event.x;
124
+ gestureState.moveY = event.y;
125
+ gestureState.dx = event.dx - baseDx;
126
+ gestureState.dy = event.dy - baseDy;
127
+ };
128
+ /** A move: travel, plus a fresh velocity reading over the time since the last one. */
129
+ const track = (event) => {
130
+ const time = now();
131
+ const elapsed = time - lastMoveTime;
132
+ const previousDx = gestureState.dx;
133
+ const previousDy = gestureState.dy;
134
+ advance(event);
135
+ // The elapsed time is 0 whenever two touches land inside one host frame (the clock only moves on
136
+ // er_tick), and the division would be Infinity. Keeping the previous velocity is the honest
137
+ // answer: nothing new has been measured yet.
138
+ if (elapsed > 0) {
139
+ gestureState.vx = (gestureState.dx - previousDx) / elapsed;
140
+ gestureState.vy = (gestureState.dy - previousDy) / elapsed;
141
+ }
142
+ lastMoveTime = time;
143
+ };
144
+ /** Wraps a should-set predicate as an engine query: track the point, ask, absent means "no" (as in RN). */
145
+ const query = (name) => typeof config[name] === 'function'
146
+ ? event => {
147
+ if (!granted)
148
+ advance(event);
149
+ return config[name](event, gestureState) === true;
150
+ }
151
+ : undefined;
152
+ /** Ends the gesture: reports it if this responder owned it, then clears the state for the next one. */
153
+ const finish = (event, callback) => {
154
+ const wasGranted = granted;
155
+ granted = false;
156
+ // Fold the final point into the travel so `dx` is the gesture's true total even when the finger
157
+ // lifted without a final move — but leave vx/vy at the last MOVE's reading. A fling ends with the
158
+ // finger resting for a frame or two, and re-measuring across that would report zero and throw the
159
+ // throw away.
160
+ advance(event);
161
+ if (wasGranted)
162
+ fire(callback, event);
163
+ resetGestureState(gestureState);
164
+ baseDx = 0;
165
+ baseDy = 0;
166
+ };
167
+ const panHandlers = {
168
+ // --- Raw touch bookkeeping (bubbles per finger): finger count + Start/End for extra fingers ---
169
+ onTouchStart(event) {
170
+ gestureState.numberActiveTouches += 1;
171
+ // A later finger joins the gesture in flight (the engine events carry no finger id, so
172
+ // independent tracking is impossible). Its own touch slot re-negotiates and re-grants this
173
+ // node, and THAT grant fires onPanResponderStart — not this raw handler, which runs first.
174
+ if (gestureState.numberActiveTouches > 1)
175
+ return;
176
+ resetGestureState(gestureState);
177
+ baseDx = 0;
178
+ baseDy = 0;
179
+ gestureState.x0 = event.x;
180
+ gestureState.y0 = event.y;
181
+ gestureState.moveX = event.x;
182
+ gestureState.moveY = event.y;
183
+ lastMoveTime = now();
184
+ },
185
+ onTouchEnd(event) {
186
+ if (gestureState.numberActiveTouches > 0)
187
+ gestureState.numberActiveTouches -= 1;
188
+ // A finger lifted but the gesture continues; the RELEASE for the last one arrives as a
189
+ // responder event below.
190
+ if (granted && gestureState.numberActiveTouches > 0)
191
+ fire('onPanResponderEnd', event);
192
+ },
193
+ onTouchCancel() {
194
+ gestureState.numberActiveTouches = 0;
195
+ },
196
+ // --- Negotiation queries (the engine calls these; returning true claims the gesture) ---
197
+ onStartShouldSetResponder: query('onStartShouldSetPanResponder'),
198
+ onStartShouldSetResponderCapture: query('onStartShouldSetPanResponderCapture'),
199
+ onMoveShouldSetResponder: query('onMoveShouldSetPanResponder'),
200
+ onMoveShouldSetResponderCapture: query('onMoveShouldSetPanResponderCapture'),
201
+ onResponderTerminationRequest: typeof config.onPanResponderTerminationRequest === 'function'
202
+ ? event => config.onPanResponderTerminationRequest(event, gestureState) ===
203
+ true
204
+ : undefined, // absent → the engine's default: yield (RN's default too)
205
+ // --- Responder lifecycle (native events; fire only on the node that owns the gesture) ---
206
+ onResponderGrant(event) {
207
+ if (granted) {
208
+ // A second finger's touch slot granted the same node: one gesture, not two. RN calls this a
209
+ // responder "start" for the extra touch.
210
+ fire('onPanResponderStart', event);
211
+ return;
212
+ }
213
+ granted = true;
214
+ baseDx = event.dx;
215
+ baseDy = event.dy;
216
+ gestureState.x0 = event.x;
217
+ gestureState.y0 = event.y;
218
+ gestureState.dx = 0;
219
+ gestureState.dy = 0;
220
+ lastMoveTime = now();
221
+ fire('onPanResponderGrant', event);
222
+ },
223
+ onResponderMove(event) {
224
+ if (!granted)
225
+ return;
226
+ track(event);
227
+ fire('onPanResponderMove', event);
228
+ },
229
+ onResponderRelease(event) {
230
+ if (!granted)
231
+ return;
232
+ // Another finger still down: its touch slot keeps the responder, so the gesture continues (the
233
+ // raw onTouchEnd above already reported the End).
234
+ if (gestureState.numberActiveTouches > 0)
235
+ return;
236
+ finish(event, 'onPanResponderRelease');
237
+ },
238
+ onResponderTerminate(event) {
239
+ if (!granted)
240
+ return;
241
+ gestureState.numberActiveTouches = 0;
242
+ finish(event, 'onPanResponderTerminate');
243
+ },
244
+ onResponderReject(event) {
245
+ fire('onPanResponderReject', event);
246
+ },
247
+ };
248
+ // Absent queries must not appear as props at all (an undefined on* prop is skipped by the host
249
+ // config anyway, but a clean bag is easier to reason about and to spread).
250
+ for (const key of Object.keys(panHandlers)) {
251
+ if (panHandlers[key] === undefined)
252
+ delete panHandlers[key];
253
+ }
254
+ return { panHandlers };
255
+ }
256
+ export const PanResponder = { create };
@@ -13,14 +13,13 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
- // Platform — minimal RN analog. OS is "embedded"; select() picks the embedded entry (falling back
18
- // to default) so apps can write Platform.select({ embedded: ..., default: ... }).
19
16
  export const Platform = {
20
- OS: 'embedded',
21
- select(specifics) {
22
- if (specifics == null) return undefined;
23
- if ('embedded' in specifics) return specifics.embedded;
24
- return specifics.default;
25
- },
17
+ OS: 'embedded',
18
+ select(specifics) {
19
+ if (specifics == null)
20
+ return undefined;
21
+ if ('embedded' in specifics)
22
+ return specifics.embedded;
23
+ return specifics.default;
24
+ },
26
25
  };
@@ -13,41 +13,28 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
16
  // Pressable — RN's core press target, as a forwardRef component over the host <Pressable> element.
18
17
  //
19
18
  // Setting `disabled` withholds all press-related event handlers (`onPress`, `onLongPress`, `onPressIn`,
20
19
  // `onPressOut`) so that NativeUI never registers callbacks on the native node. The C hit-testing
21
20
  // logic skips nodes without press handlers, preventing disabled pressables from claiming the gesture responder.
22
- import {createElement, forwardRef} from 'react';
23
- import {Pressable as HostPressable} from './components.js';
24
-
21
+ import { createElement, forwardRef } from 'react';
22
+ import { Pressable as HostPressable } from './components.mjs';
25
23
  /**
26
24
  * A container that responds to touch interactions.
27
25
  *
28
26
  * When `disabled` is true, all press handlers are withheld so the node will not intercept touches
29
27
  * or fire callbacks.
30
- *
31
- * @param {object} [props] Pressable props.
32
- * @param {boolean} [props.disabled] Whether touch interactions are disabled.
33
- * @param {Function} [props.onPress] Called when a press is activated.
34
- * @param {Function} [props.onLongPress] Called when a long press is activated.
35
- * @param {Function} [props.onPressIn] Called immediately when a touch is pressed down.
36
- * @param {Function} [props.onPressOut] Called when a touch is released or canceled.
37
- * @param {*} [props.children] Children elements.
38
- * @returns {*} A host <Pressable> element.
39
28
  */
40
29
  export const Pressable = forwardRef(function Pressable(props = {}, ref) {
41
- const {disabled, onPress, onLongPress, onPressIn, onPressOut, ...rest} =
42
- props;
43
-
44
- return createElement(HostPressable, {
45
- ...rest,
46
- ref,
47
- disabled,
48
- onPress: disabled ? undefined : onPress,
49
- onLongPress: disabled ? undefined : onLongPress,
50
- onPressIn: disabled ? undefined : onPressIn,
51
- onPressOut: disabled ? undefined : onPressOut,
52
- });
30
+ const { disabled, onPress, onLongPress, onPressIn, onPressOut, ...rest } = props;
31
+ return createElement(HostPressable, {
32
+ ...rest,
33
+ ref,
34
+ disabled,
35
+ onPress: disabled ? undefined : onPress,
36
+ onLongPress: disabled ? undefined : onLongPress,
37
+ onPressIn: disabled ? undefined : onPressIn,
38
+ onPressOut: disabled ? undefined : onPressOut,
39
+ });
53
40
  });
@@ -0,0 +1,103 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ // SectionList — a thin API-compat wrapper over <ScrollView>, NOT a virtualized list. It is <FlatList>
17
+ // with a header and a footer around each group:
18
+ //
19
+ // <SectionList sections={[{title: 'A', data: ['ant']}]}
20
+ // renderSectionHeader={({section}) => <Header title={section.title} />}
21
+ // renderItem={({item}) => <Row item={item} />} />
22
+ // renders the same tree as
23
+ // <ScrollView><Header title="A" /><Row item="ant" /></ScrollView>
24
+ //
25
+ // Header, rows and footer are FLAT SIBLINGS, as in RN — no wrapper node per section — so a section
26
+ // costs exactly the nodes it draws. Nothing sticks: `stickySectionHeadersEnabled` needs a scroll
27
+ // listener re-laying the header out every frame, which is the per-event JS cost Flow A can least
28
+ // afford.
29
+ //
30
+ // Everything the FlatList section of the README says about the node budget applies here and then
31
+ // some: a section adds its header and footer on top of its rows, and every one of them mounts and
32
+ // stays mounted. Size `sections x (1 + rows) x nodes-per-row` against ERUI_MAX_NODES before writing
33
+ // the screen.
34
+ //
35
+ // Flow B builds the same tree, unrolling the sections at build time (aot/compiler/elements/section-list.mts).
36
+ import { createElement } from 'react';
37
+ import { ScrollView } from './components.mjs';
38
+ import { pushKeyedChild } from './list-child.mjs';
39
+ import { createPropWarner, RN_PLATFORM_NO_OPS } from './warn-props.mjs';
40
+ /** The props this honours. The rest are virtualization / platform knobs with no meaning on a panel. */
41
+ const SUPPORTED = [
42
+ 'sections',
43
+ 'renderItem',
44
+ 'renderSectionHeader',
45
+ 'renderSectionFooter',
46
+ 'keyExtractor',
47
+ 'style',
48
+ ];
49
+ const warnUnsupportedProps = createPropWarner('SectionList', SUPPORTED, 'it is a thin <ScrollView> alias with no virtualization and no sticky headers. For separators, ' +
50
+ 'list headers/footers or onEndReached, use <ScrollView> + .map directly.', RN_PLATFORM_NO_OPS);
51
+ /**
52
+ * Renders `sections` into a <ScrollView>, each as an optional header, its rows, and an optional footer.
53
+ *
54
+ * Row keys are namespaced by section (`<section>:<row>`), because the rows of every section end up as
55
+ * siblings of one another — an index or an id that is only unique WITHIN a section would collide.
56
+ *
57
+ * Missing props render an empty scroller. Anything else on a section (RN's convention is `title`) is
58
+ * yours, and reaches the render callbacks. A section's own `renderItem` and `keyExtractor` win for that
59
+ * section, as in RN; a row's key defaults to the one `renderItem` already set, else the index. `style`
60
+ * goes to the ScrollView untouched.
61
+ */
62
+ export function SectionList(props = {}) {
63
+ const { sections, renderItem, renderSectionHeader, renderSectionFooter, keyExtractor, style, } = props;
64
+ warnUnsupportedProps(props);
65
+ const children = [];
66
+ if (Array.isArray(sections)) {
67
+ for (let sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
68
+ const section = sections[sectionIndex];
69
+ if (section === null || typeof section !== 'object')
70
+ continue;
71
+ const sectionKey = section.key === null || section.key === undefined
72
+ ? String(sectionIndex)
73
+ : String(section.key);
74
+ if (typeof renderSectionHeader === 'function')
75
+ pushKeyedChild(children, renderSectionHeader({ section }), () => `${sectionKey}:$header`);
76
+ const rowRenderer = typeof section.renderItem === 'function'
77
+ ? section.renderItem
78
+ : renderItem;
79
+ const rowKey = typeof section.keyExtractor === 'function'
80
+ ? section.keyExtractor
81
+ : keyExtractor;
82
+ if (Array.isArray(section.data) && typeof rowRenderer === 'function') {
83
+ for (let index = 0; index < section.data.length; index++) {
84
+ const item = section.data[index];
85
+ const row = rowRenderer({ item, index, section });
86
+ // Built lazily, so keyExtractor never runs for a row that rendered nothing or for a raw
87
+ // string — see pushKeyedChild.
88
+ pushKeyedChild(children, row, element => {
89
+ const key = rowKey
90
+ ? String(rowKey(item, index))
91
+ : element.key !== null && element.key !== undefined
92
+ ? element.key
93
+ : String(index);
94
+ return `${sectionKey}:${key}`;
95
+ });
96
+ }
97
+ }
98
+ if (typeof renderSectionFooter === 'function')
99
+ pushKeyedChild(children, renderSectionFooter({ section }), () => `${sectionKey}:$footer`);
100
+ }
101
+ }
102
+ return createElement(ScrollView, style === undefined ? null : { style }, children);
103
+ }
@@ -13,25 +13,21 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
- // StyleSheet — the React Native analog. `create` is an identity pass-through (styles are plain
18
- // objects the host config flattens at setProps time); `flatten` collapses nested arrays/objects.
19
16
  export const StyleSheet = {
20
- create(styles) {
21
- return styles;
22
- },
23
-
24
- flatten(style) {
25
- if (!style) return {};
26
- if (Array.isArray(style)) {
27
- const flattened = {};
28
- for (const entry of style)
29
- Object.assign(flattened, StyleSheet.flatten(entry));
30
- return flattened;
31
- }
32
- return style;
33
- },
34
-
35
- hairlineWidth: 1,
36
- absoluteFill: {position: 'absolute', top: 0, left: 0, right: 0, bottom: 0},
17
+ create(styles) {
18
+ return styles;
19
+ },
20
+ flatten(style) {
21
+ if (!style)
22
+ return {};
23
+ if (Array.isArray(style)) {
24
+ const flattened = {};
25
+ for (const entry of style)
26
+ Object.assign(flattened, StyleSheet.flatten(entry));
27
+ return flattened;
28
+ }
29
+ return style;
30
+ },
31
+ hairlineWidth: 1,
32
+ absoluteFill: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 },
37
33
  };
@@ -13,7 +13,6 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
16
  // TouchableOpacity — RN's press-to-dim wrapper, as a plain component over <Pressable>. No engine node
18
17
  // of its own:
19
18
  //
@@ -29,126 +28,91 @@
29
28
  // OPACITY GROUP, so the engine composites its entire subtree through an off-screen strip for as long
30
29
  // as the fade lasts. That is exactly what makes the label dim with its button — but it costs in
31
30
  // proportion to the dimmed area, so dim the box that reads as the button, not the page around it.
32
- import {
33
- createElement,
34
- forwardRef,
35
- useCallback,
36
- useEffect,
37
- useMemo,
38
- useRef,
39
- } from 'react';
40
- import {Pressable} from './Pressable.js';
41
- import {timing, useAnimatedValue} from './Animated.js';
42
- import {Easing} from './Easing.js';
43
- import {flattenStyleObj} from '../props.js';
44
-
31
+ import { createElement, forwardRef, useCallback, useEffect, useMemo, useRef, } from 'react';
32
+ import { Pressable } from './Pressable.mjs';
33
+ import { timing, useAnimatedValue } from './Animated.mjs';
34
+ import { Easing } from './Easing.mjs';
35
+ import { flattenStyleObj } from '../props.mjs';
45
36
  // RN's own numbers (Libraries/Components/Touchable/TouchableOpacity.js): the dim lands on touch-down
46
37
  // with no ramp at all and fades back over a quarter second once the finger lifts.
47
38
  const DEFAULT_ACTIVE_OPACITY = 0.2;
48
39
  const FULLY_OPAQUE = 1;
49
40
  const DIM_MS = 0;
50
41
  const RESTORE_MS = 250;
51
-
52
42
  let warnedAnimatedOpacity = false;
53
-
54
43
  /** The opacity the touchable rests at — whatever its style asks for, fully opaque if it asks nothing. */
55
44
  function restingOpacity(style) {
56
- const opacity = flattenStyleObj(style).opacity;
57
- if (opacity && opacity.__animated) {
58
- if (!warnedAnimatedOpacity) {
59
- warnedAnimatedOpacity = true;
60
- console.warn(
61
- 'embedded-react: <TouchableOpacity> ignores an animated `opacity` in its style — the press ' +
62
- 'feedback owns that property. To animate opacity yourself, use <Pressable>: the dim is just ' +
63
- 'onPressIn/onPressOut driving an Animated.Value.',
64
- );
45
+ const opacity = flattenStyleObj(style).opacity;
46
+ if (opacity && opacity.__animated) {
47
+ if (!warnedAnimatedOpacity) {
48
+ warnedAnimatedOpacity = true;
49
+ console.warn('embedded-react: <TouchableOpacity> ignores an animated `opacity` in its style — the press ' +
50
+ 'feedback owns that property. To animate opacity yourself, use <Pressable>: the dim is just ' +
51
+ 'onPressIn/onPressOut driving an Animated.Value.');
52
+ }
53
+ return FULLY_OPAQUE;
65
54
  }
66
- return FULLY_OPAQUE;
67
- }
68
- return typeof opacity === 'number' ? opacity : FULLY_OPAQUE;
55
+ return typeof opacity === 'number' ? opacity : FULLY_OPAQUE;
69
56
  }
70
-
71
57
  /** Runs `value` to `target` over `durationMs` on the native driver (a 0 ms ramp snaps, without taking a slot). */
72
58
  function fadeTo(value, target, durationMs) {
73
- timing(value, {
74
- toValue: target,
75
- duration: durationMs,
76
- easing: Easing.quadInOut,
77
- }).start();
59
+ timing(value, {
60
+ toValue: target,
61
+ duration: durationMs,
62
+ easing: Easing.quadInOut,
63
+ }).start();
78
64
  }
79
-
80
65
  /**
81
66
  * A press target that dims while it is held.
82
67
  *
83
68
  * Every <Pressable> prop works here unchanged — this IS a Pressable, plus the feedback — so the two are
84
69
  * interchangeable and the choice is only whether you want the dim.
85
70
  *
86
- * @param {object} props Pressable props, plus the two below.
87
- * @param {number} [props.activeOpacity] Opacity to dim to while held. Defaults to RN's 0.2.
88
- * @param {boolean} [props.disabled] Stops it responding to presses, and stops it dimming with them.
89
- * @returns {*} A <Pressable> whose opacity is bound to the press animation.
71
+ * `activeOpacity` is the opacity to dim to while held, RN's 0.2 by default. `disabled` stops it responding
72
+ * to presses, and stops it dimming with them.
90
73
  */
91
- export const TouchableOpacity = forwardRef(
92
- function TouchableOpacity(props, ref) {
93
- const {
94
- activeOpacity = DEFAULT_ACTIVE_OPACITY,
95
- disabled,
96
- style,
97
- onPress,
98
- onLongPress,
99
- onPressIn,
100
- onPressOut,
101
- ...rest
102
- } = props;
103
-
74
+ export const TouchableOpacity = forwardRef(function TouchableOpacity(props, ref) {
75
+ const { activeOpacity = DEFAULT_ACTIVE_OPACITY, disabled, style, onPress, onLongPress, onPressIn, onPressOut, ...rest } = props;
104
76
  // Both of these walk the style, so they are keyed to its identity rather than redone every render —
105
77
  // a StyleSheet entry (or any hoisted object) makes that a pointer compare. Memoizing the composed
106
78
  // array also keeps the reconciler's style diff a pointer compare instead of a deep walk.
107
79
  const resting = useMemo(() => restingOpacity(style), [style]);
108
80
  const opacity = useAnimatedValue(resting);
109
- const composedStyle = useMemo(() => [style, {opacity}], [style, opacity]);
110
-
81
+ const composedStyle = useMemo(() => [style, { opacity }], [style, opacity]);
111
82
  // Only read by the resting-opacity sync below: a press whose node unmounts mid-touch never sees its
112
83
  // press-out, but the animated value goes with the node, so nothing has to unwind.
113
84
  const pressed = useRef(false);
114
-
115
- const handlePressIn = useCallback(
116
- event => {
85
+ const handlePressIn = useCallback((event) => {
117
86
  pressed.current = true;
118
87
  fadeTo(opacity, activeOpacity, DIM_MS);
119
- if (onPressIn) onPressIn(event);
120
- },
121
- [opacity, activeOpacity, onPressIn],
122
- );
123
-
124
- const handlePressOut = useCallback(
125
- event => {
88
+ if (onPressIn)
89
+ onPressIn(event);
90
+ }, [opacity, activeOpacity, onPressIn]);
91
+ const handlePressOut = useCallback((event) => {
126
92
  pressed.current = false;
127
93
  fadeTo(opacity, resting, RESTORE_MS);
128
- if (onPressOut) onPressOut(event);
129
- },
130
- [opacity, resting, onPressOut],
131
- );
132
-
94
+ if (onPressOut)
95
+ onPressOut(event);
96
+ }, [opacity, resting, onPressOut]);
133
97
  // The binding owns the node's opacity from the first commit — buildProps no longer carries it — so a
134
98
  // style that CHANGES its opacity would otherwise never reach the node. Skipped while the finger is
135
99
  // down: the dim owns the value until the press-out, which already restores to the latest resting one.
136
100
  const synced = useRef(resting);
137
101
  useEffect(() => {
138
- if (synced.current === resting) return;
139
- synced.current = resting;
140
- if (!pressed.current) opacity.setValue(resting);
102
+ if (synced.current === resting)
103
+ return;
104
+ synced.current = resting;
105
+ if (!pressed.current)
106
+ opacity.setValue(resting);
141
107
  }, [opacity, resting]);
142
-
143
108
  return createElement(Pressable, {
144
- ...rest,
145
- ref,
146
- style: composedStyle,
147
- disabled,
148
- onPress,
149
- onLongPress,
150
- onPressIn: handlePressIn,
151
- onPressOut: handlePressOut,
109
+ ...rest,
110
+ ref,
111
+ style: composedStyle,
112
+ disabled,
113
+ onPress,
114
+ onLongPress,
115
+ onPressIn: handlePressIn,
116
+ onPressOut: handlePressOut,
152
117
  });
153
- },
154
- );
118
+ });