embedded-react 0.20.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 (140) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +3 -2
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +17 -47
  5. package/aot/compiler/alerts.mjs +604 -0
  6. package/aot/compiler/animations.mjs +299 -57
  7. package/aot/compiler/boot.mjs +27 -2
  8. package/aot/compiler/check.mjs +62 -10
  9. package/aot/compiler/collect.mjs +517 -137
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +276 -117
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/elements/button.mjs +182 -0
  14. package/aot/compiler/elements/image-background.mjs +76 -0
  15. package/aot/compiler/elements/image.mjs +2 -1
  16. package/aot/compiler/elements/props.mjs +47 -8
  17. package/aot/compiler/elements/rewrite.mjs +118 -0
  18. package/aot/compiler/elements/section-list.mjs +370 -0
  19. package/aot/compiler/expressions.mjs +1378 -97
  20. package/aot/compiler/handlers.mjs +1807 -354
  21. package/aot/compiler/helper-values.mjs +312 -0
  22. package/aot/compiler/ir/nodes.mjs +40 -1
  23. package/aot/compiler/ir/reads.mjs +206 -5
  24. package/aot/compiler/ir/text.mjs +5 -1
  25. package/aot/compiler/ir/types.mjs +12 -0
  26. package/aot/compiler/kept-states.mjs +177 -0
  27. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  28. package/aot/compiler/keyboard.mjs +1 -1
  29. package/aot/compiler/list-items.mjs +226 -0
  30. package/aot/compiler/list-order.mjs +586 -0
  31. package/aot/compiler/list-split.mjs +259 -0
  32. package/aot/compiler/loops.mjs +582 -0
  33. package/aot/compiler/lower.mjs +203 -53
  34. package/aot/compiler/native-files.mjs +439 -0
  35. package/aot/compiler/native-header.mjs +335 -0
  36. package/aot/compiler/native-modules.mjs +1173 -0
  37. package/aot/compiler/nodes.mjs +45 -31
  38. package/aot/compiler/options.mjs +3 -2
  39. package/aot/compiler/out.mjs +18 -1
  40. package/aot/compiler/package/animations.mjs +133 -23
  41. package/aot/compiler/package/assemble.mjs +2 -4
  42. package/aot/compiler/package/bytecode.mjs +37 -8
  43. package/aot/compiler/package/checks.mjs +4 -0
  44. package/aot/compiler/package/codegen.mjs +906 -33
  45. package/aot/compiler/package/enums.mjs +10 -0
  46. package/aot/compiler/package/format.mjs +16 -3
  47. package/aot/compiler/package/lower.mjs +707 -176
  48. package/aot/compiler/package/string-caps.mjs +515 -0
  49. package/aot/compiler/package/tables.mjs +24 -0
  50. package/aot/compiler/package/writer.mjs +22 -0
  51. package/aot/compiler/parse.mjs +3 -1
  52. package/aot/compiler/record-shapes.mjs +246 -0
  53. package/aot/compiler/records.mjs +654 -0
  54. package/aot/compiler/reducers.mjs +272 -0
  55. package/aot/compiler/scroll-to.mjs +112 -0
  56. package/aot/compiler/static-eval.mjs +315 -7
  57. package/aot/compiler/style-text.mjs +20 -5
  58. package/aot/compiler/svg.mjs +13 -7
  59. package/aot/compiler/switch-cases.mjs +198 -0
  60. package/aot/compiler/tagged-records.mjs +450 -0
  61. package/aot/compiler/typing.mjs +307 -0
  62. package/aot/compiler/updaters.mjs +321 -0
  63. package/aot/compiler/window-dimensions.mjs +94 -0
  64. package/aot/compiler/worked-values.mjs +319 -0
  65. package/aot/demo-file.mjs +30 -0
  66. package/aot/style-map.mjs +94 -3
  67. package/assets/bake-font.mjs +187 -244
  68. package/assets/bake-image.mjs +47 -71
  69. package/assets/bake-svg.mjs +465 -533
  70. package/assets/build-builtin-font.mjs +14 -27
  71. package/assets/emit-c.mjs +124 -204
  72. package/assets/emit-container.mjs +129 -197
  73. package/assets/emit-pack.mjs +92 -136
  74. package/assets/font-config.mjs +19 -27
  75. package/assets/font-sizes.mjs +432 -507
  76. package/assets/glyph-coverage.mjs +170 -214
  77. package/assets/index.mjs +52 -97
  78. package/assets/rasterize.mjs +173 -194
  79. package/assets/svg-loader.mjs +42 -48
  80. package/build.mjs +85 -94
  81. package/cli.mjs +490 -586
  82. package/compile-bin.mjs +24 -36
  83. package/flash.mjs +297 -401
  84. package/hotreload/dev-device.mjs +229 -270
  85. package/hotreload/pack-app.mjs +118 -159
  86. package/hotreload/pack-split.mjs +146 -213
  87. package/hotreload/package-transfer.mjs +128 -131
  88. package/hotreload/protocol.mjs +118 -118
  89. package/hotreload/uploader.mjs +132 -159
  90. package/hotreload/watch-tree.mjs +56 -65
  91. package/pack-container.mjs +94 -116
  92. package/package.json +19 -16
  93. package/persist-transform.mjs +89 -105
  94. package/qjsc-wasm.mjs +47 -72
  95. package/sim/embedded-react.wasm +0 -0
  96. package/sim/index.html +1 -1
  97. package/sim-server.mjs +260 -354
  98. package/src/embedded-react/Alert.mjs +112 -0
  99. package/src/embedded-react/Animated.mjs +412 -0
  100. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  101. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  102. package/src/embedded-react/Dimensions.mjs +32 -0
  103. package/src/embedded-react/Easing.mjs +34 -0
  104. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  105. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  106. package/src/embedded-react/Keyboard.mjs +82 -0
  107. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  108. package/src/embedded-react/PanResponder.mjs +256 -0
  109. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  110. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  111. package/src/embedded-react/SectionList.mjs +103 -0
  112. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  113. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  114. package/src/embedded-react/background-image.mjs +321 -0
  115. package/src/embedded-react/{components.js → components.mjs} +1 -3
  116. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  117. package/src/embedded-react/index.d.ts +238 -5
  118. package/src/embedded-react/index.js +26 -17
  119. package/src/embedded-react/layout-anim-config.mjs +85 -0
  120. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  121. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  122. package/src/embedded-react/nativeModule.mjs +371 -0
  123. package/src/embedded-react/split-style.mjs +57 -0
  124. package/src/embedded-react/svg-ops.mjs +1075 -0
  125. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  126. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  127. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  128. package/src/host-config.mjs +512 -0
  129. package/src/{native-ui.js → native-ui.mjs} +1 -8
  130. package/src/props.mjs +341 -0
  131. package/src/{renderer.js → renderer.mjs} +30 -44
  132. package/src/embedded-react/Animated.js +0 -440
  133. package/src/embedded-react/Easing.js +0 -39
  134. package/src/embedded-react/PanResponder.js +0 -330
  135. package/src/embedded-react/SectionList.js +0 -148
  136. package/src/embedded-react/layout-anim-config.js +0 -101
  137. package/src/embedded-react/split-style.js +0 -58
  138. package/src/embedded-react/svg-ops.js +0 -1295
  139. package/src/host-config.js +0 -548
  140. package/src/props.js +0 -306
