11import diff from "microdiff" ;
22import p5 from "p5" ;
3- import React , { createRef , FC , memo , useState } from "react" ;
3+ import React , { createRef , memo , useState } from "react" ;
44import { useIsomorphicEffect } from "rooks" ;
55
66type Wrapper = HTMLDivElement ;
7+ type WithChildren < T = unknown > = T & { children ?: React . ReactNode } ;
8+ type InputProps < P extends SketchProps = SketchProps > = P & {
9+ sketch : Sketch < P > ;
10+ } ;
711
8- export interface SketchProps {
12+ export type Sketch < P extends SketchProps = SketchProps > = (
13+ instance : P5Instance < P >
14+ ) => void ;
15+ export type SketchProps = {
916 [ key : string ] : any ;
10- }
11-
12- export interface Sketch {
13- ( instance : P5Instance ) : void ;
14- }
15-
16- export interface P5WrapperProps extends SketchProps {
17- sketch : Sketch ;
18- }
19-
20- export interface P5Instance extends p5 {
21- updateWithProps ?: ( props : SketchProps ) => void ;
22- }
17+ } ;
18+ export type P5WrapperProps < P extends SketchProps = SketchProps > = WithChildren <
19+ InputProps < P >
20+ > ;
21+ export type P5Instance < P extends SketchProps = SketchProps > = p5 & {
22+ updateWithProps ?: ( props : P ) => void ;
23+ } ;
2324
2425function createCanvas ( sketch : Sketch , wrapper : Wrapper ) : P5Instance {
2526 return new p5 ( sketch , wrapper ) ;
2627}
2728
28- const ReactP5WrapperComponent : FC < P5WrapperProps > = ( {
29+ function ReactP5WrapperComponent < P extends SketchProps = SketchProps > ( {
2930 sketch,
3031 children,
3132 ...props
32- } ) => {
33+ } : P5WrapperProps < P > ) {
3334 const wrapperRef = createRef < Wrapper > ( ) ;
3435 const [ instance , setInstance ] = useState < P5Instance > ( ) ;
3536
@@ -50,9 +51,12 @@ const ReactP5WrapperComponent: FC<P5WrapperProps> = ({
5051 useIsomorphicEffect ( ( ) => ( ) => instance ?. remove ( ) , [ ] ) ;
5152
5253 return < div ref = { wrapperRef } > { children } </ div > ;
53- } ;
54+ }
5455
55- function propsAreEqual ( previous : P5WrapperProps , next : P5WrapperProps ) {
56+ function propsAreEqual < P extends SketchProps = SketchProps > (
57+ previous : P5WrapperProps < P > ,
58+ next : P5WrapperProps < P >
59+ ) {
5660 const differences = diff ( previous , next ) ;
5761
5862 return differences . length === 0 ;
0 commit comments