react-native-roku 0.0.0-stage → 0.2.0-preview.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.
Files changed (200) hide show
  1. package/CHANGELOG.md +318 -0
  2. package/CONTRIBUTING.md +34 -0
  3. package/LICENSE +21 -0
  4. package/NOTICE.md +10 -0
  5. package/README.md +66 -2
  6. package/bin/react-native-roku.mjs +91 -0
  7. package/build/assets.cjs +311 -0
  8. package/build/assets.d.cts +50 -0
  9. package/build/compatibility.d.mts +40 -0
  10. package/build/compatibility.mjs +272 -0
  11. package/build/dev.d.mts +34 -0
  12. package/build/dev.mjs +297 -0
  13. package/build/devWorkspace.d.mts +11 -0
  14. package/build/devWorkspace.mjs +86 -0
  15. package/build/device.d.mts +47 -0
  16. package/build/device.mjs +310 -0
  17. package/build/environment.d.mts +29 -0
  18. package/build/environment.mjs +144 -0
  19. package/build/esbuild.d.mts +5 -0
  20. package/build/esbuild.mjs +118 -0
  21. package/build/fontVariants.d.mts +27 -0
  22. package/build/fontVariants.mjs +424 -0
  23. package/build/frontend.d.mts +52 -0
  24. package/build/frontend.mjs +75 -0
  25. package/build/identity.mjs +45 -0
  26. package/build/index.d.mts +22 -0
  27. package/build/index.mjs +87 -0
  28. package/build/intl.mjs +121 -0
  29. package/build/metro.cjs +38 -0
  30. package/build/metro.d.cts +37 -0
  31. package/build/metroFrontend.mjs +222 -0
  32. package/build/metroMetadata.mjs +119 -0
  33. package/build/metroModules.mjs +397 -0
  34. package/build/metroRefresh.d.mts +64 -0
  35. package/build/metroRefresh.mjs +346 -0
  36. package/build/metroTransformer.cjs +146 -0
  37. package/build/nativeTextMemo.mjs +296 -0
  38. package/build/nativewind.d.mts +9 -0
  39. package/build/nativewind.mjs +92 -0
  40. package/build/project.d.mts +111 -0
  41. package/build/project.mjs +615 -0
  42. package/build/reactCompiler.mjs +113 -0
  43. package/build/reactProfile.cjs +35 -0
  44. package/build/refreshRuntime.mjs +187 -0
  45. package/build/refreshSourceMap.mjs +53 -0
  46. package/build/release.mjs +99 -0
  47. package/build/resolution.d.mts +3 -0
  48. package/build/resolution.mjs +25 -0
  49. package/build/surfaces.d.mts +1 -0
  50. package/build/surfaces.mjs +42 -0
  51. package/build/testing/node.d.mts +29 -0
  52. package/build/testing/node.mjs +106 -0
  53. package/build/yoga.d.mts +8 -0
  54. package/build/yoga.mjs +186 -0
  55. package/compatibility/public-api.json +327 -0
  56. package/compatibility/registry.json +2258 -0
  57. package/config/release.json +15 -0
  58. package/config/toolchain.json +34 -0
  59. package/docs/compatibility.md +813 -0
  60. package/docs/getting-started.md +460 -0
  61. package/docs/maintenance.md +362 -0
  62. package/docs/qualification.md +241 -0
  63. package/docs/standalone-testing.md +56 -0
  64. package/examples/hello-roku/README.md +35 -0
  65. package/examples/hello-roku/package.template.json +31 -0
  66. package/examples/hello-roku/pnpm-workspace.yaml +11 -0
  67. package/examples/hello-roku/roku.config.json +22 -0
  68. package/examples/hello-roku/src/App.tsx +54 -0
  69. package/examples/hello-roku/src/Catalog.tsx +200 -0
  70. package/examples/hello-roku/src/assets.d.ts +16 -0
  71. package/examples/hello-roku/src/catalog-cover.png +0 -0
  72. package/examples/hello-roku/tsconfig.json +1 -0
  73. package/examples/hello-roku/tsconfig.react.json +1 -0
  74. package/native/components/RNRAppControl.brs +215 -0
  75. package/native/components/RNRAppControl.xml +13 -0
  76. package/native/components/RNRDeviceFacts.brs +19 -0
  77. package/native/components/RNRDeviceFacts.xml +9 -0
  78. package/native/components/RNRDeviceFactsTask.brs +74 -0
  79. package/native/components/RNRDeviceFactsTask.xml +9 -0
  80. package/native/components/RNRTextRaster.brs +15 -0
  81. package/native/components/RNRTextRaster.xml +9 -0
  82. package/native/components/RNRTextRasterTask.brs +77 -0
  83. package/native/components/RNRTextRasterTask.xml +9 -0
  84. package/native/manifest.json +7 -0
  85. package/package.json +183 -3
  86. package/src/animation/clock.ts +10 -0
  87. package/src/animation/derivedValue.ts +163 -0
  88. package/src/animation/legacyAnimated.ts +255 -0
  89. package/src/animation/reanimated.ts +243 -0
  90. package/src/animation/sharedValue.ts +285 -0
  91. package/src/animation/worklets.ts +23 -0
  92. package/src/application/AppRegistry.ts +31 -0
  93. package/src/application/mountApplication.ts +136 -0
  94. package/src/graphics/BitmapLayer.tsx +68 -0
  95. package/src/graphics/RoundedBorder.tsx +116 -0
  96. package/src/graphics/TextRasterCache.ts +259 -0
  97. package/src/graphics/gradient.ts +37 -0
  98. package/src/graphics/inlineRaster.ts +85 -0
  99. package/src/graphics/png.ts +77 -0
  100. package/src/graphics/rasterPixels.ts +89 -0
  101. package/src/graphics/surfaces.ts +105 -0
  102. package/src/graphics/textRaster.types.ts +60 -0
  103. package/src/graphics/textRasterGeometry.ts +116 -0
  104. package/src/host/host.types.ts +57 -0
  105. package/src/index.ts +22 -0
  106. package/src/interop/nativewind.ts +2 -0
  107. package/src/layout/FontResources.ts +252 -0
  108. package/src/layout/affine.ts +89 -0
  109. package/src/layout/flexLayout.ts +398 -0
  110. package/src/layout/fontFaces.d.mts +19 -0
  111. package/src/layout/fontFaces.mjs +85 -0
  112. package/src/layout/fontVariants.d.mts +2 -0
  113. package/src/layout/fontVariants.mjs +8 -0
  114. package/src/layout/inlineParagraph.ts +158 -0
  115. package/src/layout/inlineText.ts +68 -0
  116. package/src/layout/layoutReuse.ts +117 -0
  117. package/src/layout/nativeNodeFields.ts +179 -0
  118. package/src/layout/preparedFields.ts +78 -0
  119. package/src/layout/textLayout.ts +138 -0
  120. package/src/layout/transform.ts +113 -0
  121. package/src/media/SceneGraphAudio.ts +161 -0
  122. package/src/media/SceneGraphVideo.ts +167 -0
  123. package/src/media/audioSession.ts +84 -0
  124. package/src/media/index.ts +2 -0
  125. package/src/media/videoErrorDetails.ts +40 -0
  126. package/src/media/videoSession.ts +136 -0
  127. package/src/navigation/RouterStore.ts +244 -0
  128. package/src/navigation/expoRouter.ts +115 -0
  129. package/src/react-native/NativeInput.ts +494 -0
  130. package/src/react-native/NativeSystemState.ts +95 -0
  131. package/src/react-native/accessibility.ts +265 -0
  132. package/src/react-native/activityIndicator.ts +72 -0
  133. package/src/react-native/compilerPressable.ts +26 -0
  134. package/src/react-native/createNativeRoot.ts +2126 -0
  135. package/src/react-native/directionalFocus.ts +55 -0
  136. package/src/react-native/environment.ts +275 -0
  137. package/src/react-native/flatList.ts +1001 -0
  138. package/src/react-native/flatListViewability.ts +231 -0
  139. package/src/react-native/focusEvents.ts +85 -0
  140. package/src/react-native/image.ts +247 -0
  141. package/src/react-native/index.ts +54 -0
  142. package/src/react-native/interactionManager.ts +76 -0
  143. package/src/react-native/keyboard.ts +60 -0
  144. package/src/react-native/linearGradient.ts +94 -0
  145. package/src/react-native/modal.tsx +148 -0
  146. package/src/react-native/nativeHostContext.ts +80 -0
  147. package/src/react-native/nativeModules.ts +86 -0
  148. package/src/react-native/nativeRoot.types.ts +22 -0
  149. package/src/react-native/nativeViewHandle.ts +191 -0
  150. package/src/react-native/nativewindPressable.ts +38 -0
  151. package/src/react-native/panResponder.ts +73 -0
  152. package/src/react-native/primitives.ts +685 -0
  153. package/src/react-native/safeArea.ts +76 -0
  154. package/src/react-native/scrollView.ts +289 -0
  155. package/src/react-native/styleInterop.ts +8 -0
  156. package/src/react-native/styleSheet.ts +54 -0
  157. package/src/react-native/textInput.ts +334 -0
  158. package/src/react-native/unsupported.ts +25 -0
  159. package/src/renderer/SceneRoot.ts +1006 -0
  160. package/src/scene/SdkDeviceFacts.ts +151 -0
  161. package/src/scene/SdkDeviceServices.ts +72 -0
  162. package/src/scene/SdkSceneGraph.ts +124 -0
  163. package/src/scene/SdkTextEntry.ts +321 -0
  164. package/src/scene/SdkTextMetrics.ts +136 -0
  165. package/src/scene/SdkTextRasterTransport.ts +191 -0
  166. package/src/scene/SdkWebTransport.ts +122 -0
  167. package/src/scene/createRokuHost.ts +213 -0
  168. package/src/scene/sceneGraph.types.ts +38 -0
  169. package/src/services/DeviceServices.ts +53 -0
  170. package/src/services/RegistryStorage.ts +85 -0
  171. package/src/services/RokuKeySession.ts +29 -0
  172. package/src/services/asyncStorage.ts +23 -0
  173. package/src/services/base64.ts +52 -0
  174. package/src/services/crypto.ts +18 -0
  175. package/src/services/expoConstants.ts +11 -0
  176. package/src/services/expoLinking.ts +12 -0
  177. package/src/services/expoStatusBar.ts +4 -0
  178. package/src/services/installWebRuntime.ts +105 -0
  179. package/src/services/intlEnglish.ts +54 -0
  180. package/src/services/languageRuntime.ts +65 -0
  181. package/src/services/nativeTimers.ts +96 -0
  182. package/src/services/secureStore.ts +23 -0
  183. package/src/services/textEntryRequest.ts +22 -0
  184. package/src/services/utf8.ts +57 -0
  185. package/src/services/webRuntime.ts +346 -0
  186. package/src/testing/CanvasResources.ts +141 -0
  187. package/src/testing/CanvasSceneGraph.ts +496 -0
  188. package/src/testing/CanvasTextEntry.ts +237 -0
  189. package/src/testing/PreviewTextObservations.ts +82 -0
  190. package/src/testing/RecordingSceneGraph.ts +136 -0
  191. package/src/types/normalize-colors.d.ts +4 -0
  192. package/src/vector/dynamicSvg.tsx +226 -0
  193. package/src/vector/svg.ts +222 -0
  194. package/src/vector/svg.types.ts +4 -0
  195. package/src/vector/svgElements.ts +58 -0
  196. package/src/vector/svgKey.d.mts +4 -0
  197. package/src/vector/svgKey.mjs +16 -0
  198. package/src/vector/svgPath.ts +157 -0
  199. package/src/vector/svgPixels.ts +133 -0
  200. package/tsconfig.consumer.json +29 -0