@@ -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
  // Button — RN's pre-styled button, as a plain component over <Pressable> + <Text>. No engine node of
18
17
  // its own:
19
18
  //
@@ -28,70 +27,47 @@
28
27
  // Like RN's, this Button takes NO style prop — that is the whole point of it, and the escape hatch is
29
28
  // the same: drop to <Pressable> + <Text> and style them yourself. `color` and `disabled` are the two
30
29
  // knobs, exactly as upstream.
31
- import {createElement} from 'react';
32
- import {Text} from './components.js';
33
- import {Pressable} from './Pressable.js';
34
- import {createPropWarner, RN_PLATFORM_NO_OPS} from './warn-props.js';
35
-
30
+ import { createElement } from 'react';
31
+ import { Text } from './components.mjs';
32
+ import { Pressable } from './Pressable.mjs';
33
+ import { createPropWarner, RN_PLATFORM_NO_OPS } from './warn-props.mjs';
36
34
  // RN's Button styles are Platform.select({ios, android}) and Platform.OS is 'embedded', so neither
37
35
  // branch applies. These are the android ones — a filled, rounded button — because a panel button
38
36
  // should read as a button before it is styled, and the ios branch is bare text. RN's `elevation: 4`
39
37
  // is dropped: shadows are compile-gated (ERUI_SHADOWS) and off in every board config here, so it
