embedded-react 0.11.1 → 0.12.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 +19 -0
- package/aot/compile.mjs +48 -1
- package/aot/style-map.mjs +3 -0
- package/package.json +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/index.d.ts +3 -0
- package/src/props.js +9 -0
package/README.md
CHANGED
|
@@ -108,6 +108,25 @@ interpolation like `Hi {name}`, or nested `<Text>` runs) becomes the node's `tex
|
|
|
108
108
|
`Animated`, `Easing`, and the web timer globals (`setTimeout` / `setInterval`) are all available;
|
|
109
109
|
`useEffect` flushes via the host pump.
|
|
110
110
|
|
|
111
|
+
### Hiding a subtree (`display: 'none'`) — page caching
|
|
112
|
+
|
|
113
|
+
```jsx
|
|
114
|
+
<View style={{display: page === 'home' ? 'flex' : 'none'}}><Home /></View>
|
|
115
|
+
<View style={{display: page === 'settings' ? 'flex' : 'none'}}><Settings /></View>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
A hidden subtree drops out of layout, rendering, and hit-testing, but stays **mounted** — its React
|
|
119
|
+
state and effects survive, and the native nodes behind it keep their props. That is the difference
|
|
120
|
+
from the conditional render (`{page === 'home' && <Home/>}`) it replaces: a conditional unmounts the
|
|
121
|
+
page and rebuilds every node in interpreted QuickJS on the way back, which is the dominant cost of a
|
|
122
|
+
page change. Hiding costs a repaint, and a hidden page costs nothing per frame even while React keeps
|
|
123
|
+
rendering into it.
|
|
124
|
+
|
|
125
|
+
`visible={false}` is the same switch under the other spelling, with an explicit `style.display`
|
|
126
|
+
winning over it (`<Modal visible>` is unaffected — that is the Modal's own show/hide prop). Both work in Flow B too, where `display` can be static or
|
|
127
|
+
The trade is memory: a cached page holds its nodes for the life of the app, so cache
|
|
128
|
+
the pages you flip between, not every page.
|
|
129
|
+
|
|
111
130
|
## Build
|
|
112
131
|
|
|
113
132
|
```
|
package/aot/compile.mjs
CHANGED
|
@@ -1328,7 +1328,7 @@ function lowerDynamicStyleValue(key, valueNode, env) {
|
|
|
1328
1328
|
if (!meta)
|
|
1329
1329
|
throw aotError(
|
|
1330
1330
|
`AOT: a state-driven value for style "${key}" is not supported (static only)`,
|
|
1331
|
-
`state-driven styles supported: colors, opacity, sizes/margins/padding, and the layout enums (flexDirection, alignItems, alignSelf, justifyContent, position). Make "${key}" static, or drive the change another way.`,
|
|
1331
|
+
`state-driven styles supported: colors, opacity, sizes/margins/padding, and the layout enums (flexDirection, alignItems, alignSelf, justifyContent, position, display). Make "${key}" static, or drive the change another way.`,
|
|
1332
1332
|
);
|
|
1333
1333
|
if (meta.kind === 'color')
|
|
1334
1334
|
return [{field: meta.field, code: emitColorExpr(valueNode, env)}];
|
|
@@ -1449,6 +1449,7 @@ function collectStyleAssigns(openingElement, scope, env) {
|
|
|
1449
1449
|
if (attr.type !== 'JSXAttribute' || attr.name.name !== 'style') continue;
|
|
1450
1450
|
apply(attrExpr(attr));
|
|
1451
1451
|
}
|
|
1452
|
+
|
|
1452
1453
|
const staticAssigns = [];
|
|
1453
1454
|
const dynAssigns = [];
|
|
1454
1455
|
for (const [field, v] of fields)
|
|
@@ -3297,6 +3298,18 @@ function emitSvg(el, scope, out, env, state, opts) {
|
|
|
3297
3298
|
throw new Error(
|
|
3298
3299
|
'AOT: an <Svg> inside a dynamic conditional is not yet supported',
|
|
3299
3300
|
);
|
|
3301
|
+
// The vector emitter builds its box from static style assigns only, so it cannot honour `visible`
|
|
3302
|
+
// (nor a state-driven style `display`). Say so rather than dropping the prop — a silently inert
|
|
3303
|
+
// hide prop is exactly the trap this alias exists to remove.
|
|
3304
|
+
if (
|
|
3305
|
+
el.openingElement.attributes.some(
|
|
3306
|
+
a => a.type === 'JSXAttribute' && a.name && a.name.name === 'visible',
|
|
3307
|
+
)
|
|
3308
|
+
)
|
|
3309
|
+
throw aotError(
|
|
3310
|
+
'AOT: `visible` on an <Svg> is not supported',
|
|
3311
|
+
'wrap it: <View visible={…}><Svg …/></View> — the View carries the hide and the whole subtree goes with it.',
|
|
3312
|
+
);
|
|
3300
3313
|
const sourceAttr = el.openingElement.attributes.find(
|
|
3301
3314
|
a => a.type === 'JSXAttribute' && a.name && a.name.name === 'source',
|
|
3302
3315
|
);
|
|
@@ -4260,8 +4273,42 @@ function emitNodeImpl(el, scope, out, env, state, opts = {}) {
|
|
|
4260
4273
|
if (image?.tintColor)
|
|
4261
4274
|
staticAssigns.push({field: 'tint_color', expr: image.tintColor});
|
|
4262
4275
|
|
|
4276
|
+
// `visible` is the prop spelling of `display` (props.js does the same mapping in Flow A). An
|
|
4277
|
+
// explicit style `display` WINS, exactly as it does there, so the two spellings cannot disagree.
|
|
4278
|
+
// This lives here rather than in collectStyleAssigns because that helper is shared with <Modal> —
|
|
4279
|
+
// whose `visible` is its own show/hide prop, lowered to modal_visible by emitModal — and with the
|
|
4280
|
+
// typed components, which reject any prop they do not whitelist. All of those dispatch before this
|
|
4281
|
+
// point, so they are structurally out of reach.
|
|
4282
|
+
const visibleAttr = el.openingElement.attributes.find(
|
|
4283
|
+
a => a.type === 'JSXAttribute' && a.name && a.name.name === 'visible',
|
|
4284
|
+
);
|
|
4285
|
+
if (
|
|
4286
|
+
visibleAttr &&
|
|
4287
|
+
!staticAssigns.some(a => a.field === 'display') &&
|
|
4288
|
+
!dynAssigns.some(a => a.field === 'display')
|
|
4289
|
+
) {
|
|
4290
|
+
const expr = visibleAttr.value == null ? null : attrExpr(visibleAttr); // bare `visible` === true
|
|
4291
|
+
if (expr == null) {
|
|
4292
|
+
staticAssigns.push({field: 'display', expr: 'ER_DISPLAY_FLEX'});
|
|
4293
|
+
} else {
|
|
4294
|
+
try {
|
|
4295
|
+
staticAssigns.push({
|
|
4296
|
+
field: 'display',
|
|
4297
|
+
expr: evalStatic(expr, scope) ? 'ER_DISPLAY_FLEX' : 'ER_DISPLAY_NONE',
|
|
4298
|
+
});
|
|
4299
|
+
} catch {
|
|
4300
|
+
dynAssigns.push({
|
|
4301
|
+
field: 'display',
|
|
4302
|
+
code: `((${emitExpr(expr, env).code}) ? ER_DISPLAY_FLEX : ER_DISPLAY_NONE)`,
|
|
4303
|
+
});
|
|
4304
|
+
}
|
|
4305
|
+
}
|
|
4306
|
+
}
|
|
4307
|
+
|
|
4263
4308
|
// `displayCode` toggles show/hide for a state-driven conditional: the node is always built, its
|
|
4264
4309
|
// `display` flips between flex and none in app_update (joining any state-driven style assigns).
|
|
4310
|
+
// Pushed last so an enclosing conditional beats the element's own visible/display — app_update
|
|
4311
|
+
// applies staticAssigns before dynAssigns, so a dynamic toggle also beats a static one.
|
|
4265
4312
|
if (opts.displayCode)
|
|
4266
4313
|
dynAssigns.push({
|
|
4267
4314
|
field: 'display',
|
package/aot/style-map.mjs
CHANGED
|
@@ -100,6 +100,7 @@ const FLEX_DIRECTION = {
|
|
|
100
100
|
'column-reverse': 'ER_FLEX_COL_REVERSE',
|
|
101
101
|
};
|
|
102
102
|
const POSITION = {relative: 'ER_POS_RELATIVE', absolute: 'ER_POS_ABSOLUTE'};
|
|
103
|
+
const DISPLAY = {flex: 'ER_DISPLAY_FLEX', none: 'ER_DISPLAY_NONE'};
|
|
103
104
|
|
|
104
105
|
const enumKey = (table, name) => v => {
|
|
105
106
|
const c = table[v];
|
|
@@ -183,6 +184,7 @@ const KEYS = {
|
|
|
183
184
|
],
|
|
184
185
|
// Positioning: `position: 'absolute'` takes a node out of flow; left/top/right/bottom are its anchors.
|
|
185
186
|
position: v => [{field: 'position', expr: enumKey(POSITION, 'position')(v)}],
|
|
187
|
+
display: v => [{field: 'display', expr: enumKey(DISPLAY, 'display')(v)}],
|
|
186
188
|
left: v => [{field: 'left', expr: dim(v)}],
|
|
187
189
|
top: v => [{field: 'top', expr: dim(v)}],
|
|
188
190
|
right: v => [{field: 'right', expr: dim(v)}],
|
|
@@ -305,6 +307,7 @@ const ENUM_FIELDS = {
|
|
|
305
307
|
alignSelf: {field: 'align_self', table: ALIGN},
|
|
306
308
|
justifyContent: {field: 'justify_content', table: JUSTIFY},
|
|
307
309
|
position: {field: 'position', table: POSITION},
|
|
310
|
+
display: {field: 'display', table: DISPLAY},
|
|
308
311
|
};
|
|
309
312
|
|
|
310
313
|
export const DYN_FIELDS = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "embedded-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "React Native-style component package + reconciler that drives the embedded-react C engine through the QuickJS NativeUI bridge (Flow A).",
|
|
6
6
|
"license": "Apache-2.0",
|
package/sim/embedded-react.wasm
CHANGED
|
Binary file
|
|
@@ -54,6 +54,7 @@ export interface ViewStyle {
|
|
|
54
54
|
borderColor?: string;
|
|
55
55
|
opacity?: number;
|
|
56
56
|
transform?: TransformStyle[];
|
|
57
|
+
display?: 'flex' | 'none';
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
export interface TextStyle extends ViewStyle {
|
|
@@ -78,6 +79,7 @@ export type StyleProp<T> = T | false | null | undefined | StyleProp<T>[];
|
|
|
78
79
|
export interface ViewProps {
|
|
79
80
|
style?: StyleProp<ViewStyle>;
|
|
80
81
|
children?: ReactNode;
|
|
82
|
+
visible?: boolean;
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
export interface TextProps {
|
|
@@ -91,6 +93,7 @@ export interface PressableProps {
|
|
|
91
93
|
children?: ReactNode;
|
|
92
94
|
onPress?: () => void;
|
|
93
95
|
disabled?: boolean;
|
|
96
|
+
visible?: boolean;
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
export type ImageSource = number | string | {uri: string};
|
package/src/props.js
CHANGED
|
@@ -168,6 +168,15 @@ export function buildProps(type, props, flatStyle) {
|
|
|
168
168
|
const name = resolveImageSource(props.source);
|
|
169
169
|
if (name != null) flat.imageName = name;
|
|
170
170
|
}
|
|
171
|
+
// `visible` is the Modal's own show/hide prop (it maps to ERProps.modal_visible). On every other
|
|
172
|
+
// component it used to be silently inert; it now means the same thing as `display`, so a subtree can
|
|
173
|
+
// be pruned from layout, raster and hit-testing without unmounting it. An explicit style `display`
|
|
174
|
+
// wins, so the two spellings can't disagree.
|
|
175
|
+
if (type !== 'Modal' && flat.visible !== undefined) {
|
|
176
|
+
if (flat.display === undefined)
|
|
177
|
+
flat.display = flat.visible ? 'flex' : 'none';
|
|
178
|
+
delete flat.visible;
|
|
179
|
+
}
|
|
171
180
|
// <Text> content: flatten string/number/nested-<Text> children into the node's full-text string.
|
|
172
181
|
// The engine renders this when no spans are set; with spans (buildTextSpans) it carries the same
|
|
173
182
|
// text for the plain-text fallback. Non-flattenable children fall back to mounted child instances.
|