Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions packages/webgpu/src/WebGPUViewNativeComponent.web.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect, useRef } from "react";
import React, { useEffect, useRef } from "react";
import { StyleSheet } from "react-native";
import { unstable_createElement as unstableCreateElement } from "react-native-web";
import type { Int32 } from "react-native/Libraries/Types/CodegenTypes";
import type { ViewProps } from "react-native";

Expand All @@ -15,7 +14,7 @@ export interface NativeProps extends ViewProps {
function debounce<T extends (...args: any[]) => void>(
func: T,
wait: number,
immediate = false,
immediate = false
) {
let timeout: ReturnType<typeof setTimeout> | undefined;
return function debounced(
Expand All @@ -39,7 +38,7 @@ function debounce<T extends (...args: any[]) => void>(
};
}

function resizeCanvas(canvas?: HTMLCanvasElement) {
function resizeCanvas(canvas: HTMLCanvasElement | null) {
if (!canvas) {
return;
}
Expand All @@ -55,7 +54,7 @@ function resizeCanvas(canvas?: HTMLCanvasElement) {
export default function WebGPUViewNativeComponent(props: NativeProps) {
const { contextId, style, transparent, ...rest } = props;

const canvasElm = useRef<HTMLCanvasElement>();
const canvasElm = useRef<HTMLCanvasElement>(null);

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

return unstableCreateElement("canvas", {
return React.createElement("canvas", {
...rest,
style: [
styles.view,
styles.flex1,
transparent === false && { backgroundColor: "white" }, // Canvas elements are transparent by default on the web
style,
],
id: contextIdToId(contextId),
style: {
...styles.view,
...styles.flex1,
...(transparent === false ? { backgroundColor: "white" } : {}),
...(typeof style === "object" ? style : {}),
},
ref: (ref: HTMLCanvasElement) => {
canvasElm.current = ref;
if (ref) {
Expand All @@ -90,7 +89,6 @@ const styles = StyleSheet.create({
view: {
alignItems: "stretch",
backgroundColor: "transparent",
// @ts-expect-error - not a valid RN style, but it's valid for web
border: "0 solid black",
boxSizing: "border-box",
display: "flex",
Expand Down
Loading