embedded-react 0.19.0 → 0.21.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/NOTICE +5 -0
- package/README.md +8 -3
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +180 -90
- package/aot/compiler/animations.mjs +568 -317
- package/aot/compiler/boot.mjs +97 -35
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +70 -18
- package/aot/compiler/collect.mjs +787 -288
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +439 -268
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +52 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +61 -26
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +1685 -392
- package/aot/compiler/handlers.mjs +2029 -547
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +43 -4
- package/aot/compiler/ir/reads.mjs +223 -22
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +26 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +246 -111
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +195 -172
- package/aot/compiler/options.mjs +37 -31
- package/aot/compiler/out.mjs +32 -23
- package/aot/compiler/package/animations.mjs +779 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +175 -0
- package/aot/compiler/package/bytecode.mjs +315 -0
- package/aot/compiler/package/checks.mjs +1026 -0
- package/aot/compiler/package/codegen.mjs +2168 -0
- package/aot/compiler/package/enums.mjs +148 -0
- package/aot/compiler/package/format.mjs +771 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +2103 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +127 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +686 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +23 -21
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/static-eval.mjs +340 -30
- package/aot/compiler/style-text.mjs +178 -174
- package/aot/compiler/svg.mjs +480 -251
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +320 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +152 -92
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +11 -7
- package/assets/emit-c.mjs +77 -58
- package/assets/emit-container.mjs +123 -37
- package/assets/emit-pack.mjs +77 -67
- package/assets/font-config.mjs +9 -6
- package/assets/font-sizes.mjs +163 -123
- package/assets/glyph-coverage.mjs +74 -54
- package/assets/index.mjs +31 -9
- package/assets/rasterize.mjs +126 -99
- package/assets/svg-loader.mjs +8 -8
- package/build.mjs +23 -13
- package/cli.mjs +402 -172
- package/compile-bin.mjs +3 -3
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +62 -64
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +50 -37
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +37 -21
- package/package.json +7 -8
- package/persist-transform.mjs +34 -28
- package/qjsc-wasm.mjs +25 -14
- package/sim/embedded-react.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +94 -85
- package/sim-server.mjs +113 -94
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Dimensions.js +37 -0
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +3 -3
- package/src/embedded-react/LayoutAnimation.js +8 -5
- package/src/embedded-react/PanResponder.js +75 -71
- package/src/embedded-react/Pressable.js +2 -8
- package/src/embedded-react/SectionList.js +7 -9
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +88 -8
- package/src/embedded-react/index.js +2 -0
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/nativeModule.js +462 -0
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
|
@@ -56,7 +56,7 @@ export const constantText = (text) => text.parts.every(part => part.kind === 'li
|
|
|
56
56
|
? text.parts.map(part => part.text).join('')
|
|
57
57
|
: null;
|
|
58
58
|
/**
|
|
59
|
-
* Refuses a constant string too long for the engine buffer it is copied into, which
|
|
59
|
+
* Refuses a constant string too long for the engine buffer it is copied into, which would cut it short.
|
|
60
60
|
*
|
|
61
61
|
* @param text The string, or null for one built at run time (checked when it is written).
|
|
62
62
|
* @param limit The er_scene.h define that sizes the buffer.
|
|
@@ -87,35 +87,35 @@ export function aotError(message, hint) {
|
|
|
87
87
|
return error;
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
|
-
* Pins an AOT error that has no location yet to `
|
|
90
|
+
* Pins an AOT error that has no location yet to `location`, as a located emitter does.
|
|
91
91
|
*
|
|
92
92
|
* @param error The thrown value.
|
|
93
|
-
* @param
|
|
93
|
+
* @param location Where the construct being lowered is, if it is anywhere.
|
|
94
94
|
*/
|
|
95
|
-
export function locateError(error,
|
|
95
|
+
export function locateError(error, location) {
|
|
96
96
|
if (error &&
|
|
97
97
|
typeof error.message === 'string' &&
|
|
98
98
|
error.message.startsWith('AOT:') &&
|
|
99
99
|
!error.aotLoc &&
|
|
100
|
-
|
|
101
|
-
error.aotLoc =
|
|
100
|
+
location) {
|
|
101
|
+
error.aotLoc = location;
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
105
|
* Wraps an emit function so a thrown AOT error (without a location yet) is tagged with the current node's
|
|
106
106
|
* loc. Wrapped emitters nest, so the deepest node that failed is the one reported. The expression, node, and
|
|
107
|
-
* handler emitters are all wrapped, and
|
|
107
|
+
* handler emitters are all wrapped, and compilePackage formats the result with formatAotError. While it runs,
|
|
108
108
|
* currentLoc() reports the same location, for a check recorded now and run later.
|
|
109
109
|
*
|
|
110
|
-
* @param
|
|
110
|
+
* @param lower The emit function; its first parameter is the node it lowers.
|
|
111
111
|
*
|
|
112
112
|
* @returns The same function, with its AOT errors located.
|
|
113
113
|
*/
|
|
114
|
-
export function withLoc(
|
|
114
|
+
export function withLoc(lower) {
|
|
115
115
|
return function (node, ...rest) {
|
|
116
116
|
loweringAt.push(node?.loc?.start);
|
|
117
117
|
try {
|
|
118
|
-
return
|
|
118
|
+
return lower(node, ...rest);
|
|
119
119
|
}
|
|
120
120
|
catch (error) {
|
|
121
121
|
locateError(error, node?.loc?.start); // babel loc: { line (1-based), column (0-based) }
|
|
@@ -128,38 +128,35 @@ export function withLoc(fn) {
|
|
|
128
128
|
}
|
|
129
129
|
/**
|
|
130
130
|
* Rebuilds an AOT error with `file:line:col`, a code-frame, any hint, and the screen size this build folded
|
|
131
|
-
* its layout at (from the resolved `
|
|
131
|
+
* its layout at (from the resolved `options`), all folded into the message, for compilePackage to throw.
|
|
132
132
|
*
|
|
133
|
-
* @param error The error
|
|
134
|
-
* @param
|
|
133
|
+
* @param error The error compilePackage caught.
|
|
134
|
+
* @param source The app's source, for the code-frame.
|
|
135
135
|
* @param filename The source's path, as the message should show it.
|
|
136
|
-
* @param
|
|
136
|
+
* @param options The compile's resolved options, for the screen footer.
|
|
137
137
|
*
|
|
138
138
|
* @returns The located error; `error` itself when it has no location.
|
|
139
139
|
*/
|
|
140
|
-
export function formatAotError(error,
|
|
140
|
+
export function formatAotError(error, source, filename, options) {
|
|
141
141
|
if (!error || !error.aotLoc)
|
|
142
142
|
return error; // nothing to locate — leave the bare message
|
|
143
143
|
// Render the code-frame around the error's location, and the hint when there is one.
|
|
144
144
|
const { line, column } = error.aotLoc;
|
|
145
|
-
const
|
|
145
|
+
const frameLocation = { start: { line, column: column + 1 } }; // code-frame columns are 1-based
|
|
146
146
|
let codeFrame = '';
|
|
147
147
|
try {
|
|
148
|
-
codeFrame = codeFrameColumns(
|
|
148
|
+
codeFrame = codeFrameColumns(source, frameLocation, { highlightCode: false });
|
|
149
149
|
}
|
|
150
150
|
catch {
|
|
151
151
|
/* code-frame is best-effort */
|
|
152
152
|
}
|
|
153
153
|
// Name the screen size the layout was folded at: it picks which branch of a responsive app the error came from.
|
|
154
154
|
const hint = error.aotHint ? `\n\nhint: ${error.aotHint}` : '';
|
|
155
|
-
const size = `${
|
|
156
|
-
const screen =
|
|
157
|
-
? `\n\nscreen: ${size}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
: `\n\nscreen: ${size} — ER_AOT_SCREEN_W/H did not supply both dimensions, so the ` +
|
|
161
|
-
`default filled in. A responsive app picks its layout from \`screen\`, so this may be compiling a ` +
|
|
162
|
-
`branch meant for another board.`;
|
|
155
|
+
const size = `${options.screen.width}×${options.screen.height}`;
|
|
156
|
+
const screen = options.screenFrom === 'search'
|
|
157
|
+
? `\n\nscreen: this is the layout a ${size} panel picks. Every layout has to compile; name the panels with ` +
|
|
158
|
+
`--screen to drop the others.`
|
|
159
|
+
: `\n\nscreen: ${size}.`;
|
|
163
160
|
// Build the located error, keeping the location and hint for any caller that inspects them.
|
|
164
161
|
const located = new Error(`${error.message}\n at ${filename}:${line}:${column + 1}\n\n${codeFrame}${hint}${screen}`);
|
|
165
162
|
located.aotLoc = error.aotLoc;
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
-
import {
|
|
20
|
-
import { aotError } from '../diagnostics.mjs';
|
|
19
|
+
import { parseColorValue } from '../../style-map.mjs';
|
|
20
|
+
import { aotError, currentLoc } from '../diagnostics.mjs';
|
|
21
21
|
import { evalStatic } from '../static-eval.mjs';
|
|
22
22
|
import { lowerExpr } from '../expressions.mjs';
|
|
23
23
|
import { attrExpr, lowerColorValue, styleWrites, collectStyleAssigns, } from '../style-text.mjs';
|
|
@@ -45,48 +45,48 @@ const SIZE_LARGE = 36;
|
|
|
45
45
|
*
|
|
46
46
|
* @param element The JSX element.
|
|
47
47
|
* @param scope The current scope.
|
|
48
|
-
* @param
|
|
49
|
-
* @param
|
|
48
|
+
* @param output The current output.
|
|
49
|
+
* @param environment The current environment.
|
|
50
50
|
*
|
|
51
51
|
* @returns The name of the node variable (Node ID).
|
|
52
52
|
*/
|
|
53
|
-
export function emitActivityIndicator(element, scope,
|
|
53
|
+
export function emitActivityIndicator(element, scope, output, environment) {
|
|
54
54
|
// get node id
|
|
55
|
-
const nodeId = `n${
|
|
55
|
+
const nodeId = `n${output.allocateNodeId()}`;
|
|
56
56
|
// collect style assignments
|
|
57
|
-
const { staticAssigns,
|
|
58
|
-
const hasField = (field) => styleWrites(staticAssigns,
|
|
57
|
+
const { staticAssigns, dynamicAssigns, binds } = collectStyleAssigns(element.openingElement, scope, environment, elementSpec('ActivityIndicator'));
|
|
58
|
+
const hasField = (field) => styleWrites(staticAssigns, dynamicAssigns, field);
|
|
59
59
|
// `style.color` tints the spinner, as in Flow A; a `color` prop written after it wins.
|
|
60
|
-
for (const assign of [...staticAssigns, ...
|
|
60
|
+
for (const assign of [...staticAssigns, ...dynamicAssigns]) {
|
|
61
61
|
if (assign.field === 'color') {
|
|
62
62
|
assign.field = 'indicator_color';
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
// process element attributes
|
|
66
66
|
let size = SIZE_LARGE;
|
|
67
|
-
for (const
|
|
67
|
+
for (const attribute of element.openingElement.attributes) {
|
|
68
68
|
// Ensure all props are explicit named attributes.
|
|
69
69
|
// Spread props cannot be analyzed statically
|
|
70
|
-
if (
|
|
70
|
+
if (attribute.type !== 'JSXAttribute') {
|
|
71
71
|
throw aotError('AOT: spread props on <ActivityIndicator> are not supported');
|
|
72
72
|
}
|
|
73
73
|
// skip non-props
|
|
74
|
-
const name =
|
|
74
|
+
const name = attribute.name.name;
|
|
75
75
|
if (name === 'style' || name === 'ref' || name === 'key')
|
|
76
76
|
continue;
|
|
77
77
|
// process props
|
|
78
|
-
const node = attrExpr(
|
|
78
|
+
const node = attrExpr(attribute);
|
|
79
79
|
if (name === 'color') {
|
|
80
80
|
try {
|
|
81
81
|
staticAssigns.push({
|
|
82
82
|
field: 'indicator_color',
|
|
83
|
-
|
|
83
|
+
value: parseColorValue(String(evalStatic(node, scope))),
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
86
|
catch {
|
|
87
|
-
|
|
87
|
+
dynamicAssigns.push({
|
|
88
88
|
field: 'indicator_color',
|
|
89
|
-
value: lowerColorValue(node,
|
|
89
|
+
value: lowerColorValue(node, environment),
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
}
|
|
@@ -103,22 +103,22 @@ export function emitActivityIndicator(element, scope, out, env) {
|
|
|
103
103
|
? Math.round(sizeValue)
|
|
104
104
|
: NaN;
|
|
105
105
|
if (Number.isNaN(size)) {
|
|
106
|
-
throw at(aotError(`AOT: <ActivityIndicator size> must be 'small', 'large' or a number of pixels (got ${JSON.stringify(sizeValue) ?? String(sizeValue)})`),
|
|
106
|
+
throw at(aotError(`AOT: <ActivityIndicator size> must be 'small', 'large' or a number of pixels (got ${JSON.stringify(sizeValue) ?? String(sizeValue)})`), attribute);
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
else if (name === 'animating') {
|
|
110
110
|
try {
|
|
111
111
|
staticAssigns.push({
|
|
112
112
|
field: 'animating',
|
|
113
|
-
|
|
113
|
+
value: evalStatic(node, scope) ? 1 : 0,
|
|
114
114
|
});
|
|
115
115
|
}
|
|
116
116
|
catch {
|
|
117
|
-
|
|
117
|
+
dynamicAssigns.push({
|
|
118
118
|
field: 'animating',
|
|
119
119
|
value: {
|
|
120
120
|
kind: 'flag',
|
|
121
|
-
test: lowerExpr(node,
|
|
121
|
+
test: lowerExpr(node, environment),
|
|
122
122
|
truthiness: 'value',
|
|
123
123
|
negate: false,
|
|
124
124
|
whenTrue: 1,
|
|
@@ -132,16 +132,17 @@ export function emitActivityIndicator(element, scope, out, env) {
|
|
|
132
132
|
}
|
|
133
133
|
// default size: large unless style sets width/height
|
|
134
134
|
if (!hasField('width')) {
|
|
135
|
-
staticAssigns.push({ field: 'width',
|
|
135
|
+
staticAssigns.push({ field: 'width', value: size });
|
|
136
136
|
}
|
|
137
137
|
if (!hasField('height')) {
|
|
138
|
-
staticAssigns.push({ field: 'height',
|
|
138
|
+
staticAssigns.push({ field: 'height', value: size });
|
|
139
139
|
}
|
|
140
140
|
const node = {
|
|
141
141
|
id: nodeId,
|
|
142
|
+
location: currentLoc(),
|
|
142
143
|
type: 'ER_NODE_ACTIVITY_INDICATOR',
|
|
143
144
|
constantProps: staticAssigns,
|
|
144
|
-
stateProps:
|
|
145
|
+
stateProps: dynamicAssigns,
|
|
145
146
|
placeholder: null,
|
|
146
147
|
text: null,
|
|
147
148
|
image: null,
|
|
@@ -149,7 +150,7 @@ export function emitActivityIndicator(element, scope, out, env) {
|
|
|
149
150
|
children: [],
|
|
150
151
|
wiring: [
|
|
151
152
|
...binds.map((bind, index) => ({ kind: 'bind', bind, index })),
|
|
152
|
-
...lowerRefBinds(element.openingElement,
|
|
153
|
+
...lowerRefBinds(element.openingElement, environment).map(slot => ({
|
|
153
154
|
kind: 'ref',
|
|
154
155
|
slot,
|
|
155
156
|
})),
|
|
@@ -0,0 +1,182 @@
|
|
|
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
|
+
- Imports
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { evalStatic, evalStaticOrThrow, withUndefined } from '../static-eval.mjs';
|
|
20
|
+
import { attrExpr } from '../style-text.mjs';
|
|
21
|
+
import { emitNode } from '../nodes.mjs';
|
|
22
|
+
import { attributeOf, elementOf, expressionChild, literalObject, refuseSpreads, } from './rewrite.mjs';
|
|
23
|
+
import { at } from './props.mjs';
|
|
24
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
25
|
+
- Constants
|
|
26
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
27
|
+
// Button.js's styles. Flow B has no textAlign, so the Pressable centres the label instead.
|
|
28
|
+
const BUTTON_STYLE = {
|
|
29
|
+
backgroundColor: '#2196F3',
|
|
30
|
+
borderRadius: 2,
|
|
31
|
+
alignItems: 'center',
|
|
32
|
+
};
|
|
33
|
+
const BUTTON_DISABLED_STYLE = { backgroundColor: '#dfdfdf' };
|
|
34
|
+
const TEXT_STYLE = { color: '#ffffff', padding: 8, fontWeight: '500' };
|
|
35
|
+
const TEXT_DISABLED_STYLE = { color: '#a1a1a1' };
|
|
36
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
37
|
+
- Implementation
|
|
38
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
39
|
+
/**
|
|
40
|
+
* <Button title onPress color disabled /> → the tree Button.js renders: a styled <Pressable> around one <Text>.
|
|
41
|
+
*
|
|
42
|
+
* @param element The <Button>, its props already checked.
|
|
43
|
+
* @param scope Compile-time constants in reach.
|
|
44
|
+
* @param output Everything emitted so far.
|
|
45
|
+
* @param environment The expression environment.
|
|
46
|
+
* @param state The component's state table.
|
|
47
|
+
* @param options How the element is placed.
|
|
48
|
+
*
|
|
49
|
+
* @returns What the <Pressable> lowers to.
|
|
50
|
+
*/
|
|
51
|
+
export function emitButton(element, scope, output, environment, state, options) {
|
|
52
|
+
refuseSpreads(element, 'Button');
|
|
53
|
+
const attributes = new Map();
|
|
54
|
+
for (const attribute of element.openingElement.attributes) {
|
|
55
|
+
attributes.set(attribute.name.name, attribute);
|
|
56
|
+
}
|
|
57
|
+
// `disabled` picks the styles and drops the press handler, so it must fold.
|
|
58
|
+
const disabledAttribute = attributes.get('disabled');
|
|
59
|
+
let disabled = false;
|
|
60
|
+
if (disabledAttribute) {
|
|
61
|
+
try {
|
|
62
|
+
disabled = !!evalStaticOrThrow(attrExpr(disabledAttribute), scope, 'AOT: <Button disabled> must be a compile-time constant', 'it picks the grey styles and drops onPress at build time. To gate on state, leave it ' +
|
|
63
|
+
'enabled and return early in the handler, or build the <Pressable> + <Text> yourself.');
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw at(error, disabledAttribute);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// A color that folds to nothing keeps the default blue, as `if (color)` does in Flow A.
|
|
70
|
+
const buttonStyles = [literalObject(BUTTON_STYLE)];
|
|
71
|
+
const textStyles = [literalObject(TEXT_STYLE)];
|
|
72
|
+
const colorAttribute = attributes.get('color');
|
|
73
|
+
if (colorAttribute && !disabled && !foldsFalsy(colorAttribute, scope)) {
|
|
74
|
+
buttonStyles.push({
|
|
75
|
+
type: 'ObjectExpression',
|
|
76
|
+
properties: [
|
|
77
|
+
{
|
|
78
|
+
type: 'ObjectProperty',
|
|
79
|
+
key: { type: 'Identifier', name: 'backgroundColor' },
|
|
80
|
+
value: attrExpr(colorAttribute),
|
|
81
|
+
computed: false,
|
|
82
|
+
shorthand: false,
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (disabled) {
|
|
88
|
+
buttonStyles.push(literalObject(BUTTON_DISABLED_STYLE));
|
|
89
|
+
textStyles.push(literalObject(TEXT_DISABLED_STYLE));
|
|
90
|
+
}
|
|
91
|
+
// The label, coerced as Flow A coerces it: a missing title draws nothing.
|
|
92
|
+
const titleAttribute = attributes.get('title');
|
|
93
|
+
const label = titleAttribute
|
|
94
|
+
? [
|
|
95
|
+
expressionChild(titleText(attrExpr(titleAttribute), scope)),
|
|
96
|
+
]
|
|
97
|
+
: [];
|
|
98
|
+
const text = elementOf('Text', [attributeOf('style', { type: 'ArrayExpression', elements: textStyles })], label, element);
|
|
99
|
+
const pressableAttributes = [
|
|
100
|
+
attributeOf('style', { type: 'ArrayExpression', elements: buttonStyles }),
|
|
101
|
+
];
|
|
102
|
+
const onPress = attributes.get('onPress');
|
|
103
|
+
if (onPress && !disabled) {
|
|
104
|
+
pressableAttributes.push(onPress);
|
|
105
|
+
}
|
|
106
|
+
return emitNode(elementOf('Pressable', pressableAttributes, [text], element), scope, output, environment, state, options);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A title as Button.js draws it, `title == null ? '' : String(title)`: worked out now when it folds, else
|
|
110
|
+
* `String(title)`, so a boolean draws as "true" or "false" rather than nothing. No value worked out at run time
|
|
111
|
+
* is null, so only a ternary's branches can be, and each is worked out on its own.
|
|
112
|
+
*
|
|
113
|
+
* @param title The title.
|
|
114
|
+
* @param scope Compile-time constants in reach.
|
|
115
|
+
*
|
|
116
|
+
* @returns The label's text.
|
|
117
|
+
*/
|
|
118
|
+
function titleText(title, scope) {
|
|
119
|
+
const folded = foldedTitle(title, scope);
|
|
120
|
+
if (folded)
|
|
121
|
+
return folded;
|
|
122
|
+
if (title.type === 'ConditionalExpression')
|
|
123
|
+
return titleBranches(title, scope);
|
|
124
|
+
return {
|
|
125
|
+
type: 'CallExpression',
|
|
126
|
+
callee: { type: 'Identifier', name: 'String' },
|
|
127
|
+
arguments: [title],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A ternary title with each branch that folds as its text, `null` and `undefined` as nothing. A branch worked
|
|
132
|
+
* out at run time stays as written, since text lowers no call inside a ternary.
|
|
133
|
+
*
|
|
134
|
+
* @param title The ternary.
|
|
135
|
+
* @param scope Compile-time constants in reach.
|
|
136
|
+
*
|
|
137
|
+
* @returns The ternary of texts.
|
|
138
|
+
*/
|
|
139
|
+
function titleBranches(title, scope) {
|
|
140
|
+
const branch = (expression) => foldedTitle(expression, scope) ??
|
|
141
|
+
(expression.type === 'ConditionalExpression'
|
|
142
|
+
? titleBranches(expression, scope)
|
|
143
|
+
: expression);
|
|
144
|
+
return {
|
|
145
|
+
...title,
|
|
146
|
+
consequent: branch(title.consequent),
|
|
147
|
+
alternate: branch(title.alternate),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* A title that folds, as its text.
|
|
152
|
+
*
|
|
153
|
+
* @param title The title.
|
|
154
|
+
* @param scope Compile-time constants in reach.
|
|
155
|
+
*
|
|
156
|
+
* @returns The text, or null when it is worked out at run time.
|
|
157
|
+
*/
|
|
158
|
+
function foldedTitle(title, scope) {
|
|
159
|
+
try {
|
|
160
|
+
const value = evalStatic(title, withUndefined(scope));
|
|
161
|
+
return { type: 'StringLiteral', value: value == null ? '' : String(value) };
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Whether an attribute folds to a falsy value. One that does not fold is a value worked out at run time.
|
|
169
|
+
*
|
|
170
|
+
* @param attribute The attribute.
|
|
171
|
+
* @param scope Compile-time constants in reach.
|
|
172
|
+
*
|
|
173
|
+
* @returns Whether it folds falsy.
|
|
174
|
+
*/
|
|
175
|
+
function foldsFalsy(attribute, scope) {
|
|
176
|
+
try {
|
|
177
|
+
return !evalStatic(attrExpr(attribute), scope);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|