40
38
  // would be a prop marshaled on every commit to paint nothing.
41
39
  const styles = {
42
- button: {backgroundColor: '#2196F3', borderRadius: 2},
43
- buttonDisabled: {backgroundColor: '#dfdfdf'},
44
- text: {color: '#ffffff', textAlign: 'center', padding: 8, fontWeight: '500'},
45
- textDisabled: {color: '#a1a1a1'},
40
+ button: { backgroundColor: '#2196F3', borderRadius: 2 },
41
+ buttonDisabled: { backgroundColor: '#dfdfdf' },
42
+ text: { color: '#ffffff', textAlign: 'center', padding: 8, fontWeight: '500' },
43
+ textDisabled: { color: '#a1a1a1' },
46
44
  };
47
-
48
45
  /** The props that do something here. RN's rest are OS services with nothing to wrap on an MCU. */
49
46
  const SUPPORTED = ['title', 'onPress', 'color', 'disabled'];
50
-
51
- const warnUnsupportedProps = createPropWarner(
52
- 'Button',
53
- SUPPORTED,
54
- "it is RN's fixed-style button — for a button you style yourself, use <Pressable> + <Text> " +
55
- '(the same tree this renders).',
56
- RN_PLATFORM_NO_OPS,
57
- );
58
-
47
+ const warnUnsupportedProps = createPropWarner('Button', SUPPORTED, "it is RN's fixed-style button — for a button you style yourself, use <Pressable> + <Text> " +
48
+ '(the same tree this renders).', RN_PLATFORM_NO_OPS);
59
49
  /**
60
50
  * A pre-styled press target with a centred label.
61
51
  *
62
- * @param {object} [props] Missing / empty renders an unlabelled button, rather than throwing on a
63
- * device where a render-time throw takes the whole app down (RN `invariant`s on a missing title).
64
- * @param {string} [props.title] The label. Coerced to a string; not upper-cased (RN does that on
65
- * Android, and an upper-cased glyph an app never wrote is a glyph the font bake never baked).
66
- * @param {(event: object) => void} [props.onPress] Press handler. Not attached while `disabled`.
67
- * @param {string} [props.color] Fill colour, replacing the default blue.
68
- * @param {boolean} [props.disabled] Greys the button out and stops it responding to presses.
69
- * @returns {*} A <Pressable> wrapping a single <Text>.
52
+ * Missing or empty props render an unlabelled button, rather than throwing on a device where a
53
+ * render-time throw takes the whole app down (RN `invariant`s on a missing title). The title is coerced
54
+ * to a string and not upper-cased (RN does that on Android, and an upper-cased glyph an app never wrote
55
+ * is a glyph the font bake never baked). `onPress` is not attached while `disabled`.
70
56
  */
