embedded-react 0.1.1 → 0.2.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.
@@ -1,196 +1,196 @@
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
-
17
- // react-reconciler host config: translates React's mutation API into NativeUI.* calls.
18
- //
19
- // Instances ARE the integer node handles returned by NativeUI.createNode(). We keep no JS-side
20
- // wrapper objects — the engine owns the scene graph, the handle is the identity.
21
- import { DefaultEventPriority } from 'react-reconciler/constants';
22
- import { NativeUI } from './native-ui.js';
23
- import { buildProps, buildTextSpans, isEventProp, isTextContent } from './props.js';
24
- import { flattenSvg } from './embedded-react/svg-ops.js';
25
- import { splitAnimatedStyle } from './embedded-react/split-style.js';
26
-
27
- /**
28
- * Applies a node's resolved props, binding any Animated.Value found in its `style` to the matching
29
- * node prop (native driver). This makes animated styles work on ANY host element — `<Pressable
30
- * style={{ transform: [{ scale: v }] }}>` binds without an Animated.* wrapper — which is what the
31
- * Flow B AOT compiler does too, so the two render paths stay in parity. An Animated.* wrapper has
32
- * already stripped its bindings into a ref, so splitAnimatedStyle finds none here (no double bind).
33
- */
34
- function applyProps(type, handle, props) {
35
- const { staticStyle, bindings } = splitAnimatedStyle(props.style);
36
- NativeUI.setProps(handle, buildProps(type, bindings.length ? { ...props, style: staticStyle } : props));
37
- for (const b of bindings) b.value.__bind(handle, b.prop);
38
- }
39
-
40
- /**
41
- * Applies inline-styled text spans for a <Text> node (no-op for other types). buildTextSpans returns
42
- * [] for uniform text, which reverts the node to plain-text rendering — so this also clears stale
43
- * spans when a Text changes from styled-runs to a single style across renders.
44
- */
45
- function applyTextSpans(type, handle, props) {
46
- if (type === 'Text') NativeUI.setTextSpans(handle, buildTextSpans(props));
47
- }
48
-
49
- /**
50
- * Compiles an <Svg>'s declarative children (Path/Circle/G/...) into the node's vector op-tape. Like
51
- * text spans, the Svg owns its subtree — React does not mount the shape children (see
52
- * shouldSetTextContent), so we flatten props.children here on create and every update.
53
- */
54
- function applyVectorOps(type, handle, props) {
55
- if (type !== 'Svg') return;
56
- const { ops, paints } = flattenSvg(props);
57
- NativeUI.setVectorOps(handle, ops, paints);
58
- }
59
-
60
- /**
61
- * Registers/clears on* event handlers. A handler present in old but not new props is cleared.
62
- */
63
- function applyEvents(handle, prevProps, nextProps) {
64
- if (prevProps) {
65
- for (const key in prevProps) {
66
- if (isEventProp(key, prevProps[key]) && !(nextProps && isEventProp(key, nextProps[key]))) {
67
- NativeUI.setEvent(handle, key, null);
68
- }
69
- }
70
- }
71
- for (const key in nextProps) {
72
- if (isEventProp(key, nextProps[key])) {
73
- NativeUI.setEvent(handle, key, nextProps[key]);
74
- }
75
- }
76
- }
77
-
78
- export const hostConfig = {
79
- supportsMutation: true,
80
- supportsPersistence: false,
81
- supportsHydration: false,
82
- isPrimaryRenderer: true,
83
- noTimeout: -1,
84
- warnsIfNotActing: false,
85
-
86
- // --- Context (we carry none) ---
87
- getRootHostContext() {
88
- return {};
89
- },
90
- getChildHostContext(parentContext) {
91
- return parentContext;
92
- },
93
- getPublicInstance(instance) {
94
- return instance;
95
- },
96
-
97
- // --- Commit lifecycle ---
98
- prepareForCommit() {
99
- return null;
100
- },
101
- resetAfterCommit() {
102
- NativeUI.commit();
103
- },
104
-
105
- // --- Creation ---
106
- createInstance(type, props) {
107
- const handle = NativeUI.createNode(type);
108
- applyProps(type, handle, props);
109
- applyTextSpans(type, handle, props);
110
- applyVectorOps(type, handle, props);
111
- applyEvents(handle, null, props);
112
- return handle;
113
- },
114
- createTextInstance(text) {
115
- // Raw text is only legal inside <Text> (handled via shouldSetTextContent). This fallback
116
- // wraps stray text in a Text node so it still renders rather than crashing.
117
- const handle = NativeUI.createNode('Text');
118
- NativeUI.setProps(handle, { text: String(text) });
119
- return handle;
120
- },
121
- appendInitialChild(parent, child) {
122
- NativeUI.appendChild(parent, child);
123
- },
124
- finalizeInitialChildren() {
125
- return false;
126
- },
127
- shouldSetTextContent(type, props) {
128
- // Own the whole subtree for any flattenable <Text> (strings, interpolation, nested <Text>): React
129
- // skips mounting children and we render them via the node's text + spans. Non-flattenable content
130
- // (e.g. a <View> inside <Text>) returns false, falling back to mounted child instances.
131
- // <Svg> also owns its subtree: the shape children are flattened into the vector op-tape
132
- // (applyVectorOps), never mounted as host nodes.
133
- return type === 'Svg' || (type === 'Text' && isTextContent(props.children));
134
- },
135
-
136
- // --- Mutation ---
137
- appendChild(parent, child) {
138
- NativeUI.appendChild(parent, child);
139
- },
140
- appendChildToContainer(container, child) {
141
- NativeUI.appendChild(container, child);
142
- },
143
- insertBefore(parent, child, beforeChild) {
144
- NativeUI.insertBefore(parent, child, beforeChild);
145
- },
146
- insertInContainerBefore(container, child, beforeChild) {
147
- NativeUI.insertBefore(container, child, beforeChild);
148
- },
149
- removeChild(parent, child) {
150
- NativeUI.removeChild(parent, child);
151
- NativeUI.destroyNode(child);
152
- },
153
- removeChildFromContainer(container, child) {
154
- NativeUI.removeChild(container, child);
155
- NativeUI.destroyNode(child);
156
- },
157
- clearContainer() {
158
- // Children are removed individually via removeChildFromContainer.
159
- },
160
- prepareUpdate() {
161
- // Always re-apply: NativeUI.setProps is fully declarative, so a non-null payload is enough.
162
- return true;
163
- },
164
- commitUpdate(instance, _payload, type, prevProps, nextProps) {
165
- applyProps(type, instance, nextProps);
166
- applyTextSpans(type, instance, nextProps);
167
- applyVectorOps(type, instance, nextProps);
168
- applyEvents(instance, prevProps, nextProps);
169
- },
170
- commitTextUpdate(textInstance, _oldText, newText) {
171
- NativeUI.setProps(textInstance, { text: String(newText) });
172
- },
173
-
174
- // --- Misc required hooks (no-ops for our renderer) ---
175
- detachDeletedInstance() {},
176
- getCurrentEventPriority() {
177
- return DefaultEventPriority;
178
- },
179
- getInstanceFromNode() {
180
- return null;
181
- },
182
- beforeActiveInstanceBlur() {},
183
- afterActiveInstanceBlur() {},
184
- prepareScopeUpdate() {},
185
- getInstanceFromScope() {
186
- return null;
187
- },
188
-
189
- // --- Scheduling ---
190
- scheduleTimeout: (fn, delay) => setTimeout(fn, delay),
191
- cancelTimeout: (id) => clearTimeout(id),
192
- supportsMicrotasks: true,
193
- scheduleMicrotask:
194
- typeof queueMicrotask === 'function' ? queueMicrotask : (fn) => Promise.resolve().then(fn),
195
- now: () => NativeUI.now(),
196
- };
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
+
17
+ // react-reconciler host config: translates React's mutation API into NativeUI.* calls.
18
+ //
19
+ // Instances ARE the integer node handles returned by NativeUI.createNode(). We keep no JS-side
20
+ // wrapper objects — the engine owns the scene graph, the handle is the identity.
21
+ import { DefaultEventPriority } from 'react-reconciler/constants';
22
+ import { NativeUI } from './native-ui.js';
23
+ import { buildProps, buildTextSpans, isEventProp, isTextContent } from './props.js';
24
+ import { flattenSvg } from './embedded-react/svg-ops.js';
25
+ import { splitAnimatedStyle } from './embedded-react/split-style.js';
26
+
27
+ /**
28
+ * Applies a node's resolved props, binding any Animated.Value found in its `style` to the matching
29
+ * node prop (native driver). This makes animated styles work on ANY host element — `<Pressable
30
+ * style={{ transform: [{ scale: v }] }}>` binds without an Animated.* wrapper — which is what the
31
+ * Flow B AOT compiler does too, so the two render paths stay in parity. An Animated.* wrapper has
32
+ * already stripped its bindings into a ref, so splitAnimatedStyle finds none here (no double bind).
33
+ */
34
+ function applyProps(type, handle, props) {
35
+ const { staticStyle, bindings } = splitAnimatedStyle(props.style);
36
+ NativeUI.setProps(handle, buildProps(type, bindings.length ? { ...props, style: staticStyle } : props));
37
+ for (const b of bindings) b.value.__bind(handle, b.prop);
38
+ }
39
+
40
+ /**
41
+ * Applies inline-styled text spans for a <Text> node (no-op for other types). buildTextSpans returns
42
+ * [] for uniform text, which reverts the node to plain-text rendering — so this also clears stale
43
+ * spans when a Text changes from styled-runs to a single style across renders.
44
+ */
45
+ function applyTextSpans(type, handle, props) {
46
+ if (type === 'Text') NativeUI.setTextSpans(handle, buildTextSpans(props));
47
+ }
48
+
49
+ /**
50
+ * Compiles an <Svg>'s declarative children (Path/Circle/G/...) into the node's vector op-tape. Like
51
+ * text spans, the Svg owns its subtree — React does not mount the shape children (see
52
+ * shouldSetTextContent), so we flatten props.children here on create and every update.
53
+ */
54
+ function applyVectorOps(type, handle, props) {
55
+ if (type !== 'Svg') return;
56
+ const { ops, paints } = flattenSvg(props);
57
+ NativeUI.setVectorOps(handle, ops, paints);
58
+ }
59
+
60
+ /**
61
+ * Registers/clears on* event handlers. A handler present in old but not new props is cleared.
62
+ */
63
+ function applyEvents(handle, prevProps, nextProps) {
64
+ if (prevProps) {
65
+ for (const key in prevProps) {
66
+ if (isEventProp(key, prevProps[key]) && !(nextProps && isEventProp(key, nextProps[key]))) {
67
+ NativeUI.setEvent(handle, key, null);
68
+ }
69
+ }
70
+ }
71
+ for (const key in nextProps) {
72
+ if (isEventProp(key, nextProps[key])) {
73
+ NativeUI.setEvent(handle, key, nextProps[key]);
74
+ }
75
+ }
76
+ }
77
+
78
+ export const hostConfig = {
79
+ supportsMutation: true,
80
+ supportsPersistence: false,
81
+ supportsHydration: false,
82
+ isPrimaryRenderer: true,
83
+ noTimeout: -1,
84
+ warnsIfNotActing: false,
85
+
86
+ // --- Context (we carry none) ---
87
+ getRootHostContext() {
88
+ return {};
89
+ },
90
+ getChildHostContext(parentContext) {
91
+ return parentContext;
92
+ },
93
+ getPublicInstance(instance) {
94
+ return instance;
95
+ },
96
+
97
+ // --- Commit lifecycle ---
98
+ prepareForCommit() {
99
+ return null;
100
+ },
101
+ resetAfterCommit() {
102
+ NativeUI.commit();
103
+ },
104
+
105
+ // --- Creation ---
106
+ createInstance(type, props) {
107
+ const handle = NativeUI.createNode(type);
108
+ applyProps(type, handle, props);
109
+ applyTextSpans(type, handle, props);
110
+ applyVectorOps(type, handle, props);
111
+ applyEvents(handle, null, props);
112
+ return handle;
113
+ },
114
+ createTextInstance(text) {
115
+ // Raw text is only legal inside <Text> (handled via shouldSetTextContent). This fallback
116
+ // wraps stray text in a Text node so it still renders rather than crashing.
117
+ const handle = NativeUI.createNode('Text');
118
+ NativeUI.setProps(handle, { text: String(text) });
119
+ return handle;
120
+ },
121
+ appendInitialChild(parent, child) {
122
+ NativeUI.appendChild(parent, child);
123
+ },
124
+ finalizeInitialChildren() {
125
+ return false;
126
+ },
127
+ shouldSetTextContent(type, props) {
128
+ // Own the whole subtree for any flattenable <Text> (strings, interpolation, nested <Text>): React
129
+ // skips mounting children and we render them via the node's text + spans. Non-flattenable content
130
+ // (e.g. a <View> inside <Text>) returns false, falling back to mounted child instances.
131
+ // <Svg> also owns its subtree: the shape children are flattened into the vector op-tape
132
+ // (applyVectorOps), never mounted as host nodes.
133
+ return type === 'Svg' || (type === 'Text' && isTextContent(props.children));
134
+ },
135
+
136
+ // --- Mutation ---
137
+ appendChild(parent, child) {
138
+ NativeUI.appendChild(parent, child);
139
+ },
140
+ appendChildToContainer(container, child) {
141
+ NativeUI.appendChild(container, child);
142
+ },
143
+ insertBefore(parent, child, beforeChild) {
144
+ NativeUI.insertBefore(parent, child, beforeChild);
145
+ },
146
+ insertInContainerBefore(container, child, beforeChild) {
147
+ NativeUI.insertBefore(container, child, beforeChild);
148
+ },
149
+ removeChild(parent, child) {
150
+ NativeUI.removeChild(parent, child);
151
+ NativeUI.destroyNode(child);
152
+ },
153
+ removeChildFromContainer(container, child) {
154
+ NativeUI.removeChild(container, child);
155
+ NativeUI.destroyNode(child);
156
+ },
157
+ clearContainer() {
158
+ // Children are removed individually via removeChildFromContainer.
159
+ },
160
+ prepareUpdate() {
161
+ // Always re-apply: NativeUI.setProps is fully declarative, so a non-null payload is enough.
162
+ return true;
163
+ },
164
+ commitUpdate(instance, _payload, type, prevProps, nextProps) {
165
+ applyProps(type, instance, nextProps);
166
+ applyTextSpans(type, instance, nextProps);
167
+ applyVectorOps(type, instance, nextProps);
168
+ applyEvents(instance, prevProps, nextProps);
169
+ },
170
+ commitTextUpdate(textInstance, _oldText, newText) {
171
+ NativeUI.setProps(textInstance, { text: String(newText) });
172
+ },
173
+
174
+ // --- Misc required hooks (no-ops for our renderer) ---
175
+ detachDeletedInstance() {},
176
+ getCurrentEventPriority() {
177
+ return DefaultEventPriority;
178
+ },
179
+ getInstanceFromNode() {
180
+ return null;
181
+ },
182
+ beforeActiveInstanceBlur() {},
183
+ afterActiveInstanceBlur() {},
184
+ prepareScopeUpdate() {},
185
+ getInstanceFromScope() {
186
+ return null;
187
+ },
188
+
189
+ // --- Scheduling ---
190
+ scheduleTimeout: (fn, delay) => setTimeout(fn, delay),
191
+ cancelTimeout: (id) => clearTimeout(id),
192
+ supportsMicrotasks: true,
193
+ scheduleMicrotask:
194
+ typeof queueMicrotask === 'function' ? queueMicrotask : (fn) => Promise.resolve().then(fn),
195
+ now: () => NativeUI.now(),
196
+ };
package/src/native-ui.js CHANGED
@@ -1,24 +1,24 @@
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
-
17
- // The QuickJS C bridge (native_ui_bridge.c) installs a global `NativeUI` object before the
18
- // bundle runs. Re-export it so the rest of the JS imports it cleanly instead of touching the
19
- // global directly.
20
- export const NativeUI = globalThis.NativeUI;
21
-
22
- if (!NativeUI) {
23
- throw new Error('NativeUI global is missing — the QuickJS bridge must be installed before the bundle runs.');
24
- }
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
+
17
+ // The QuickJS C bridge (native_ui_bridge.c) installs a global `NativeUI` object before the
18
+ // bundle runs. Re-export it so the rest of the JS imports it cleanly instead of touching the
19
+ // global directly.
20
+ export const NativeUI = globalThis.NativeUI;
21
+
22
+ if (!NativeUI) {
23
+ throw new Error('NativeUI global is missing — the QuickJS bridge must be installed before the bundle runs.');
24
+ }