Skip to content

Commit 310034b

Browse files
authored
chore(🐙): refactor WebGPUViewNativeComponent for React usage (#335)
1 parent 7af78d4 commit 310034b

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

packages/webgpu/src/WebGPUViewNativeComponent.web.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { useEffect, useRef } from "react";
1+
import React, { useEffect, useRef } from "react";
22
import { StyleSheet } from "react-native";
3-
import { unstable_createElement as unstableCreateElement } from "react-native-web";
43
import type { Int32 } from "react-native/Libraries/Types/CodegenTypes";
54
import type { ViewProps } from "react-native";
65

@@ -15,7 +14,7 @@ export interface NativeProps extends ViewProps {
1514
function debounce<T extends (...args: any[]) => void>(
1615
func: T,
1716
wait: number,
18-
immediate = false,
17+
immediate = false
1918
) {
2019
let timeout: ReturnType<typeof setTimeout> | undefined;
2120
return function debounced(
@@ -39,7 +38,7 @@ function debounce<T extends (...args: any[]) => void>(
3938
};
4039
}
4140

42-
function resizeCanvas(canvas?: HTMLCanvasElement) {
41+
function resizeCanvas(canvas: HTMLCanvasElement | null) {
4342
if (!canvas) {
4443
return;
4544
}
@@ -55,7 +54,7 @@ function resizeCanvas(canvas?: HTMLCanvasElement) {
5554
export default function WebGPUViewNativeComponent(props: NativeProps) {
5655
const { contextId, style, transparent, ...rest } = props;
5756

58-
const canvasElm = useRef<HTMLCanvasElement>();
57+
const canvasElm = useRef<HTMLCanvasElement>(null);
5958

6059
useEffect(() => {
6160
const onResize = debounce(() => resizeCanvas(canvasElm.current), 100);
@@ -65,15 +64,15 @@ export default function WebGPUViewNativeComponent(props: NativeProps) {
6564
};
6665
}, []);
6766

68-
return unstableCreateElement("canvas", {
67+
return React.createElement("canvas", {
6968
...rest,
70-
style: [
71-
styles.view,
72-
styles.flex1,
73-
transparent === false && { backgroundColor: "white" }, // Canvas elements are transparent by default on the web
74-
style,
75-
],
7669
id: contextIdToId(contextId),
70+
style: {
71+
...styles.view,
72+
...styles.flex1,
73+
...(transparent === false ? { backgroundColor: "white" } : {}),
74+
...(typeof style === "object" ? style : {}),
75+
},
7776
ref: (ref: HTMLCanvasElement) => {
7877
canvasElm.current = ref;
7978
if (ref) {
@@ -90,7 +89,6 @@ const styles = StyleSheet.create({
9089
view: {
9190
alignItems: "stretch",
9291
backgroundColor: "transparent",
93-
// @ts-expect-error - not a valid RN style, but it's valid for web
9492
border: "0 solid black",
9593
boxSizing: "border-box",
9694
display: "flex",

0 commit comments

Comments
 (0)