71
57
  export function Button(props = {}) {
72
- const {title, onPress, color, disabled} = props;
73
-
74
- warnUnsupportedProps(props);
75
-
76
- const buttonStyles = [styles.button];
77
- const textStyles = [styles.text];
78
- if (color) buttonStyles.push({backgroundColor: color});
79
- if (disabled) {
80
- buttonStyles.push(styles.buttonDisabled);
81
- textStyles.push(styles.textDisabled);
82
- }
83
-
84
- return createElement(
85
- Pressable,
86
- {
87
- style: buttonStyles,
88
- disabled,
89
- onPress,
90
- },
91
- createElement(
92
- Text,
93
- {style: textStyles},
94
- title === undefined || title === null ? '' : String(title),
95
- ),
96
- );
58
+ const { title, onPress, color, disabled } = props;
59
+ warnUnsupportedProps(props);
60
+ const buttonStyles = [styles.button];
61
+ const textStyles = [styles.text];
62
+ if (color)
63
+ buttonStyles.push({ backgroundColor: color });
64
+ if (disabled) {
65
+ buttonStyles.push(styles.buttonDisabled);
66
+ textStyles.push(styles.textDisabled);
67
+ }
68
+ return createElement(Pressable, {
69
+ style: buttonStyles,
70
+ disabled,
71
+ onPress,
72
+ }, createElement(Text, { style: textStyles }, title === undefined || title === null ? '' : String(title)));
97
73
  }
@@ -0,0 +1,32 @@
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
+ export const Dimensions = {
17
+ get(dimension) {
18
+ if (dimension !== 'window' && dimension !== 'screen') {
19
+ throw new Error(`Dimensions.get: no dimension named "${dimension}"`);
20
+ }
21
+ return {
22
+ width: screen.width,
23
+ height: screen.height,
24
+ scale: 1,
25
+ fontScale: 1,
26
+ };
27
+ },
28
+ };
29
+ // The panel's size. It is fixed while the app runs, so this never re-renders.
30
+ export function useWindowDimensions() {
31
+ return Dimensions.get('window');
32
+ }
@@ -0,0 +1,34 @@
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
+ export const Easing = {
17
+ linear: 'linear',
18
+ ease: 'ease',
19
+ easeIn: 'easeIn',
20
+ easeOut: 'easeOut',
21
+ easeInOut: 'easeInOut',
22
+ quadIn: 'quadIn',
23
+ quadOut: 'quadOut',
24
+ quadInOut: 'quadInOut',
25
+ cubicIn: 'cubicIn',
26
+ cubicOut: 'cubicOut',
27
+ cubicInOut: 'cubicInOut',
28
+ bounceOut: 'bounceOut',
29
+ elasticOut: 'elasticOut',
30
+ /** Custom cubic-bezier curve via control points. */
31
+ bezier(x1, y1, x2, y2) {
32
+ return { x1, y1, x2, y2 };
33
+ },
34
+ };
@@ -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
  // FlatList — a thin API-compat wrapper over <ScrollView>, NOT a virtualized list.
18
17
  //
19
18
  // <FlatList data={items} keyExtractor={(it) => it.id}
@@ -32,57 +31,41 @@
32
31
  // which performs the same rewrite at compile time. The two flows accept the SAME four props on
33
32
  // purpose — a list that renders in the simulator must also compile for the device, so anything
34
33
  // Flow B rejects is warned about here rather than silently forwarded.
35
- import {createElement} from 'react';
36
- import {ScrollView} from './components.js';
37
- import {pushKeyedChild} from './list-child.js';
38
- import {createPropWarner} from './warn-props.js';
39
-
34
+ import { createElement } from 'react';
35
+ import { ScrollView } from './components.mjs';
36
+ import { pushKeyedChild } from './list-child.mjs';
37
+ import { createPropWarner } from './warn-props.mjs';
40
38
  /** The only props both flows honour. Everything else is a virtualization/platform knob with no meaning. */
41
39
  const SUPPORTED = ['data', 'renderItem', 'keyExtractor', 'style'];
42
-
43
40
  // Warns once about props that do nothing here and that the AOT refuses to compile, so a list built in
44
41
  // the simulator doesn't fail the device build later.
