-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathSpline.tsx
More file actions
173 lines (159 loc) · 4.18 KB
/
Spline.tsx
File metadata and controls
173 lines (159 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use client';
import { useEffect, useRef, useState, forwardRef } from 'react';
import { Application } from '@splinetool/runtime';
import type {
SPEObject,
SplineEvent,
SplineEventName,
} from '@splinetool/runtime';
import ParentSize from './ParentSize';
export type { SPEObject, SplineEvent, SplineEventName };
export interface SplineProps
extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onLoad'> {
scene: string;
onLoad?: (e: Application) => void;
onSplineMouseDown?: (e: SplineEvent) => void;
onSplineMouseUp?: (e: SplineEvent) => void;
onSplineMouseHover?: (e: SplineEvent) => void;
onSplineKeyDown?: (e: SplineEvent) => void;
onSplineKeyUp?: (e: SplineEvent) => void;
onSplineStart?: (e: SplineEvent) => void;
onSplineLookAt?: (e: SplineEvent) => void;
onSplineFollow?: (e: SplineEvent) => void;
onSplineScroll?: (e: SplineEvent) => void;
renderOnDemand?: boolean;
}
const Spline = forwardRef<HTMLDivElement, SplineProps>(
(
{
scene,
style,
onSplineMouseDown,
onSplineMouseUp,
onSplineMouseHover,
onSplineKeyDown,
onSplineKeyUp,
onSplineStart,
onSplineLookAt,
onSplineFollow,
onSplineScroll,
onLoad,
renderOnDemand = true,
children,
...props
},
ref
) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<Error>();
// We throw the error so ErrorBoundary can catch it
if (error) {
throw error;
}
// Initialize runtime when component is mounted
useEffect(() => {
setIsLoading(true);
let speApp: Application;
const events: {
name: SplineEventName;
cb?: (e: SplineEvent) => void;
options: AddEventListenerOptions;
}[] = [
{
name: 'mouseDown',
cb: onSplineMouseDown,
options: {},
},
{
name: 'mouseUp',
cb: onSplineMouseUp,
options: {},
},
{
name: 'mouseHover',
cb: onSplineMouseHover,
options: {},
},
{
name: 'keyDown',
cb: onSplineKeyDown,
options: {},
},
{
name: 'keyUp',
cb: onSplineKeyUp,
options: {},
},
{
name: 'start',
cb: onSplineStart,
options: {},
},
{
name: 'lookAt',
cb: onSplineLookAt,
options: { passive: true },
},
{
name: 'follow',
cb: onSplineFollow,
options: { passive: true },
},
{
name: 'scroll',
cb: onSplineScroll,
options: { passive: true },
},
];
if (canvasRef.current) {
speApp = new Application(canvasRef.current, { renderOnDemand });
async function init() {
await speApp.load(scene);
for (let event of events) {
if (event.cb) {
// @ts-expect-error: @splinetool/runtime doesn't have types for addEventListener options
speApp.addEventListener(event.name, event.cb, event.options);
}
}
setIsLoading(false);
onLoad?.(speApp);
}
init().catch((err) => {
setError(err as Error);
});
}
return () => {
for (let event of events) {
if (event.cb) {
// @ts-expect-error: @splinetool/runtime doesn't have types for addEventListener options
speApp.removeEventListener(event.name, event.cb, event.options);
}
}
speApp.dispose();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [scene]);
return (
<ParentSize
ref={ref}
parentSizeStyles={{ overflow: 'hidden', ...style }}
debounceTime={50}
{...props}
>
{() => (
<>
{isLoading && children}
<canvas
ref={canvasRef}
style={{
display: isLoading ? 'none' : 'block',
}}
/>
</>
)}
</ParentSize>
);
}
);
export default Spline;