Skip to content

Commit b978e59

Browse files
authored
refactor: Change component name from ReactP5Wrapper to P5Canvas (#525)
1 parent 4df3752 commit b978e59

38 files changed

+918
-331
lines changed

.github/README.md

Lines changed: 608 additions & 16 deletions
Large diffs are not rendered by default.

config/vite/library.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export function library(root: string): UserConfig {
2626
emptyOutDir: true,
2727
lib: {
2828
entry: resolve(root, "src", "main.tsx"),
29-
name: "ReactP5Wrapper",
30-
fileName: "ReactP5Wrapper",
29+
name: "P5Canvas",
30+
fileName: "P5Canvas",
3131
formats: ["es", "cjs"]
3232
},
3333
rollupOptions: {

demo/app.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactP5Wrapper } from "@/main.tsx";
1+
import { P5Canvas } from "@/main.tsx";
22
import React, { useCallback, useMemo, useState } from "react";
33
import { createRoot } from "react-dom/client";
44

@@ -61,7 +61,7 @@ function App() {
6161

6262
return (
6363
<>
64-
<ReactP5Wrapper sketch={state.sketch} rotation={state.rotation} />
64+
<P5Canvas sketch={state.sketch} rotation={state.rotation} />
6565
{state.sketch === record && (
6666
<div style={{ marginTop: "1rem", marginBottom: "1rem" }}>
6767
<button id="start-recording">Start Recording</button>

src/components/P5Canvas.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as React from "react";
2+
import { propsAreEqual } from "@utils/propsAreEqual";
3+
4+
const P5CanvasGuard = React.lazy(() => import("@components/P5CanvasGuard"));
5+
6+
export const P5Canvas = React.memo(P5CanvasGuard, propsAreEqual);

src/components/ReactP5WrapperGuard.tsx renamed to src/components/P5CanvasGuard.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as React from "react";
2-
import { type P5WrapperProps } from "@contracts/P5WrapperProps";
3-
import { type P5WrapperPropsWithSketch } from "@contracts/P5WrapperPropsWithSketch";
2+
import { type P5CanvasProps } from "@contracts/P5CanvasProps";
3+
import { type P5CanvasPropsWithSketch } from "@contracts/P5CanvasPropsWithSketch";
44
import { type SketchProps } from "@contracts/SketchProps";
55
import { logErrorBoundaryError } from "@utils/logErrorBoundaryError";
66
import { ReactNode } from "react";
77
import { FallbackProps } from "react-error-boundary";
88

9-
const ReactP5WrapperWithSketch = React.lazy(
10-
() => import("@components/ReactP5WrapperWithSketch")
9+
const P5CanvasWithSketch = React.lazy(
10+
() => import("@components/P5CanvasWithSketch")
1111
);
1212

1313
const ErrorBoundary = React.lazy(() =>
@@ -16,11 +16,11 @@ const ErrorBoundary = React.lazy(() =>
1616
}))
1717
);
1818

19-
const ReactP5WrapperGuard = <Props extends SketchProps>(
20-
props: P5WrapperProps<Props>
19+
const P5CanvasGuard = <Props extends SketchProps>(
20+
props: P5CanvasProps<Props>
2121
) => {
2222
if (props.sketch === undefined) {
23-
console.error("[ReactP5Wrapper] The `sketch` prop is required.");
23+
console.error("[P5Canvas] The `sketch` prop is required.");
2424

2525
return props.fallback?.() ?? null;
2626
}
@@ -43,13 +43,13 @@ const ReactP5WrapperGuard = <Props extends SketchProps>(
4343
props.loading?.() ?? <p data-testid="loading">🚀 Loading...</p>
4444
}
4545
>
46-
<ReactP5WrapperWithSketch
46+
<P5CanvasWithSketch
4747
/** @see https://github.com/P5-wrapper/react/discussions/360 */
48-
{...(props as unknown as P5WrapperPropsWithSketch<Props>)}
48+
{...(props as unknown as P5CanvasPropsWithSketch<Props>)}
4949
/>
5050
</React.Suspense>
5151
</ErrorBoundary>
5252
);
5353
};
5454

55-
export default ReactP5WrapperGuard;
55+
export default P5CanvasGuard;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as React from "react";
2+
import { CanvasContainerClassName } from "@constants/CanvasContainerClassName";
3+
import { type CanvasContainerRef } from "@contracts/CanvasContainerRef";
4+
import { type P5CanvasInstanceRef } from "@contracts/P5CanvasInstanceRef";
5+
import { type P5CanvasPropsWithSketch } from "@contracts/P5CanvasPropsWithSketch";
6+
import { type SketchProps } from "@contracts/SketchProps";
7+
import { removeP5CanvasInstance } from "@utils/removeP5CanvasInstance";
8+
import { updateP5CanvasInstance } from "@utils/updateP5CanvasInstance";
9+
import { withoutKeys } from "@utils/withoutKeys";
10+
11+
const P5CanvasWithSketch = <Props extends SketchProps>(
12+
props: P5CanvasPropsWithSketch<Props>
13+
) => {
14+
const canvasContainerRef: CanvasContainerRef = React.useRef(null);
15+
const p5CanvasInstanceRef: P5CanvasInstanceRef<Props> = React.useRef(null);
16+
const sketchProps: SketchProps = React.useMemo(
17+
() =>
18+
withoutKeys(props, [
19+
"sketch",
20+
"fallback",
21+
"loading",
22+
"error",
23+
"children"
24+
]),
25+
[props]
26+
);
27+
28+
React.useEffect(() => {
29+
p5CanvasInstanceRef.current = updateP5CanvasInstance(
30+
p5CanvasInstanceRef,
31+
canvasContainerRef,
32+
props.sketch
33+
);
34+
}, [props.sketch]);
35+
36+
React.useEffect(() => {
37+
/** @see https://github.com/P5-wrapper/react/discussions/360 */
38+
p5CanvasInstanceRef.current?.updateWithProps?.(
39+
sketchProps as unknown as Props
40+
);
41+
}, [sketchProps, canvasContainerRef, p5CanvasInstanceRef]);
42+
43+
React.useEffect(() => () => removeP5CanvasInstance(p5CanvasInstanceRef), []);
44+
45+
return (
46+
<div
47+
ref={canvasContainerRef}
48+
className={CanvasContainerClassName}
49+
data-testid="canvas-container"
50+
>
51+
{props.children}
52+
</div>
53+
);
54+
};
55+
56+
export default P5CanvasWithSketch;

src/components/ReactP5Wrapper.tsx

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/components/ReactP5WrapperWithSketch.tsx

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CanvasContainerClassName = "canvas-container";

src/constants/P5WrapperClassName.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)