45
- const warnUnsupportedProps = createPropWarner(
46
- 'FlatList',
47
- SUPPORTED,
48
- 'it is a thin <ScrollView> alias with no virtualization, and the AOT rejects these props outright ' +
42
+ const warnUnsupportedProps = createPropWarner('FlatList', SUPPORTED, 'it is a thin <ScrollView> alias with no virtualization, and the AOT rejects these props outright ' +
49
43
  '("AOT: <FlatList> prop ... is not supported"). For headers/footers/separators/horizontal/' +
50
- 'onEndReached, use <ScrollView> + .map directly.',
51
- );
52
-
44
+ 'onEndReached, use <ScrollView> + .map directly.');
53
45
  /**
54
46
  * Renders `data` through `renderItem` into a <ScrollView>. Rows that render to nothing (null / false)
55
47
  * are dropped, matching a plain `.map` + conditional.
56
48
  *
57
- * @param {object} [props] Missing / empty renders an empty scroller, like a missing `data`.
58
- * @param {Array} [props.data] Row data. A non-array (or missing) renders an empty scroller.
59
- * @param {(info: {item: *, index: number}) => *} [props.renderItem] Row renderer, called per item.
60
- * @param {(item: *, index: number) => string|number} [props.keyExtractor] React key per row; defaults
61
- * to the key `renderItem` already set, else the index.
62
- * @param {object} [props.style] Forwarded to the ScrollView untouched.
63
- * @returns {*} A <ScrollView> element holding one child per rendered row.
49
+ * Missing props, or a `data` that is not an array, render an empty scroller. A row's key is
50
+ * `keyExtractor`'s, else the one `renderItem` already set, else the index. `style` goes to the ScrollView
51
+ * untouched.
64
52
  */
