embedded-react 0.17.0 → 0.18.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/aot/compiler/animations.mjs +102 -34
- package/aot/compiler/assemble.mjs +27 -5
- package/aot/compiler/branch-merge.mjs +5 -9
- package/aot/compiler/c/animations.mjs +90 -16
- package/aot/compiler/c/nodes.mjs +20 -11
- package/aot/compiler/c/statements.mjs +59 -11
- package/aot/compiler/check.mjs +32 -3
- package/aot/compiler/collect.mjs +46 -2
- package/aot/compiler/control-flow.mjs +26 -14
- package/aot/compiler/diagnostics.mjs +33 -0
- package/aot/compiler/elements/dial.mjs +1 -3
- package/aot/compiler/elements/props.mjs +19 -9
- package/aot/compiler/elements/text-input.mjs +7 -2
- package/aot/compiler/expressions.mjs +29 -7
- package/aot/compiler/handlers.mjs +62 -24
- package/aot/compiler/keyboard.mjs +35 -21
- package/aot/compiler/lower.mjs +3 -3
- package/aot/compiler/nodes.mjs +16 -8
- package/aot/compiler/options.mjs +63 -12
- package/aot/compiler/static-eval.mjs +36 -4
- package/aot/compiler/style-text.mjs +35 -12
- package/aot/compiler/svg.mjs +7 -3
- package/aot/style-map.mjs +9 -11
- package/assets/bake-svg.mjs +3 -2
- package/package.json +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/Animated.js +41 -0
- package/src/embedded-react/Button.js +3 -4
- package/src/embedded-react/Pressable.js +59 -0
- package/src/embedded-react/TouchableOpacity.js +6 -9
- package/src/embedded-react/index.d.ts +118 -30
- package/src/embedded-react/index.js +1 -1
- package/src/embedded-react/named-colors.js +37 -0
- package/src/embedded-react/svg-ops.js +13 -16
- package/src/host-config.js +50 -8
|
@@ -20,13 +20,13 @@ import { attemptLowering, check } from './check.mjs';
|
|
|
20
20
|
import { BootReadError, bootEnvOf, bootSlotOf, foldBoot, isBoot, } from './boot.mjs';
|
|
21
21
|
import { lowerStyle, isStyleKey, STYLE_KEYS, DYN_FIELDS, colorLiteral, } from '../style-map.mjs';
|
|
22
22
|
import { parseColor } from '../../src/embedded-react/svg-ops.js';
|
|
23
|
-
import { aotError } from './diagnostics.mjs';
|
|
23
|
+
import { aotError, checkEngineString } from './diagnostics.mjs';
|
|
24
24
|
import { at, checkAnimatedStyleKey, checkAttributes, checkStyleKey, inlineFragments, TEXT_SPAN, } from './elements/props.mjs';
|
|
25
25
|
import { evalStatic, foldScope, withUndefined } from './static-eval.mjs';
|
|
26
26
|
import { cstr } from './c-syntax.mjs';
|
|
27
27
|
import { jsxChildText, lowerExpr, lowerText } from './expressions.mjs';
|
|
28
28
|
import { printPropValue } from './c/nodes.mjs';
|
|
29
|
-
import { ANIM_STYLE_PROPS, ANIM_TRANSFORM_PROPS, parseInterp, } from './animations.mjs';
|
|
29
|
+
import { ANIM_COLOR_KEYS, ANIM_STYLE_PROPS, ANIM_TRANSFORM_PROPS, parseInterp, } from './animations.mjs';
|
|
30
30
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
31
31
|
- Constants
|
|
32
32
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -74,6 +74,22 @@ export function argbLiteral(value) {
|
|
|
74
74
|
.toUpperCase() +
|
|
75
75
|
'u');
|
|
76
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Refuses an animated binding whose values do not fit the key: a color key takes only an interpolation onto
|
|
79
|
+
* colors, and every other key only numbers.
|
|
80
|
+
*
|
|
81
|
+
* @param key The style or transform key.
|
|
82
|
+
* @param interp The binding's interpolation, or undefined for the value itself.
|
|
83
|
+
* @param node The node to pin an error to.
|
|
84
|
+
*/
|
|
85
|
+
function checkAnimatedColor(key, interp, node) {
|
|
86
|
+
if (ANIM_COLOR_KEYS.has(key) && !interp?.colors) {
|
|
87
|
+
throw at(aotError(`AOT: an animated "${key}" needs colors`, "bind value.interpolate({inputRange: [0, 1], outputRange: ['#000', '#fff']}) rather than a number."), node);
|
|
88
|
+
}
|
|
89
|
+
if (!ANIM_COLOR_KEYS.has(key) && interp?.colors) {
|
|
90
|
+
throw at(aotError(`AOT: an animated "${key}" takes numbers, not colors`, 'an outputRange of colors drives backgroundColor or color.'), node);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
77
93
|
/**
|
|
78
94
|
* Lowers a dynamic (state-referencing) color expression to an ARGB value: a color, or a ternary of them.
|
|
79
95
|
*
|
|
@@ -134,7 +150,9 @@ export const emitColorExpr = (node, env) => printPropValue(lowerColorValue(node,
|
|
|
134
150
|
*/
|
|
135
151
|
export function lowerEnumValue(node, table, env) {
|
|
136
152
|
if (node.type === 'StringLiteral') {
|
|
137
|
-
const enumConstant = table
|
|
153
|
+
const enumConstant = Object.hasOwn(table, node.value)
|
|
154
|
+
? table[node.value]
|
|
155
|
+
: undefined;
|
|
138
156
|
if (!enumConstant) {
|
|
139
157
|
throw aotError(`AOT: unsupported enum value "${node.value}"`, `one of: ${Object.keys(table).join(', ')}`);
|
|
140
158
|
}
|
|
@@ -158,7 +176,7 @@ export function lowerEnumValue(node, table, env) {
|
|
|
158
176
|
// A statically resolvable enum (a const string) folds to its constant.
|
|
159
177
|
try {
|
|
160
178
|
const enumValue = evalStatic(node, foldScope(env, env.consts ?? {}));
|
|
161
|
-
if (typeof enumValue === 'string' && table
|
|
179
|
+
if (typeof enumValue === 'string' && Object.hasOwn(table, enumValue)) {
|
|
162
180
|
return { kind: 'constant', code: table[enumValue] };
|
|
163
181
|
}
|
|
164
182
|
}
|
|
@@ -453,6 +471,7 @@ export function collectStyleAssigns(openingElement, scope, env, spec, bootValues
|
|
|
453
471
|
if (!Object.hasOwn(ANIM_TRANSFORM_PROPS, transformKey)) {
|
|
454
472
|
throw at(aotError(`AOT: transform "${transformKey}" cannot be animated in Flow B`, `animated transforms: ${Object.keys(ANIM_TRANSFORM_PROPS).join(', ')}.`), transformProp);
|
|
455
473
|
}
|
|
474
|
+
checkAnimatedColor(transformKey, mapped?.interp, transformProp);
|
|
456
475
|
for (const animProp of ANIM_TRANSFORM_PROPS[transformKey]) {
|
|
457
476
|
transformBinds.push(mapped
|
|
458
477
|
? { cVar: mapped.cVar, prop: animProp, interp: mapped.interp }
|
|
@@ -501,6 +520,7 @@ export function collectStyleAssigns(openingElement, scope, env, spec, bootValues
|
|
|
501
520
|
const animVar = animRef(prop.value);
|
|
502
521
|
if (animVar && ANIM_STYLE_PROPS[key]) {
|
|
503
522
|
checkAnimatedStyleKey(spec, key, prop);
|
|
523
|
+
checkAnimatedColor(key, undefined, prop);
|
|
504
524
|
for (const animProp of ANIM_STYLE_PROPS[key]) {
|
|
505
525
|
binds.push({ cVar: animVar, prop: animProp });
|
|
506
526
|
}
|
|
@@ -509,6 +529,7 @@ export function collectStyleAssigns(openingElement, scope, env, spec, bootValues
|
|
|
509
529
|
const animInterp = animInterpRef(prop.value);
|
|
510
530
|
if (animInterp && ANIM_STYLE_PROPS[key]) {
|
|
511
531
|
checkAnimatedStyleKey(spec, key, prop);
|
|
532
|
+
checkAnimatedColor(key, animInterp.interp, prop);
|
|
512
533
|
for (const animProp of ANIM_STYLE_PROPS[key]) {
|
|
513
534
|
binds.push({
|
|
514
535
|
cVar: animInterp.cVar,
|
|
@@ -630,10 +651,7 @@ export function lowerNodeText(children, scope, env) {
|
|
|
630
651
|
const parts = [];
|
|
631
652
|
for (const child of inlineFragments(children)) {
|
|
632
653
|
if (child.type === 'JSXText') {
|
|
633
|
-
|
|
634
|
-
? child.value.replace(/\s+/g, ' ').trim()
|
|
635
|
-
: child.value;
|
|
636
|
-
parts.push({ kind: 'literal', text });
|
|
654
|
+
parts.push({ kind: 'literal', text: cleanJsxText(child.value) });
|
|
637
655
|
}
|
|
638
656
|
else if (child.type === 'JSXExpressionContainer') {
|
|
639
657
|
if (child.expression.type === 'JSXEmptyExpression')
|
|
@@ -666,7 +684,7 @@ const unsupportedTextChild = (child) => at(aotError(`AOT: a ${child.type} cannot
|
|
|
666
684
|
*
|
|
667
685
|
* @returns The text as React would render it.
|
|
668
686
|
*/
|
|
669
|
-
function cleanJsxText(value) {
|
|
687
|
+
export function cleanJsxText(value) {
|
|
670
688
|
// Find the last line with content: it is the only one not followed by a joining space.
|
|
671
689
|
const lines = value.split(/\r\n|\n|\r/);
|
|
672
690
|
let lastContentLine = 0;
|
|
@@ -748,13 +766,18 @@ export function collectTextSpans(children, scope, env) {
|
|
|
748
766
|
text_decoration: '0xFF',
|
|
749
767
|
letter_spacing: 'ER_LAYOUT_AUTO',
|
|
750
768
|
});
|
|
769
|
+
// A span's text is a C literal in a fixed engine buffer.
|
|
770
|
+
const spanText = (text) => {
|
|
771
|
+
checkEngineString(text, 'ER_SPAN_TEXT_MAX', 'a <Text> span');
|
|
772
|
+
return cstr(text);
|
|
773
|
+
};
|
|
751
774
|
// One span per child: text and static {…} values inherit the parent's style; a nested <Text> carries its own.
|
|
752
775
|
const spans = [];
|
|
753
776
|
for (const child of children) {
|
|
754
777
|
if (child.type === 'JSXText') {
|
|
755
778
|
const text = cleanJsxText(child.value);
|
|
756
779
|
if (text) {
|
|
757
|
-
spans.push(inheritSpan(
|
|
780
|
+
spans.push(inheritSpan(spanText(text)));
|
|
758
781
|
}
|
|
759
782
|
}
|
|
760
783
|
else if (child.type === 'JSXExpressionContainer') {
|
|
@@ -769,7 +792,7 @@ export function collectTextSpans(children, scope, env) {
|
|
|
769
792
|
}
|
|
770
793
|
// React draws nothing for null, undefined, or a boolean child.
|
|
771
794
|
if (value !== undefined && value !== null && typeof value !== 'boolean') {
|
|
772
|
-
spans.push(inheritSpan(
|
|
795
|
+
spans.push(inheritSpan(spanText(String(value))));
|
|
773
796
|
}
|
|
774
797
|
}
|
|
775
798
|
else if (child.type === 'JSXElement' &&
|
|
@@ -784,7 +807,7 @@ export function collectTextSpans(children, scope, env) {
|
|
|
784
807
|
const spanStyle = (fieldName, fallback) => staticAssigns.find(assign => assign.field === fieldName)?.expr ??
|
|
785
808
|
fallback;
|
|
786
809
|
spans.push({
|
|
787
|
-
text:
|
|
810
|
+
text: spanText(staticTextContent(child.children, scope)),
|
|
788
811
|
color: spanStyle('color', '0u'),
|
|
789
812
|
font_size: spanStyle('font_size', '0'),
|
|
790
813
|
font_weight: spanStyle('font_weight', '0xFF'),
|
package/aot/compiler/svg.mjs
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { check, checkNumber } from './check.mjs';
|
|
20
20
|
import { lowerStyle, NODE_TYPES } from '../style-map.mjs';
|
|
21
|
-
import { flattenSvg, parseColor, parsePath, KAPPA, PAINT_STRIDE, scaleVectorArtifact, } from '../../src/embedded-react/svg-ops.js';
|
|
21
|
+
import { flattenSvg, parseColor, parsePath, KAPPA, PAINT_PROPS, PAINT_STRIDE, scaleVectorArtifact, } from '../../src/embedded-react/svg-ops.js';
|
|
22
22
|
import { aotError } from './diagnostics.mjs';
|
|
23
23
|
import { evalStatic, withUndefined } from './static-eval.mjs';
|
|
24
24
|
import { lowerExpr } from './expressions.mjs';
|
|
@@ -1079,7 +1079,7 @@ function emitSvgSource(element, sourceAttr, scope, out, env) {
|
|
|
1079
1079
|
out.images.set(artifact.name, artifact.png);
|
|
1080
1080
|
}
|
|
1081
1081
|
const node = lowerSvgBox(`n${out.allocateNodeId()}`, NODE_TYPES.Image, svgBoxSize('width', width, true), svgBoxSize('height', height, true), element.openingElement, scope, env);
|
|
1082
|
-
node.image = { kind: 'static', name: artifact.name
|
|
1082
|
+
node.image = { kind: 'static', name: artifact.name };
|
|
1083
1083
|
return node;
|
|
1084
1084
|
}
|
|
1085
1085
|
// Scale the vector artifact to the box and bake its ops, paints and gradients into const tables.
|
|
@@ -1133,7 +1133,11 @@ function emitSvgDynamic(element, scope, out, env, state) {
|
|
|
1133
1133
|
continue;
|
|
1134
1134
|
locals.push(...shape.locals);
|
|
1135
1135
|
tape.push(tapeOp('ER_VOP_SHAPE'), tapeNumber(shapePaints.length), ...shape.entries);
|
|
1136
|
-
|
|
1136
|
+
// The root's paint cascades to each shape, as flattenSvg does.
|
|
1137
|
+
const paintAttrs = { ...attrs };
|
|
1138
|
+
for (const name of PAINT_PROPS)
|
|
1139
|
+
paintAttrs[name] ??= rootAttrs[name];
|
|
1140
|
+
shapePaints.push(lowerShapePaint(paintAttrs, env, scope));
|
|
1137
1141
|
}
|
|
1138
1142
|
// Allocate the node and this <Svg>'s table names.
|
|
1139
1143
|
const nodeId = `n${out.allocateNodeId()}`;
|
package/aot/style-map.mjs
CHANGED
|
@@ -13,18 +13,13 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
|
+
- Imports
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { NAMED_COLORS } from '../src/embedded-react/named-colors.js';
|
|
16
20
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
21
|
- Constants
|
|
18
22
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
-
/** A few CSS/RN named colors (extend as demos need them); everything else must be hex. */
|
|
20
|
-
const NAMED_COLORS = {
|
|
21
|
-
transparent: 0x00000000,
|
|
22
|
-
white: 0xffffffff,
|
|
23
|
-
black: 0xff000000,
|
|
24
|
-
red: 0xffff0000,
|
|
25
|
-
green: 0xff00ff00,
|
|
26
|
-
blue: 0xff0000ff,
|
|
27
|
-
};
|
|
28
23
|
// RN enum values → ER_* constants, shared by the static (KEYS) and state-driven (ENUM_FIELDS) lowerings.
|
|
29
24
|
const ALIGN = {
|
|
30
25
|
auto: 'ER_ALIGN_AUTO',
|
|
@@ -146,8 +141,9 @@ export function parseColorValue(color) {
|
|
|
146
141
|
}
|
|
147
142
|
// Accept a named color, else a hex string, expanding the 3-digit form to 6.
|
|
148
143
|
const normalized = color.trim().toLowerCase();
|
|
149
|
-
if (normalized
|
|
144
|
+
if (Object.hasOwn(NAMED_COLORS, normalized)) {
|
|
150
145
|
return NAMED_COLORS[normalized] >>> 0;
|
|
146
|
+
}
|
|
151
147
|
const hexMatch = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(normalized);
|
|
152
148
|
if (!hexMatch) {
|
|
153
149
|
throw new Error(`unsupported color "${color}" (use #rgb / #rrggbb / #rrggbbaa or a named color)`);
|
|
@@ -196,7 +192,9 @@ export function colorLiteral(color) {
|
|
|
196
192
|
* @returns A lookup that throws on a value the table does not have.
|
|
197
193
|
*/
|
|
198
194
|
const enumKey = (table, name) => (value) => {
|
|
199
|
-
const constant = table
|
|
195
|
+
const constant = Object.hasOwn(table, value)
|
|
196
|
+
? table[value]
|
|
197
|
+
: undefined;
|
|
200
198
|
if (!constant) {
|
|
201
199
|
throw new Error(`${name}: unsupported value "${value}" (one of ${Object.keys(table).join(', ')})`);
|
|
202
200
|
}
|
package/assets/bake-svg.mjs
CHANGED
|
@@ -303,11 +303,12 @@ function urlRef(v) {
|
|
|
303
303
|
return m ? m[1] : null;
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
-
/** A gradient coordinate: "50%" -> 0.5; a plain number passes through. */
|
|
306
|
+
/** A gradient coordinate: "50%" -> 0.5; a plain number passes through; NaN or infinite falls back to dflt. */
|
|
307
307
|
function gradCoord(v, dflt) {
|
|
308
308
|
if (v == null) return dflt;
|
|
309
309
|
const s = String(v).trim();
|
|
310
|
-
|
|
310
|
+
const n = s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s);
|
|
311
|
+
return Number.isFinite(n) ? n : dflt;
|
|
311
312
|
}
|
|
312
313
|
|
|
313
314
|
/** Local-space bounding box of a path-op array (no SHAPE header; MOVE/LINE/QUAD/CUBIC, CLOSE has no args). */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "embedded-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.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
|
|
@@ -21,6 +21,31 @@ import {createElement, useRef, useEffect} from 'react';
|
|
|
21
21
|
import {NativeUI} from '../native-ui.js';
|
|
22
22
|
import {deepEqualProps} from '../props.js';
|
|
23
23
|
|
|
24
|
+
// The style keys an animated value can drive only through an interpolation onto colors.
|
|
25
|
+
const COLOR_PROPS = new Set(['backgroundColor', 'color']);
|
|
26
|
+
|
|
27
|
+
let _warnedColorBind = false;
|
|
28
|
+
let _warnedNumberBind = false;
|
|
29
|
+
|
|
30
|
+
/** Warns once that `prop` takes a color, which a plain float or a numeric outputRange cannot give it. */
|
|
31
|
+
function warnColorBind(prop) {
|
|
32
|
+
if (_warnedColorBind) return;
|
|
33
|
+
_warnedColorBind = true;
|
|
34
|
+
console.warn(
|
|
35
|
+
`embedded-react: an animated "${prop}" needs colors: bind value.interpolate({inputRange, ` +
|
|
36
|
+
`outputRange: ['#000', '#fff']}) rather than the value itself, which is ignored.`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Warns once that `prop` takes numbers, which an outputRange holding anything else cannot give it. */
|
|
41
|
+
function warnNumberBind(prop) {
|
|
42
|
+
if (_warnedNumberBind) return;
|
|
43
|
+
_warnedNumberBind = true;
|
|
44
|
+
console.warn(
|
|
45
|
+
`embedded-react: an animated "${prop}" takes an outputRange of numbers; this one is ignored.`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
24
49
|
/** A standalone animatable value bound to an engine-side float. */
|
|
25
50
|
export class AnimatedValue {
|
|
26
51
|
constructor(initial = 0) {
|
|
@@ -53,6 +78,10 @@ export class AnimatedValue {
|
|
|
53
78
|
__bind(node, prop) {
|
|
54
79
|
// The engine applies the current value on bind and ignores duplicate (node, prop) pairs, so
|
|
55
80
|
// this is safe to call on every render.
|
|
81
|
+
if (COLOR_PROPS.has(prop)) {
|
|
82
|
+
warnColorBind(prop);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
56
85
|
NativeUI.animValueBind(this._handle, node, prop);
|
|
57
86
|
}
|
|
58
87
|
|
|
@@ -74,6 +103,18 @@ export class AnimatedInterpolation {
|
|
|
74
103
|
}
|
|
75
104
|
|
|
76
105
|
__bind(node, prop) {
|
|
106
|
+
const out = this._config?.outputRange;
|
|
107
|
+
if (Array.isArray(out)) {
|
|
108
|
+
if (COLOR_PROPS.has(prop)) {
|
|
109
|
+
if (out.some(c => typeof c !== 'string')) {
|
|
110
|
+
warnColorBind(prop);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
} else if (out.some(n => typeof n !== 'number')) {
|
|
114
|
+
warnNumberBind(prop);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
77
118
|
NativeUI.animValueBindInterpolated(
|
|
78
119
|
this._parent._handle,
|
|
79
120
|
node,
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
// the same: drop to <Pressable> + <Text> and style them yourself. `color` and `disabled` are the two
|
|
30
30
|
// knobs, exactly as upstream.
|
|
31
31
|
import {createElement} from 'react';
|
|
32
|
-
import {
|
|
32
|
+
import {Text} from './components.js';
|
|
33
|
+
import {Pressable} from './Pressable.js';
|
|
33
34
|
import {createPropWarner, RN_PLATFORM_NO_OPS} from './warn-props.js';
|
|
34
35
|
|
|
35
36
|
// RN's Button styles are Platform.select({ios, android}) and Platform.OS is 'embedded', so neither
|
|
@@ -85,9 +86,7 @@ export function Button(props = {}) {
|
|
|
85
86
|
{
|
|
86
87
|
style: buttonStyles,
|
|
87
88
|
disabled,
|
|
88
|
-
|
|
89
|
-
// in name only (the engine has no such node flag), so a node left holding onPress still fires.
|
|
90
|
-
onPress: disabled ? undefined : onPress,
|
|
89
|
+
onPress,
|
|
91
90
|
},
|
|
92
91
|
createElement(
|
|
93
92
|
Text,
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
// Pressable — RN's core press target, as a forwardRef component over the host <Pressable> element.
|
|
18
|
+
//
|
|
19
|
+
// Setting `disabled` withholds all press-related event handlers (`onPress`, `onLongPress`, `onPressIn`,
|
|
20
|
+
// `onPressOut`) so that NativeUI never registers callbacks on the native node. The C hit-testing
|
|
21
|
+
// 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
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A container that responds to touch interactions.
|
|
27
|
+
*
|
|
28
|
+
* When `disabled` is true, all press handlers are withheld so the node will not intercept touches
|
|
29
|
+
* 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
|
+
*/
|
|
40
|
+
export const Pressable = forwardRef(function Pressable(props = {}, ref) {
|
|
41
|
+
const {
|
|
42
|
+
disabled,
|
|
43
|
+
onPress,
|
|
44
|
+
onLongPress,
|
|
45
|
+
onPressIn,
|
|
46
|
+
onPressOut,
|
|
47
|
+
...rest
|
|
48
|
+
} = props;
|
|
49
|
+
|
|
50
|
+
return createElement(HostPressable, {
|
|
51
|
+
...rest,
|
|
52
|
+
ref,
|
|
53
|
+
disabled,
|
|
54
|
+
onPress: disabled ? undefined : onPress,
|
|
55
|
+
onLongPress: disabled ? undefined : onLongPress,
|
|
56
|
+
onPressIn: disabled ? undefined : onPressIn,
|
|
57
|
+
onPressOut: disabled ? undefined : onPressOut,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -37,13 +37,13 @@ import {
|
|
|
37
37
|
useMemo,
|
|
38
38
|
useRef,
|
|
39
39
|
} from 'react';
|
|
40
|
-
import {Pressable} from './
|
|
40
|
+
import {Pressable} from './Pressable.js';
|
|
41
41
|
import {timing, useAnimatedValue} from './Animated.js';
|
|
42
42
|
import {Easing} from './Easing.js';
|
|
43
43
|
import {flattenStyleObj} from '../props.js';
|
|
44
44
|
|
|
45
45
|
// RN's own numbers (Libraries/Components/Touchable/TouchableOpacity.js): the dim lands on touch-down
|
|
46
|
-
// with no ramp at all
|
|
46
|
+
// with no ramp at all and fades back over a quarter second once the finger lifts.
|
|
47
47
|
const DEFAULT_ACTIVE_OPACITY = 0.2;
|
|
48
48
|
const DIM_MS = 0;
|
|
49
49
|
const RESTORE_MS = 250;
|
|
@@ -140,13 +140,10 @@ export const TouchableOpacity = forwardRef(
|
|
|
140
140
|
ref,
|
|
141
141
|
style: composedStyle,
|
|
142
142
|
disabled,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
onLongPress: disabled ? undefined : onLongPress,
|
|
148
|
-
onPressIn: disabled ? undefined : handlePressIn,
|
|
149
|
-
onPressOut: disabled ? undefined : handlePressOut,
|
|
143
|
+
onPress,
|
|
144
|
+
onLongPress,
|
|
145
|
+
onPressIn: handlePressIn,
|
|
146
|
+
onPressOut: handlePressOut,
|
|
150
147
|
});
|
|
151
148
|
},
|
|
152
149
|
);
|