1
1
import diff from "microdiff" ;
2
2
import p5 from "p5" ;
3
- import React , { createRef , FC , memo , useState } from "react" ;
3
+ import React , { createRef , memo , useState } from "react" ;
4
4
import { useIsomorphicEffect } from "rooks" ;
5
5
6
6
type Wrapper = HTMLDivElement ;
7
+ type WithChildren < T = unknown > = T & { children ?: React . ReactNode } ;
8
+ type InputProps < P extends SketchProps = SketchProps > = P & {
9
+ sketch : Sketch < P > ;
10
+ } ;
7
11
8
- export interface SketchProps {
12
+ export type Sketch < P extends SketchProps = SketchProps > = (
13
+ instance : P5Instance < P >
14
+ ) => void ;
15
+ export type SketchProps = {
9
16
[ 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
+ } ;
23
24
24
25
function createCanvas ( sketch : Sketch , wrapper : Wrapper ) : P5Instance {
25
26
return new p5 ( sketch , wrapper ) ;
26
27
}
27
28
28
- const ReactP5WrapperComponent : FC < P5WrapperProps > = ( {
29
+ function ReactP5WrapperComponent < P extends SketchProps = SketchProps > ( {
29
30
sketch,
30
31
children,
31
32
...props
32
- } ) => {
33
+ } : P5WrapperProps < P > ) {
33
34
const wrapperRef = createRef < Wrapper > ( ) ;
34
35
const [ instance , setInstance ] = useState < P5Instance > ( ) ;
35
36
@@ -50,9 +51,12 @@ const ReactP5WrapperComponent: FC<P5WrapperProps> = ({
50
51
useIsomorphicEffect ( ( ) => ( ) => instance ?. remove ( ) , [ ] ) ;
51
52
52
53
return < div ref = { wrapperRef } > { children } </ div > ;
53
- } ;
54
+ }
54
55
55
- function propsAreEqual ( previous : P5WrapperProps , next : P5WrapperProps ) {
56
+ function propsAreEqual < P extends SketchProps = SketchProps > (
57
+ previous : P5WrapperProps < P > ,
58
+ next : P5WrapperProps < P >
59
+ ) {
56
60
const differences = diff ( previous , next ) ;
57
61
58
62
return differences . length === 0 ;
0 commit comments