65
53
  export function FlatList(props = {}) {
66
- const {data, renderItem, keyExtractor, style} = props;
67
-
68
- warnUnsupportedProps(props);
69
-
70
- const rows = [];
71
- if (Array.isArray(data) && typeof renderItem === 'function') {
72
- for (let index = 0; index < data.length; index++) {
73
- const item = data[index];
74
- const row = renderItem({item, index});
75
- // The key is built lazily: pushKeyedChild calls this only for a row that can hold one, so
76
- // keyExtractor never runs for a row that rendered nothing or for a raw string.
77
- pushKeyedChild(rows, row, element =>
78
- keyExtractor
79
- ? String(keyExtractor(item, index))
80
- : element.key !== null && element.key !== undefined
81
- ? element.key
82
- : String(index),
83
- );
54
+ const { data, renderItem, keyExtractor, style } = props;
55
+ warnUnsupportedProps(props);
56
+ const rows = [];
57
+ if (Array.isArray(data) && typeof renderItem === 'function') {
58
+ for (let index = 0; index < data.length; index++) {
59
+ const item = data[index];
60
+ const row = renderItem({ item, index });
61
+ // The key is built lazily: pushKeyedChild calls this only for a row that can hold one, so
62
+ // keyExtractor never runs for a row that rendered nothing or for a raw string.
63
+ pushKeyedChild(rows, row, element => keyExtractor
64
+ ? String(keyExtractor(item, index))
65
+ : element.key !== null && element.key !== undefined
66
+ ? element.key
67
+ : String(index));
68
+ }
84
69
  }
85
- }
86
-
87
- return createElement(ScrollView, style === undefined ? null : {style}, rows);
70
+ return createElement(ScrollView, style === undefined ? null : { style }, rows);
88
71
  }
@@ -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
  // ImageBackground — RN's "content on top of a picture" wrapper, as a plain component. No engine node
18
17
  // of its own:
19
18
  //
@@ -33,10 +32,9 @@
33
32
  // the container's width/height onto the image to undo <Image>'s own sizing. Ours doesn't size itself,
34
33
  // so the inset alone fills the box — and since an absolute child resolves its insets against the
35
34
  // container's PADDING box, as in RN, the picture fills the whole container even when it has padding.
36
- import {createElement, forwardRef} from 'react';
37
- import {View, Image} from './components.js';
38
- import {StyleSheet} from './StyleSheet.js';
39
-
35
+ import { createElement, forwardRef } from 'react';
36
+ import { View, Image } from './components.mjs';
37
+ import { StyleSheet } from './StyleSheet.mjs';
40
38
  /**
41
39
  * A <View> with an <Image> stretched behind its children.
42
40
  *
@@ -45,23 +43,14 @@ import {StyleSheet} from './StyleSheet.js';
45
43
  * `onLayout` handlers on the image rather than the container, and the children paint on top of it, so
46
44
  * a handler meant for the whole area belongs on a <Pressable> around this component.
47
45
  *
48
- * @param {object} props Image props, plus the four below.
49
- * @param {*} [props.style] Container style — the box the picture fills.
50
- * @param {*} [props.imageStyle] Style merged over the fill, for `borderRadius`, `opacity`, `tintColor`.
51
- * @param {*} [props.imageRef] Ref to the <Image> node (the outer `ref` goes to the <View>).
52
- * @param {*} [props.children] Rendered above the picture, laid out against the container.
53
- * @returns {*} A <View> whose first child is the backing <Image>.
46
+ * `imageStyle` is merged over the fill, for `borderRadius`, `opacity`, `tintColor`. `imageRef` is a ref to
47
+ * the <Image> node (the outer `ref` goes to the <View>). The children are laid out against the container.
54
48
  */
55
49
  export const ImageBackground = forwardRef(function ImageBackground(props, ref) {
56
- const {children, style, imageStyle, imageRef, ...imageProps} = props;
57
- return createElement(
58
- View,
59
- {style, ref},
60
- createElement(Image, {
61
- ...imageProps,
62
- style: [StyleSheet.absoluteFill, imageStyle],
63
- ref: imageRef,
64
- }),
65
- children,
66
- );
50
+ const { children, style, imageStyle, imageRef, ...imageProps } = props;
51
+ return createElement(View, { style, ref }, createElement(Image, {
52
+ ...imageProps,
53
+ style: [StyleSheet.absoluteFill, imageStyle],
54
+ ref: imageRef,
55
+ }), children);
67
56
  });
@@ -0,0 +1,82 @@
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
+ // Keyboard — RN's Keyboard over the engine's on-screen keyboard, which shows while a <TextInput> has the
17
+ // focus. A build without it (ERUI_ONSCREEN_KEYBOARD=0) has no keyboard to report: dismiss() still takes
18
+ // the focus away, but no event fires.
19
+ import { NativeUI } from '../native-ui.mjs';
20
+ const listeners = {
21
+ keyboardWillShow: new Set(),
22
+ keyboardDidShow: new Set(),
23
+ keyboardWillHide: new Set(),
24
+ keyboardDidHide: new Set(),
25
+ };
26
+ let subscribed = false;
27
+ // Where the keyboard last showed, for the hide events.
28
+ let lastFrame = { x: 0, y: 0, width: 0, height: 0 };
29
+ const coordinates = (frame) => ({
30
+ screenX: frame.x,
31
+ screenY: frame.y,
32
+ width: frame.width,
33
+ height: frame.height,
34
+ });
35
+ function emit(eventName, event) {
36
+ // A copy, so a listener can remove itself.
37
+ for (const listener of [...listeners[eventName]])
38
+ listener(event);
39
+ }
40
+ function onKeyboardChange(shown) {
41
+ if (shown) {
42
+ lastFrame = NativeUI.keyboardFrame() ?? lastFrame;
43
+ const event = { endCoordinates: coordinates(lastFrame) };
44
+ emit('keyboardWillShow', event);
45
+ emit('keyboardDidShow', event);
46
+ }
47
+ else {
48
+ // It ends just below the screen.
49
+ const hidden = { ...lastFrame, y: lastFrame.y + lastFrame.height };
50
+ const event = { endCoordinates: coordinates(hidden) };
51
+ emit('keyboardWillHide', event);
52
+ emit('keyboardDidHide', event);
53
+ }
54
+ }
55
+ export const Keyboard = {
56
+ // Nothing animates, so each "will" event fires just before its "did".
57
+ addListener(eventName, listener) {
58
+ const set = listeners[eventName];
59
+ if (!set) {
60
+ throw new Error(`Keyboard.addListener: no event named "${eventName}"`);
61
+ }
62
+ if (!subscribed) {
63
+ NativeUI.setKeyboardListener(onKeyboardChange);
64
+ subscribed = true;
65
+ }
66
+ set.add(listener);
67
+ return { remove: () => set.delete(listener) };
68
+ },
69
+ removeAllListeners(eventName) {
70
+ listeners[eventName]?.clear();
71
+ },
72
+ dismiss() {
73
+ NativeUI.dismissKeyboard();
74
+ },
75
+ isVisible() {
76
+ return NativeUI.keyboardFrame() !== undefined;
77
+ },
78
+ metrics() {
79
+ const frame = NativeUI.keyboardFrame();
80
+ return frame ? coordinates(frame) : undefined;
81
+ },
82
+ };
@@ -13,43 +13,32 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
16
  // LayoutAnimation — the React Native analog. Calling configureNext(...) before a state update that
18
17
  // changes layout makes the engine tween every node whose computed rect moved (instead of snapping)
19
18
  // on the next commit. Backed by the engine's er_layout_animation_configure_next; the tween advances in C
20
19
  // each frame (er_layout_animation_tick), so there's no per-frame JS.
21
- import {NativeUI} from '../native-ui.js';
22
- import {
23
- Types,
24
- Properties,
25
- Presets,
26
- toEngineConfig,
27
- create,
28
- DEFAULT_DURATION_MS,
29
- } from './layout-anim-config.js';
30
-
20
+ import { NativeUI } from '../native-ui.mjs';
21
+ import { Types, Properties, Presets, toEngineConfig, create, DEFAULT_DURATION_MS, } from './layout-anim-config.mjs';
31
22
  /**
32
23
  * Arms a layout animation for the next commit. Call right before the setState that changes layout.
33
24
  * onAnimationDidEnd is approximated with a timer (the engine config carries no completion hook).
34
25
  */
35
26
  export function configureNext(config, onAnimationDidEnd) {
36
- NativeUI.configureNextLayoutAnimation(toEngineConfig(config));
37
- if (typeof onAnimationDidEnd === 'function') {
38
- const durationMs =
39
- config && typeof config.duration === 'number'
40
- ? config.duration
41
- : DEFAULT_DURATION_MS;
42
- setTimeout(onAnimationDidEnd, durationMs);
43
- }
27
+ NativeUI.configureNextLayoutAnimation(toEngineConfig(config));
28
+ if (typeof onAnimationDidEnd === 'function') {
29
+ const durationMs = config && typeof config.duration === 'number'
30
+ ? config.duration
31
+ : DEFAULT_DURATION_MS;
32
+ setTimeout(onAnimationDidEnd, durationMs);
33
+ }
44
34
  }
45
-
46
35
  export const LayoutAnimation = {
47
- configureNext,
48
- create,
49
- Types,
50
- Properties,
51
- Presets,
52
- easeInEaseOut: onDone => configureNext(Presets.easeInEaseOut, onDone),
53
- linear: onDone => configureNext(Presets.linear, onDone),
54
- spring: onDone => configureNext(Presets.spring, onDone),
36
+ configureNext,
37
+ create,
38
+ Types,
39
+ Properties,
40
+ Presets,
41
+ easeInEaseOut: (onDone) => configureNext(Presets.easeInEaseOut, onDone),
42
+ linear: (onDone) => configureNext(Presets.linear, onDone),
43
+ spring: (onDone) => configureNext(Presets.spring, onDone),
55
44
  };