@@ -0,0 +1,685 @@
1
+ import {
2
+ createElement,
3
+ forwardRef,
4
+ useId,
5
+ useImperativeHandle,
6
+ useInsertionEffect,
7
+ useMemo,
8
+ useEffect,
9
+ useLayoutEffect,
10
+ useRef,
11
+ useState,
12
+ type ForwardedRef,
13
+ type ForwardRefExoticComponent,
14
+ type ReactNode,
15
+ type ReactElement,
16
+ type RefAttributes,
17
+ Children,
18
+ isValidElement,
19
+ } from "react";
20
+ import { jsx, jsxs } from "react/jsx-runtime";
21
+ import {
22
+ generatedSceneFields,
23
+ plainBackgroundLayout,
24
+ type SceneElementProps,
25
+ } from "../renderer/SceneRoot";
26
+ import type { LayoutStyle } from "../layout/flexLayout";
27
+ import { hasOnlyBuiltinObjectPrototypeFields } from "../layout/layoutReuse";
28
+ import { RokuHostError } from "../scene/sceneGraph.types";
29
+ import { RoundedBorder } from "../graphics/RoundedBorder";
30
+ import {
31
+ useNativeHost,
32
+ type FocusPresentation,
33
+ type NativeHostContextValue,
34
+ } from "./nativeHostContext";
35
+ import {
36
+ createNativeViewHandle,
37
+ bindNativeViewHandle,
38
+ findNodeHandle,
39
+ type NativeViewHandle,
40
+ } from "./nativeViewHandle";
41
+ import { flattenStyle, rokuColor, type NativeStyle } from "./styleSheet";
42
+ import { hasRoundedCorners, uniformBorder, type NativeGraphics } from "../graphics/surfaces";
43
+ import {
44
+ scalarPressableKeyOrder,
45
+ usesScalarPressableStyle,
46
+ type CompiledPressableKeys,
47
+ } from "./compilerPressable";
48
+ import type { NativeFocusBinding, NativeFocusHandlers } from "./focusEvents";
49
+ import { normalizeAccessibility, type AccessibilityProps } from "./accessibility";
50
+
51
+ // SceneGraph host names are strings outside React's DOM JSX namespace.
52
+ type CreateSceneElement = (
53
+ type: string,
54
+ props: SceneElementProps & { children?: ReactNode },
55
+ ) => ReturnType<typeof jsx>;
56
+ const sceneElement = jsx as CreateSceneElement;
57
+ const sceneElementWithChildren = jsxs as CreateSceneElement;
58
+ declare const __DEV__: boolean;
59
+ const reuseBackgroundElements = typeof __DEV__ !== "undefined" && !__DEV__;
60
+ const backgroundElements = new Map<string | number, ReactElement>();
61
+ const defaultAccessibility = normalizeAccessibility({});
62
+ const borderEdges = [
63
+ ["Top", "borderTopWidth", "borderTopColor"],
64
+ ["Bottom", "borderBottomWidth", "borderBottomColor"],
65
+ ["Left", "borderLeftWidth", "borderLeftColor"],
66
+ ["Right", "borderRightWidth", "borderRightColor"],
67
+ ] as const;
68
+
69
+ function plainBackground(color: unknown) {
70
+ // Development React elements retain their rendering owner.
71
+ const cacheable =
72
+ reuseBackgroundElements && (typeof color === "string" || typeof color === "number");
73
+ const previous = cacheable ? backgroundElements.get(color) : undefined;
74
+ if (previous) return previous;
75
+ const element = createElement("rectangle", {
76
+ key: "background",
77
+ fields: plainBackgroundFields(color),
78
+ layout: plainBackgroundLayout,
79
+ } satisfies SceneElementProps & { key: string });
80
+ if (cacheable && backgroundElements.size < 64) backgroundElements.set(color, element);
81
+ return element;
82
+ }
83
+
84
+ const backgroundFieldsByColor = new Map<string | number, Readonly<{ color: string }>>();
85
+
86
+ function plainBackgroundFields(color: unknown) {
87
+ const cacheable = typeof color === "string" || typeof color === "number";
88
+ const previous = cacheable ? backgroundFieldsByColor.get(color) : undefined;
89
+ if (previous) return previous;
90
+ const fields = generatedSceneFields({ color: rokuColor(color) });
91
+ if (cacheable && backgroundFieldsByColor.size < 64) backgroundFieldsByColor.set(color, fields);
92
+ return fields;
93
+ }
94
+
95
+ export interface NativeElementProps extends NativeFocusHandlers, AccessibilityProps {
96
+ readonly children?: ReactNode;
97
+ readonly style?: NativeStyle;
98
+ readonly className?: string;
99
+ readonly onLayout?: (event: unknown) => void;
100
+ readonly nativeID?: string;
101
+ readonly testID?: string;
102
+ readonly focusable?: boolean;
103
+ readonly accessible?: boolean;
104
+ readonly accessibilityLabel?: string;
105
+ readonly accessibilityRole?: string;
106
+ readonly pointerEvents?: string;
107
+ readonly importantForAccessibility?: string;
108
+ readonly accessibilityElementsHidden?: boolean;
109
+ readonly "aria-hidden"?: boolean;
110
+ readonly nextFocusUp?: number | null;
111
+ readonly nextFocusRight?: number | null;
112
+ readonly nextFocusDown?: number | null;
113
+ readonly nextFocusLeft?: number | null;
114
+ }
115
+ interface NativeTextProps extends NativeElementProps {
116
+ readonly numberOfLines?: number;
117
+ readonly allowFontScaling?: boolean;
118
+ readonly ellipsizeMode?: "tail" | "head" | "middle" | "clip";
119
+ readonly onPress?: () => void;
120
+ }
121
+
122
+ export function useElement(props: NativeElementProps, ref: ForwardedRef<NativeViewHandle>) {
123
+ return useNativeElement(useNativeHost(), props, ref, props.style);
124
+ }
125
+
126
+ function useNativeIdentity(
127
+ host: NativeHostContextValue,
128
+ nativeID: string | undefined,
129
+ ref: ForwardedRef<NativeViewHandle>,
130
+ ) {
131
+ const reactId = useId();
132
+ const id = nativeID ?? reactId;
133
+ const { handle, fields, bind, read } = useMemo(() => {
134
+ const handle = createNativeViewHandle(id, host);
135
+ return {
136
+ handle,
137
+ fields: generatedSceneFields({ id }),
138
+ bind: () => bindNativeViewHandle(handle),
139
+ read: () => handle,
140
+ };
141
+ }, [host, id]);
142
+ useLayoutEffect(bind, [bind]);
143
+ useImperativeHandle(ref, read, [read]);
144
+ return { id, handle, fields };
145
+ }
146
+
147
+ function primitiveStyle(
148
+ props: Pick<NativeElementProps, "className">,
149
+ nativeStyle: NativeStyle,
150
+ ): LayoutStyle {
151
+ if (props.className)
152
+ throw new RokuHostError(
153
+ "C05_NATIVEWIND",
154
+ `Class ${props.className} reached a primitive without style interop`,
155
+ );
156
+ return flattenStyle(nativeStyle) ?? {};
157
+ }
158
+
159
+ function nativeLayout(
160
+ { id, handle }: ReturnType<typeof useNativeIdentity>,
161
+ nativeTag: number | null,
162
+ props: Omit<NativeElementProps, "style" | "children">,
163
+ style: LayoutStyle,
164
+ disabled: boolean | undefined,
165
+ plainText?: boolean,
166
+ stableFocus?: NativeFocusBinding,
167
+ ) {
168
+ if (plainText)
169
+ return {
170
+ id,
171
+ nativeTag,
172
+ view: handle,
173
+ style,
174
+ accessibility: defaultAccessibility,
175
+ importantForAccessibility: "auto",
176
+ accessibilityElementsHidden: false,
177
+ pointerEvents: style.pointerEvents ?? "auto",
178
+ };
179
+ return {
180
+ id,
181
+ nativeTag,
182
+ view: handle,
183
+ focusEvents:
184
+ stableFocus ??
185
+ (props.onFocus || props.onFocusCapture || props.onBlur || props.onBlurCapture
186
+ ? {
187
+ onFocus: props.onFocus,
188
+ onFocusCapture: props.onFocusCapture,
189
+ onBlur: props.onBlur,
190
+ onBlurCapture: props.onBlurCapture,
191
+ }
192
+ : undefined),
193
+ style,
194
+ onLayout: props.onLayout,
195
+ focusable: props.focusable,
196
+ accessibilityLabel: props.accessibilityLabel,
197
+ accessibility: normalizeAccessibility(props, disabled),
198
+ importantForAccessibility: props.importantForAccessibility ?? "auto",
199
+ accessibilityElementsHidden: props["aria-hidden"] ?? props.accessibilityElementsHidden ?? false,
200
+ accessible: props.accessible,
201
+ pointerEvents: props.pointerEvents ?? style.pointerEvents ?? "auto",
202
+ testID: props.testID,
203
+ focusTargets:
204
+ props.nextFocusUp !== undefined ||
205
+ props.nextFocusRight !== undefined ||
206
+ props.nextFocusDown !== undefined ||
207
+ props.nextFocusLeft !== undefined
208
+ ? {
209
+ up: props.nextFocusUp,
210
+ right: props.nextFocusRight,
211
+ down: props.nextFocusDown,
212
+ left: props.nextFocusLeft,
213
+ }
214
+ : undefined,
215
+ };
216
+ }
217
+
218
+ function useNativeElement(
219
+ host: NativeHostContextValue,
220
+ props: Omit<NativeElementProps, "style" | "children">,
221
+ ref: ForwardedRef<NativeViewHandle>,
222
+ nativeStyle: NativeStyle,
223
+ disabled?: boolean,
224
+ plainText = false,
225
+ ) {
226
+ const identity = useNativeIdentity(host, props.nativeID, ref);
227
+ const style = primitiveStyle(props, nativeStyle);
228
+ const nativeTag = findNodeHandle(identity.handle);
229
+ return {
230
+ id: identity.id,
231
+ nativeTag,
232
+ style,
233
+ layout: nativeLayout(identity, nativeTag, props, style, disabled, plainText),
234
+ fields: identity.fields,
235
+ };
236
+ }
237
+
238
+ export function background(style: LayoutStyle, graphics?: NativeGraphics): ReactNode[] {
239
+ const result: ReactNode[] = [];
240
+ if (hasRoundedCorners(style)) {
241
+ if (!graphics?.effects)
242
+ throw new RokuHostError(
243
+ "C03_ROUNDED_SURFACE",
244
+ "Rounded surfaces require an OS 16 OpenGL Effect capability",
245
+ );
246
+ let border = 0,
247
+ asymmetric = false;
248
+ try {
249
+ border = uniformBorder(style);
250
+ } catch (error) {
251
+ if (!(error instanceof RokuHostError) || error.code !== "C03_ROUNDED_BORDER") throw error;
252
+ asymmetric = true;
253
+ }
254
+ return [
255
+ createElement(
256
+ "rectangle",
257
+ {
258
+ key: "rounded-surface",
259
+ fields: { color: rokuColor(style.backgroundColor ?? "transparent") },
260
+ layout: { decoration: "surface", border },
261
+ },
262
+ createElement("effect", {
263
+ fields: {
264
+ borderWidth: border,
265
+ borderColor: rokuColor(style.borderColor ?? "black"),
266
+ },
267
+ layout: { resource: true, radiusStyle: style, insetBorder: border },
268
+ }),
269
+ ),
270
+ ...(asymmetric ? [createElement(RoundedBorder, { key: "asymmetric-border", style })] : []),
271
+ ];
272
+ }
273
+ if (style.backgroundColor != null) result.push(plainBackground(style.backgroundColor));
274
+ for (const [edge, widthField, colorField] of borderEdges) {
275
+ const border = style[widthField] ?? style.borderWidth ?? 0;
276
+ if (typeof border !== "number" || !Number.isFinite(border) || border < 0)
277
+ throw new RokuHostError("C03_BORDER", "Border widths must be finite and nonnegative");
278
+ if (border > 0) {
279
+ const color = rokuColor(style[colorField] ?? style.borderColor ?? "black");
280
+ result.push(
281
+ createElement("rectangle", {
282
+ key: `border-${edge}`,
283
+ fields: { color },
284
+ layout: { decoration: edge, thickness: border },
285
+ } satisfies SceneElementProps & { key: string }),
286
+ );
287
+ }
288
+ }
289
+ return result;
290
+ }
291
+
292
+ // Every View retains its native group. The upstream collapse hint never removes
293
+ // ref/measurement identity in this renderer, including collapsable={false}.
294
+ type ViewProps = NativeElementProps & { readonly collapsable?: boolean };
295
+ export const ScrollViewLayer = forwardRef<
296
+ NativeViewHandle,
297
+ ViewProps & { readonly scrollTranslation?: readonly [number, number] }
298
+ >((props, ref) => {
299
+ const element = useElement(props, ref),
300
+ host = useNativeHost();
301
+ return createElement(
302
+ "group",
303
+ {
304
+ fields: element.fields,
305
+ layout: props.scrollTranslation
306
+ ? { ...element.layout, scrollTranslation: props.scrollTranslation }
307
+ : element.layout,
308
+ },
309
+ ...background(element.style, host.graphics),
310
+ element.style.overflow === "hidden" && hasRoundedCorners(element.style)
311
+ ? createElement(SurfaceClip, { style: element.style }, props.children)
312
+ : props.children,
313
+ );
314
+ });
315
+ export const View: ForwardRefExoticComponent<ViewProps & RefAttributes<NativeViewHandle>> =
316
+ ScrollViewLayer;
317
+ View.displayName = "RokuView";
318
+
319
+ export function SurfaceClip({
320
+ style,
321
+ children,
322
+ }: {
323
+ readonly style: LayoutStyle;
324
+ readonly children?: ReactNode;
325
+ }) {
326
+ const host = useNativeHost(),
327
+ [ready, setReady] = useState(0),
328
+ dimensions = useRef(new Map<number, { width: boolean; height: boolean }>());
329
+ const masks = host.graphics?.cornerMasks;
330
+ const composed = host.graphics?.composedMasks;
331
+ const composedDimensions = useRef({ width: false, height: false });
332
+ if (composed) {
333
+ const observe = (axis: "width" | "height", value: unknown) => {
334
+ if (!(Number(value) > 0)) return;
335
+ composedDimensions.current[axis] = true;
336
+ if (composedDimensions.current.width && composedDimensions.current.height) setReady(15);
337
+ };
338
+ return createElement(
339
+ "maskGroup",
340
+ {
341
+ layout: {
342
+ decoration: "background",
343
+ paintWrapper: true,
344
+ radiusStyle: style,
345
+ composedMask: true,
346
+ },
347
+ events: {
348
+ maskBitmapWidth: (value) => observe("width", value),
349
+ maskBitmapHeight: (value) => observe("height", value),
350
+ },
351
+ } satisfies SceneElementProps,
352
+ createElement(
353
+ "group",
354
+ { layout: { decoration: "background", paintWrapper: true, maskReadiness: ready === 15 } },
355
+ children,
356
+ ),
357
+ );
358
+ }
359
+ if (!masks)
360
+ throw new RokuHostError(
361
+ "C03_ROUNDED_CLIP",
362
+ "Rounded child clipping requires the OpenGL corner-mask assets",
363
+ );
364
+ let content: ReactNode = createElement(
365
+ "group",
366
+ {
367
+ layout: {
368
+ decoration: "background",
369
+ paintWrapper: true,
370
+ maskReadiness: ready === 15,
371
+ },
372
+ },
373
+ children,
374
+ );
375
+ for (let corner = 3; corner >= 0; corner--) {
376
+ const current = corner;
377
+ const observe = (axis: "width" | "height", value: unknown) => {
378
+ if (!(Number(value) > 0)) return;
379
+ const entry = dimensions.current.get(current) ?? {
380
+ width: false,
381
+ height: false,
382
+ };
383
+ entry[axis] = true;
384
+ dimensions.current.set(current, entry);
385
+ if (entry.width && entry.height) setReady((previous) => previous | (1 << current));
386
+ };
387
+ content = createElement(
388
+ "maskGroup",
389
+ {
390
+ fields: { maskUri: masks[corner] },
391
+ layout: {
392
+ decoration: "background",
393
+ paintWrapper: true,
394
+ corner,
395
+ radiusStyle: style,
396
+ },
397
+ events: {
398
+ maskBitmapWidth: (value) => observe("width", value),
399
+ maskBitmapHeight: (value) => observe("height", value),
400
+ },
401
+ } satisfies SceneElementProps,
402
+ content,
403
+ );
404
+ }
405
+ return content;
406
+ }
407
+
408
+ export const Text = forwardRef<NativeViewHandle, NativeTextProps>((props, ref) => {
409
+ const plain =
410
+ Object.keys(props).length === 2 &&
411
+ Object.hasOwn(props, "style") &&
412
+ Object.hasOwn(props, "children") &&
413
+ (typeof props.children === "string" || typeof props.children === "number");
414
+ const element = useNativeElement(useNativeHost(), props, ref, props.style, undefined, plain);
415
+ if (props.onPress)
416
+ throw new RokuHostError("C07_TEXT_PRESS", "Pressable text needs an input adapter");
417
+ if (props.ellipsizeMode && props.ellipsizeMode !== "tail")
418
+ throw new RokuHostError(
419
+ "C04_TEXT_ELLIPSIS",
420
+ `Ellipsis mode ${props.ellipsizeMode} needs conformance`,
421
+ );
422
+ const raster =
423
+ Number(element.style.letterSpacing ?? 0) !== 0 ||
424
+ element.style.textShadowColor != null ||
425
+ element.style.textShadowRadius != null;
426
+ let inline = false;
427
+ const children = props.children;
428
+ const scalarText =
429
+ typeof children === "string"
430
+ ? children
431
+ : typeof children === "number"
432
+ ? String(children)
433
+ : undefined;
434
+ if (children != null && scalarText === undefined) {
435
+ Children.forEach(children, (child) => {
436
+ if (isValidElement(child)) inline = true;
437
+ });
438
+ }
439
+ const directText = raster ? undefined : scalarText;
440
+ return sceneElement(
441
+ inline && !raster
442
+ ? props.numberOfLines === 1
443
+ ? "multiStyleLabel"
444
+ : "inlineText"
445
+ : raster
446
+ ? "poster"
447
+ : "label",
448
+ {
449
+ fields: element.fields,
450
+ layout: Object.assign(element.layout, {
451
+ textRaster: raster,
452
+ inlineRaster: inline && raster,
453
+ numberOfLines: props.numberOfLines ?? 0,
454
+ scalarText: directText,
455
+ }),
456
+ children: directText === undefined ? children : undefined,
457
+ },
458
+ );
459
+ });
460
+ Text.displayName = "RokuText";
461
+
462
+ interface PressableState {
463
+ readonly pressed: boolean;
464
+ readonly focused: boolean;
465
+ readonly hovered: boolean;
466
+ }
467
+ interface PressableEvent {
468
+ readonly nativeEvent: {
469
+ readonly target: number | undefined;
470
+ readonly timestamp: number;
471
+ };
472
+ }
473
+ export interface NativePressableProps extends Omit<NativeElementProps, "style" | "children"> {
474
+ /** Experimental early presentation for already-mounted native focus moves.
475
+ * The ordinary React focus callback still owns the final render. */
476
+ readonly unstable_tvFocusPresentation?: FocusPresentation;
477
+ readonly style?: NativeStyle | ((state: PressableState) => NativeStyle);
478
+ readonly children?: ReactNode | ((state: PressableState) => ReactNode);
479
+ readonly disabled?: boolean;
480
+ readonly hasTVPreferredFocus?: boolean;
481
+ readonly delayLongPress?: number;
482
+ readonly onPress?: (event: PressableEvent) => void;
483
+ readonly onPressIn?: (event: PressableEvent) => void;
484
+ readonly onPressOut?: (event: PressableEvent) => void;
485
+ readonly onLongPress?: (event: PressableEvent) => void;
486
+ }
487
+
488
+ type PressableProps = NativePressableProps & RefAttributes<NativeViewHandle>;
489
+ type PressableLayoutCache = {
490
+ readonly handle: NativeViewHandle;
491
+ readonly nativeTag: number | null;
492
+ readonly style: LayoutStyle;
493
+ readonly content: ReactNode;
494
+ readonly metadata: {
495
+ readonly keys: CompiledPressableKeys;
496
+ readonly values: readonly unknown[];
497
+ };
498
+ readonly output: ReactElement;
499
+ };
500
+ type PressableBinding = {
501
+ props: PressableProps;
502
+ focusEvents?: () => NativeFocusHandlers;
503
+ output?: PressableLayoutCache;
504
+ };
505
+ // Branded JSX uses ordinary data props. Inherited layout or input fields
506
+ // require the public primitive's full prop-reading path.
507
+ function comparePressableInputs(
508
+ props: PressableProps,
509
+ baseline?: PressableLayoutCache["metadata"],
510
+ ): PressableLayoutCache["metadata"] | undefined {
511
+ const keys = scalarPressableKeyOrder(props);
512
+ if (
513
+ !keys ||
514
+ Object.getPrototypeOf(props) !== Object.prototype ||
515
+ !hasOnlyBuiltinObjectPrototypeFields()
516
+ )
517
+ return undefined;
518
+ if (baseline?.keys === keys) {
519
+ for (let i = 0; i < keys.length; i++) {
520
+ if (!Object.is(props[keys[i]], baseline.values[i])) return undefined;
521
+ }
522
+ return baseline;
523
+ }
524
+ if (baseline) return undefined;
525
+ const values: unknown[] = [];
526
+ for (const name of keys) {
527
+ const value = props[name];
528
+ if (
529
+ value != null &&
530
+ typeof value !== "string" &&
531
+ typeof value !== "boolean" &&
532
+ typeof value !== "number"
533
+ )
534
+ return undefined;
535
+ values.push(value);
536
+ }
537
+ return { keys, values };
538
+ }
539
+
540
+ export function Pressable(props: PressableProps) {
541
+ const host = useNativeHost(),
542
+ input = host.input;
543
+ if (!input)
544
+ throw new RokuHostError("C07_INPUT_HOST", "Pressable requires a mounted native input port");
545
+ const current = useRef<PressableBinding | null>(null);
546
+ const binding = (current.current ??= { props });
547
+ useInsertionEffect(() => {
548
+ binding.props = props;
549
+ }, [binding, props]);
550
+ const [state, setState] = useState<PressableState>({
551
+ pressed: false,
552
+ focused: false,
553
+ hovered: false,
554
+ });
555
+ const style = typeof props.style === "function" ? props.style(state) : props.style;
556
+ const identity = useNativeIdentity(host, props.nativeID, props.ref ?? null);
557
+ const paint = primitiveStyle(props, style);
558
+ const nativeTag = findNodeHandle(identity.handle);
559
+ const { id } = identity;
560
+ const focusable = !props.disabled && props.focusable !== false;
561
+ const nativeProps = useMemo(
562
+ () => ({
563
+ fields: generatedSceneFields({ id, focusable }),
564
+ events: Object.freeze({
565
+ focusedChild: (value: unknown) => input.observeFocus(id, value === id),
566
+ }),
567
+ }),
568
+ [id, focusable, input],
569
+ );
570
+ const event = () => ({
571
+ nativeEvent: {
572
+ target: nativeTag ?? undefined,
573
+ timestamp: Date.now(),
574
+ },
575
+ });
576
+ useLayoutEffect(
577
+ () =>
578
+ input.register(id, {
579
+ tag: nativeTag ?? undefined,
580
+ enabled: () => {
581
+ const placement = host.placement(id);
582
+ return (
583
+ host.active &&
584
+ !!placement &&
585
+ placement.visible !== false &&
586
+ placement.modalBlocked !== true &&
587
+ !binding.props.disabled &&
588
+ binding.props.focusable !== false
589
+ );
590
+ },
591
+ focus(focused) {
592
+ setState((value) => (value.focused === focused ? value : { ...value, focused }));
593
+ if (focused) host.announceFocus(id);
594
+ host.dispatchFocus(id, focused);
595
+ },
596
+ pressed(pressed) {
597
+ setState((value) => (value.pressed === pressed ? value : { ...value, pressed }));
598
+ (pressed ? binding.props.onPressIn : binding.props.onPressOut)?.(event());
599
+ },
600
+ activate: () => binding.props.onPress?.(event()),
601
+ get longPress() {
602
+ return binding.props.onLongPress ? () => binding.props.onLongPress?.(event()) : undefined;
603
+ },
604
+ get longPressDelay() {
605
+ return binding.props.delayLongPress;
606
+ },
607
+ }),
608
+ [input, id, host],
609
+ );
610
+ useEffect(() => {
611
+ if (!props.hasTVPreferredFocus || props.disabled || props.focusable === false) return;
612
+ let canceled = false;
613
+ void host.whenCommitted().then(
614
+ () => {
615
+ if (!canceled) host.requestFocus(id);
616
+ },
617
+ () => undefined,
618
+ );
619
+ return () => {
620
+ canceled = true;
621
+ };
622
+ }, [host, id, props.hasTVPreferredFocus, props.disabled, props.focusable]);
623
+ const previous = binding.output;
624
+ if (
625
+ previous?.handle === identity.handle &&
626
+ previous.nativeTag === nativeTag &&
627
+ previous.style === paint &&
628
+ comparePressableInputs(props, previous.metadata) &&
629
+ previous.content === props.children
630
+ )
631
+ return previous.output;
632
+
633
+ const metadata = usesScalarPressableStyle(paint) ? comparePressableInputs(props) : undefined;
634
+ const cacheable = metadata && typeof props.children !== "function" ? metadata : undefined;
635
+ const base = nativeLayout(
636
+ identity,
637
+ nativeTag,
638
+ props,
639
+ paint,
640
+ props.disabled,
641
+ false,
642
+ cacheable ? (binding.focusEvents ??= () => binding.props) : undefined,
643
+ );
644
+ const layout = props.unstable_tvFocusPresentation
645
+ ? { ...base, focusPresentation: props.unstable_tvFocusPresentation }
646
+ : base;
647
+ const rounded = hasRoundedCorners(paint);
648
+ const simplePaint =
649
+ !rounded &&
650
+ paint.borderWidth == null &&
651
+ paint.borderTopWidth == null &&
652
+ paint.borderBottomWidth == null &&
653
+ paint.borderLeftWidth == null &&
654
+ paint.borderRightWidth == null;
655
+ const backgroundFields =
656
+ simplePaint && paint.backgroundColor != null
657
+ ? plainBackgroundFields(paint.backgroundColor)
658
+ : undefined;
659
+ const decorations = simplePaint ? undefined : background(paint, host.graphics);
660
+ const content = typeof props.children === "function" ? props.children(state) : props.children;
661
+ const child =
662
+ paint.overflow === "hidden" && rounded
663
+ ? createElement(SurfaceClip, { style: paint }, content)
664
+ : content;
665
+ const { fields, events } = nativeProps;
666
+ let output;
667
+ if (!decorations?.length) {
668
+ output = sceneElement("group", { fields, layout, events, backgroundFields, children: child });
669
+ } else {
670
+ decorations.push(child);
671
+ output = sceneElementWithChildren("group", { fields, layout, events, children: decorations });
672
+ }
673
+ binding.output = cacheable
674
+ ? {
675
+ handle: identity.handle,
676
+ nativeTag,
677
+ style: paint,
678
+ content,
679
+ metadata: cacheable,
680
+ output,
681
+ }
682
+ : undefined;
683
+ return output;
684
+ }
685
+ Pressable.displayName = "RokuPressable";