r3f-motion 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +11 -0
- package/dist/cjs/index.js +465 -0
- package/dist/es/components/MotionCamera.mjs +31 -0
- package/dist/es/index.mjs +3 -0
- package/dist/es/render/gestures/use-hover.mjs +72 -0
- package/dist/es/render/gestures/use-tap.mjs +89 -0
- package/dist/es/render/motion.mjs +223 -0
- package/dist/es/render/use-render.mjs +68 -0
- package/dist/index.d.ts +52 -0
- package/package.json +100 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Framer B.V.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# React Three Fiber - Motion
|
|
2
|
+
|
|
3
|
+
### (React 19 version of framer-motion-3d)
|
|
4
|
+
|
|
5
|
+
Framer Motion is now [Motion](https://motion.dev/). Subsequently, and due to React19, the creators of Framer Motion deprecated [framer-motion-3d](https://motion.dev/docs/react-three-fiber).
|
|
6
|
+
|
|
7
|
+
This acts as a drop-in replacement for that. Targeting React 19 and using motion / motion/react under the hood.
|
|
8
|
+
|
|
9
|
+
## Storybook demo
|
|
10
|
+
|
|
11
|
+
[demo](https://r3f-motion.vercel.app/)
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var tslib = require('tslib');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var motion$1 = require('motion');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
var fiber = require('@react-three/fiber');
|
|
9
|
+
var three = require('three');
|
|
10
|
+
|
|
11
|
+
const useRender = (Component, props, forwardedRef, instanceRef, initialValues) => {
|
|
12
|
+
/**
|
|
13
|
+
* Create a callback ref that captures the Three.js instance
|
|
14
|
+
*/
|
|
15
|
+
const callbackRef = react.useCallback((instance) => {
|
|
16
|
+
if (!instance)
|
|
17
|
+
return;
|
|
18
|
+
// Apply initial values immediately to prevent FOUC
|
|
19
|
+
if (initialValues) {
|
|
20
|
+
// Property mapping configuration
|
|
21
|
+
const propertyMap = {
|
|
22
|
+
x: (val) => instance.position && (instance.position.x = val),
|
|
23
|
+
y: (val) => instance.position && (instance.position.y = val),
|
|
24
|
+
z: (val) => instance.position && (instance.position.z = val),
|
|
25
|
+
rotateX: (val) => instance.rotation && (instance.rotation.x = val),
|
|
26
|
+
rotateY: (val) => instance.rotation && (instance.rotation.y = val),
|
|
27
|
+
rotateZ: (val) => instance.rotation && (instance.rotation.z = val),
|
|
28
|
+
scale: (val) => instance.scale &&
|
|
29
|
+
instance.scale.set(val, val, val),
|
|
30
|
+
scaleX: (val) => instance.scale && (instance.scale.x = val),
|
|
31
|
+
scaleY: (val) => instance.scale && (instance.scale.y = val),
|
|
32
|
+
scaleZ: (val) => instance.scale && (instance.scale.z = val),
|
|
33
|
+
color: (val) => {
|
|
34
|
+
const color = instance.color;
|
|
35
|
+
if (color && color.set) {
|
|
36
|
+
color.set(val);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
opacity: (val) => instance.opacity !== undefined &&
|
|
40
|
+
(instance.opacity = val),
|
|
41
|
+
emissive: (val) => {
|
|
42
|
+
const emissive = instance.emissive;
|
|
43
|
+
if (emissive && emissive.set) {
|
|
44
|
+
emissive.set(val);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
emissiveIntensity: (val) => instance.emissiveIntensity !== undefined &&
|
|
48
|
+
(instance.emissiveIntensity = val),
|
|
49
|
+
roughness: (val) => instance.roughness !== undefined &&
|
|
50
|
+
(instance.roughness = val),
|
|
51
|
+
metalness: (val) => instance.metalness !== undefined &&
|
|
52
|
+
(instance.metalness = val),
|
|
53
|
+
};
|
|
54
|
+
for (const key in initialValues) {
|
|
55
|
+
const setter = propertyMap[key];
|
|
56
|
+
if (setter) {
|
|
57
|
+
setter(initialValues[key]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Store instance in the ref so animations can access it
|
|
62
|
+
instanceRef.current = instance;
|
|
63
|
+
// Call the forwarded ref if it exists
|
|
64
|
+
if (typeof forwardedRef === "function") {
|
|
65
|
+
forwardedRef(instance);
|
|
66
|
+
}
|
|
67
|
+
else if (forwardedRef) {
|
|
68
|
+
// eslint-disable-next-line react-hooks/immutability, @typescript-eslint/no-explicit-any
|
|
69
|
+
forwardedRef.current = instance;
|
|
70
|
+
}
|
|
71
|
+
}, [instanceRef, forwardedRef, initialValues]);
|
|
72
|
+
return react.createElement(Component, Object.assign({ ref: callbackRef }, props));
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function useHover(isStatic, props, options) {
|
|
76
|
+
const { whileHover, onHoverStart, onHoverEnd, onPointerOver, onPointerOut } = props;
|
|
77
|
+
const { captureInstanceState, buildTargetFromState, animateToTarget, resolveVariant, transition, } = options;
|
|
78
|
+
const preHoverStateRef = react.useRef(null);
|
|
79
|
+
const returnTimeoutRef = react.useRef(null);
|
|
80
|
+
const handlePointerEnter = react.useCallback((event) => {
|
|
81
|
+
if (whileHover) {
|
|
82
|
+
if (returnTimeoutRef.current) {
|
|
83
|
+
clearTimeout(returnTimeoutRef.current);
|
|
84
|
+
returnTimeoutRef.current = null;
|
|
85
|
+
}
|
|
86
|
+
if (!preHoverStateRef.current) {
|
|
87
|
+
preHoverStateRef.current = captureInstanceState();
|
|
88
|
+
}
|
|
89
|
+
const targetValues = typeof whileHover === "string"
|
|
90
|
+
? resolveVariant(whileHover)
|
|
91
|
+
: whileHover;
|
|
92
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
93
|
+
}
|
|
94
|
+
onHoverStart === null || onHoverStart === void 0 ? void 0 : onHoverStart(event.nativeEvent, {
|
|
95
|
+
point: {
|
|
96
|
+
x: event.nativeEvent.clientX,
|
|
97
|
+
y: event.nativeEvent.clientY,
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
onPointerOver === null || onPointerOver === void 0 ? void 0 : onPointerOver(event);
|
|
101
|
+
}, [
|
|
102
|
+
whileHover,
|
|
103
|
+
onHoverStart,
|
|
104
|
+
onPointerOver,
|
|
105
|
+
captureInstanceState,
|
|
106
|
+
resolveVariant,
|
|
107
|
+
animateToTarget,
|
|
108
|
+
transition,
|
|
109
|
+
]);
|
|
110
|
+
const handlePointerLeave = react.useCallback((event) => {
|
|
111
|
+
if (whileHover && preHoverStateRef.current) {
|
|
112
|
+
const targetValues = buildTargetFromState(preHoverStateRef.current);
|
|
113
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
114
|
+
returnTimeoutRef.current = setTimeout(() => {
|
|
115
|
+
preHoverStateRef.current = null;
|
|
116
|
+
returnTimeoutRef.current = null;
|
|
117
|
+
}, ((transition === null || transition === void 0 ? void 0 : transition.duration) || 0.2) * 1000);
|
|
118
|
+
}
|
|
119
|
+
onHoverEnd === null || onHoverEnd === void 0 ? void 0 : onHoverEnd(event.nativeEvent, {
|
|
120
|
+
point: {
|
|
121
|
+
x: event.nativeEvent.clientX,
|
|
122
|
+
y: event.nativeEvent.clientY,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
onPointerOut === null || onPointerOut === void 0 ? void 0 : onPointerOut(event);
|
|
126
|
+
}, [
|
|
127
|
+
whileHover,
|
|
128
|
+
onHoverEnd,
|
|
129
|
+
onPointerOut,
|
|
130
|
+
buildTargetFromState,
|
|
131
|
+
animateToTarget,
|
|
132
|
+
transition,
|
|
133
|
+
]);
|
|
134
|
+
const isHoverEnabled = whileHover || onHoverStart || onHoverEnd;
|
|
135
|
+
if (!isHoverEnabled)
|
|
136
|
+
return {};
|
|
137
|
+
return {
|
|
138
|
+
onPointerEnter: handlePointerEnter,
|
|
139
|
+
onPointerLeave: handlePointerLeave,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function useTap(isStatic, props, options) {
|
|
144
|
+
const { whileTap, onTapStart, onTap, onTapCancel, onPointerDown, onPointerUp, } = props;
|
|
145
|
+
const { captureInstanceState, buildTargetFromState, animateToTarget, resolveVariant, transition, } = options;
|
|
146
|
+
const preTapStateRef = react.useRef(null);
|
|
147
|
+
const returnTimeoutRef = react.useRef(null);
|
|
148
|
+
const handlePointerDown = react.useCallback((event) => {
|
|
149
|
+
if (whileTap) {
|
|
150
|
+
if (returnTimeoutRef.current) {
|
|
151
|
+
clearTimeout(returnTimeoutRef.current);
|
|
152
|
+
returnTimeoutRef.current = null;
|
|
153
|
+
}
|
|
154
|
+
if (!preTapStateRef.current) {
|
|
155
|
+
preTapStateRef.current = captureInstanceState();
|
|
156
|
+
}
|
|
157
|
+
const targetValues = typeof whileTap === "string"
|
|
158
|
+
? resolveVariant(whileTap)
|
|
159
|
+
: whileTap;
|
|
160
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
161
|
+
}
|
|
162
|
+
onTapStart === null || onTapStart === void 0 ? void 0 : onTapStart(event.nativeEvent, {
|
|
163
|
+
point: {
|
|
164
|
+
x: event.nativeEvent.clientX,
|
|
165
|
+
y: event.nativeEvent.clientY,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown(event);
|
|
169
|
+
}, [
|
|
170
|
+
whileTap,
|
|
171
|
+
onTapStart,
|
|
172
|
+
onPointerDown,
|
|
173
|
+
captureInstanceState,
|
|
174
|
+
resolveVariant,
|
|
175
|
+
animateToTarget,
|
|
176
|
+
transition,
|
|
177
|
+
]);
|
|
178
|
+
const handlePointerUp = react.useCallback((event) => {
|
|
179
|
+
if (whileTap && preTapStateRef.current) {
|
|
180
|
+
const targetValues = buildTargetFromState(preTapStateRef.current);
|
|
181
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
182
|
+
returnTimeoutRef.current = setTimeout(() => {
|
|
183
|
+
preTapStateRef.current = null;
|
|
184
|
+
returnTimeoutRef.current = null;
|
|
185
|
+
}, ((transition === null || transition === void 0 ? void 0 : transition.duration) || 0.2) * 1000);
|
|
186
|
+
onTap === null || onTap === void 0 ? void 0 : onTap(event.nativeEvent, {
|
|
187
|
+
point: {
|
|
188
|
+
x: event.nativeEvent.clientX,
|
|
189
|
+
y: event.nativeEvent.clientY,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp(event);
|
|
194
|
+
}, [
|
|
195
|
+
whileTap,
|
|
196
|
+
onTap,
|
|
197
|
+
onPointerUp,
|
|
198
|
+
buildTargetFromState,
|
|
199
|
+
animateToTarget,
|
|
200
|
+
transition,
|
|
201
|
+
]);
|
|
202
|
+
const handlePointerCancel = react.useCallback((event) => {
|
|
203
|
+
if (whileTap && preTapStateRef.current) {
|
|
204
|
+
const targetValues = buildTargetFromState(preTapStateRef.current);
|
|
205
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
206
|
+
returnTimeoutRef.current = setTimeout(() => {
|
|
207
|
+
preTapStateRef.current = null;
|
|
208
|
+
returnTimeoutRef.current = null;
|
|
209
|
+
}, ((transition === null || transition === void 0 ? void 0 : transition.duration) || 0.2) * 1000);
|
|
210
|
+
onTapCancel === null || onTapCancel === void 0 ? void 0 : onTapCancel(event.nativeEvent, {
|
|
211
|
+
point: {
|
|
212
|
+
x: event.nativeEvent.clientX,
|
|
213
|
+
y: event.nativeEvent.clientY,
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}, [whileTap, onTapCancel, buildTargetFromState, animateToTarget, transition]);
|
|
218
|
+
const isTapEnabled = onTap || onTapStart || onTapCancel || whileTap;
|
|
219
|
+
if (!isTapEnabled)
|
|
220
|
+
return {};
|
|
221
|
+
return {
|
|
222
|
+
onPointerDown: handlePointerDown,
|
|
223
|
+
onPointerUp: handlePointerUp,
|
|
224
|
+
onPointerCancel: handlePointerCancel,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const MotionContext = react.createContext(null);
|
|
229
|
+
function custom(Component) {
|
|
230
|
+
const MotionComponent = react.forwardRef((props, ref) => {
|
|
231
|
+
const instanceRef = react.useRef(null);
|
|
232
|
+
const animationRef = react.useRef(null);
|
|
233
|
+
const childIndexCounterRef = react.useRef(0);
|
|
234
|
+
const parentContext = react.useContext(MotionContext);
|
|
235
|
+
const childIndex = react.useMemo(() => { var _a, _b; return (_b = (_a = parentContext === null || parentContext === void 0 ? void 0 : parentContext.getNextChildIndex) === null || _a === void 0 ? void 0 : _a.call(parentContext)) !== null && _b !== void 0 ? _b : 0; },
|
|
236
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
237
|
+
[]);
|
|
238
|
+
const _a = props, { initial: initialProp, animate: animateProp, transition, variants, custom, inherit = true, children } = _a, restProps = tslib.__rest(_a, ["initial", "animate", "transition", "variants", "custom", "inherit", "children"]);
|
|
239
|
+
const initial = initialProp !== undefined
|
|
240
|
+
? initialProp
|
|
241
|
+
: inherit && (parentContext === null || parentContext === void 0 ? void 0 : parentContext.initial);
|
|
242
|
+
const animate = animateProp !== undefined
|
|
243
|
+
? animateProp
|
|
244
|
+
: inherit && (parentContext === null || parentContext === void 0 ? void 0 : parentContext.animate);
|
|
245
|
+
const effectiveVariants = variants || (inherit ? parentContext === null || parentContext === void 0 ? void 0 : parentContext.variants : undefined);
|
|
246
|
+
const effectiveCustom = custom !== undefined ? custom : parentContext === null || parentContext === void 0 ? void 0 : parentContext.custom;
|
|
247
|
+
const resolveVariant = react.useCallback((variantKey) => {
|
|
248
|
+
if (!effectiveVariants || !variantKey)
|
|
249
|
+
return null;
|
|
250
|
+
const variant = effectiveVariants[variantKey];
|
|
251
|
+
return typeof variant === "function"
|
|
252
|
+
? variant(effectiveCustom)
|
|
253
|
+
: variant;
|
|
254
|
+
}, [effectiveVariants, effectiveCustom]);
|
|
255
|
+
const captureInstanceState = react.useCallback(() => {
|
|
256
|
+
const instance = instanceRef.current;
|
|
257
|
+
if (!instance)
|
|
258
|
+
return null;
|
|
259
|
+
return {
|
|
260
|
+
position: {
|
|
261
|
+
x: instance.position.x,
|
|
262
|
+
y: instance.position.y,
|
|
263
|
+
z: instance.position.z,
|
|
264
|
+
},
|
|
265
|
+
rotation: {
|
|
266
|
+
x: instance.rotation.x,
|
|
267
|
+
y: instance.rotation.y,
|
|
268
|
+
z: instance.rotation.z,
|
|
269
|
+
},
|
|
270
|
+
scale: {
|
|
271
|
+
x: instance.scale.x,
|
|
272
|
+
y: instance.scale.y,
|
|
273
|
+
z: instance.scale.z,
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
}, []);
|
|
277
|
+
const buildTargetFromState = react.useCallback((capturedState) => ({
|
|
278
|
+
x: capturedState.position.x,
|
|
279
|
+
y: capturedState.position.y,
|
|
280
|
+
z: capturedState.position.z,
|
|
281
|
+
rotateX: capturedState.rotation.x,
|
|
282
|
+
rotateY: capturedState.rotation.y,
|
|
283
|
+
rotateZ: capturedState.rotation.z,
|
|
284
|
+
scale: capturedState.scale.x,
|
|
285
|
+
}), []);
|
|
286
|
+
const animateToTarget = react.useCallback((targetValues, options) => {
|
|
287
|
+
var _a;
|
|
288
|
+
const instance = instanceRef.current;
|
|
289
|
+
if (!instance || !targetValues)
|
|
290
|
+
return;
|
|
291
|
+
(_a = animationRef.current) === null || _a === void 0 ? void 0 : _a.stop();
|
|
292
|
+
const convertOptions = (opts) => {
|
|
293
|
+
if (!opts)
|
|
294
|
+
return { type: "tween" };
|
|
295
|
+
const base = {
|
|
296
|
+
type: opts.type === "spring"
|
|
297
|
+
? "spring"
|
|
298
|
+
: opts.type === "inertia"
|
|
299
|
+
? "inertia"
|
|
300
|
+
: "tween",
|
|
301
|
+
};
|
|
302
|
+
const validKeys = [
|
|
303
|
+
"duration",
|
|
304
|
+
"ease",
|
|
305
|
+
"delay",
|
|
306
|
+
"repeat",
|
|
307
|
+
"repeatType",
|
|
308
|
+
"repeatDelay",
|
|
309
|
+
"stiffness",
|
|
310
|
+
"damping",
|
|
311
|
+
"mass",
|
|
312
|
+
];
|
|
313
|
+
return Object.assign(base, ...validKeys
|
|
314
|
+
.filter((key) => opts[key] !== undefined)
|
|
315
|
+
.map((key) => ({ [key]: opts[key] })));
|
|
316
|
+
};
|
|
317
|
+
const getPropertyOpts = (key) => options && typeof options === "object" && key in options
|
|
318
|
+
? convertOptions(options[key])
|
|
319
|
+
: convertOptions(options);
|
|
320
|
+
const transformMap = {
|
|
321
|
+
x: { target: instance.position, prop: "x" },
|
|
322
|
+
y: { target: instance.position, prop: "y" },
|
|
323
|
+
z: { target: instance.position, prop: "z" },
|
|
324
|
+
rotateX: { target: instance.rotation, prop: "x" },
|
|
325
|
+
rotateY: { target: instance.rotation, prop: "y" },
|
|
326
|
+
rotateZ: { target: instance.rotation, prop: "z" },
|
|
327
|
+
scale: { target: instance.scale, prop: "x", multi: true },
|
|
328
|
+
scaleX: { target: instance.scale, prop: "x" },
|
|
329
|
+
scaleY: { target: instance.scale, prop: "y" },
|
|
330
|
+
scaleZ: { target: instance.scale, prop: "z" },
|
|
331
|
+
};
|
|
332
|
+
const animations = [];
|
|
333
|
+
const animateColor = (target, value, opts) => {
|
|
334
|
+
const ColorConstructor = target.constructor;
|
|
335
|
+
const tempColor = new ColorConstructor(value);
|
|
336
|
+
["r", "g", "b"].forEach((channel) => animations.push(motion$1.animate(target, { [channel]: tempColor[channel] }, opts)));
|
|
337
|
+
};
|
|
338
|
+
Object.entries(targetValues).forEach(([key, value]) => {
|
|
339
|
+
const opts = getPropertyOpts(key);
|
|
340
|
+
const mapping = transformMap[key];
|
|
341
|
+
if (mapping === null || mapping === void 0 ? void 0 : mapping.target) {
|
|
342
|
+
if (mapping.multi) {
|
|
343
|
+
["x", "y", "z"].forEach((axis) => animations.push(motion$1.animate(mapping.target, { [axis]: value }, opts)));
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
animations.push(motion$1.animate(mapping.target, { [mapping.prop]: value }, opts));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
else if (key === "color" && instance.color) {
|
|
350
|
+
animateColor(instance.color, value, opts);
|
|
351
|
+
}
|
|
352
|
+
else if (key === "emissive" && instance.emissive) {
|
|
353
|
+
animateColor(instance.emissive, value, opts);
|
|
354
|
+
}
|
|
355
|
+
else if (key === "opacity" && instance.opacity !== undefined) {
|
|
356
|
+
animations.push(motion$1.animate(instance, { opacity: value }, opts));
|
|
357
|
+
}
|
|
358
|
+
else if (key === "emissiveIntensity" &&
|
|
359
|
+
instance.emissiveIntensity !== undefined) {
|
|
360
|
+
animations.push(motion$1.animate(instance, { emissiveIntensity: value }, opts));
|
|
361
|
+
}
|
|
362
|
+
else if (key === "roughness" &&
|
|
363
|
+
instance.roughness !== undefined) {
|
|
364
|
+
animations.push(motion$1.animate(instance, { roughness: value }, opts));
|
|
365
|
+
}
|
|
366
|
+
else if (key === "metalness" &&
|
|
367
|
+
instance.metalness !== undefined) {
|
|
368
|
+
animations.push(motion$1.animate(instance, { metalness: value }, opts));
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
animationRef.current = {
|
|
372
|
+
stop: () => animations.forEach((anim) => { var _a; return (_a = anim.stop) === null || _a === void 0 ? void 0 : _a.call(anim); }),
|
|
373
|
+
};
|
|
374
|
+
}, []);
|
|
375
|
+
react.useEffect(() => {
|
|
376
|
+
if (!instanceRef.current || !animate)
|
|
377
|
+
return;
|
|
378
|
+
const resolved = typeof animate === "string" ? resolveVariant(animate) : animate;
|
|
379
|
+
if (!resolved)
|
|
380
|
+
return;
|
|
381
|
+
const resolvedObj = resolved;
|
|
382
|
+
const { transition: variantTransition } = resolvedObj, targetValues = tslib.__rest(resolvedObj, ["transition"]);
|
|
383
|
+
let effectiveTransition = variantTransition || transition;
|
|
384
|
+
const parentTrans = parentContext === null || parentContext === void 0 ? void 0 : parentContext.transition;
|
|
385
|
+
if ((parentTrans === null || parentTrans === void 0 ? void 0 : parentTrans.delayChildren) !== undefined ||
|
|
386
|
+
(parentTrans === null || parentTrans === void 0 ? void 0 : parentTrans.staggerChildren) !== undefined) {
|
|
387
|
+
const orchestrationDelay = (parentTrans.delayChildren || 0) +
|
|
388
|
+
childIndex * (parentTrans.staggerChildren || 0);
|
|
389
|
+
effectiveTransition = Object.assign(Object.assign({}, effectiveTransition), { delay: ((effectiveTransition === null || effectiveTransition === void 0 ? void 0 : effectiveTransition.delay) || 0) + orchestrationDelay });
|
|
390
|
+
}
|
|
391
|
+
animateToTarget(targetValues, effectiveTransition);
|
|
392
|
+
return () => { var _a; return (_a = animationRef.current) === null || _a === void 0 ? void 0 : _a.stop(); };
|
|
393
|
+
}, [
|
|
394
|
+
animate,
|
|
395
|
+
transition,
|
|
396
|
+
resolveVariant,
|
|
397
|
+
animateToTarget,
|
|
398
|
+
childIndex,
|
|
399
|
+
parentContext,
|
|
400
|
+
]);
|
|
401
|
+
const gestureProps = {
|
|
402
|
+
captureInstanceState,
|
|
403
|
+
buildTargetFromState,
|
|
404
|
+
animateToTarget,
|
|
405
|
+
resolveVariant,
|
|
406
|
+
transition,
|
|
407
|
+
};
|
|
408
|
+
const gestureHandlers = Object.assign(Object.assign({}, useHover(false, props, gestureProps)), useTap(false, props, gestureProps));
|
|
409
|
+
const resolvedInitialValues = typeof initial === "string" ? resolveVariant(initial) : initial;
|
|
410
|
+
const element = useRender(Component, Object.assign(Object.assign(Object.assign({}, restProps), gestureHandlers), { children }), ref, instanceRef, resolvedInitialValues);
|
|
411
|
+
const getNextChildIndex = react.useCallback(() => {
|
|
412
|
+
const index = childIndexCounterRef.current;
|
|
413
|
+
childIndexCounterRef.current += 1;
|
|
414
|
+
return index;
|
|
415
|
+
}, []);
|
|
416
|
+
if (initial !== undefined || animate !== undefined || effectiveVariants) {
|
|
417
|
+
const contextValue = {
|
|
418
|
+
initial,
|
|
419
|
+
animate,
|
|
420
|
+
variants: effectiveVariants,
|
|
421
|
+
transition,
|
|
422
|
+
custom: effectiveCustom,
|
|
423
|
+
getNextChildIndex,
|
|
424
|
+
};
|
|
425
|
+
return react.createElement(MotionContext.Provider, { value: contextValue }, element);
|
|
426
|
+
}
|
|
427
|
+
return element;
|
|
428
|
+
});
|
|
429
|
+
MotionComponent.displayName = `Motion(${Component})`;
|
|
430
|
+
return MotionComponent;
|
|
431
|
+
}
|
|
432
|
+
const componentCache = new Map();
|
|
433
|
+
const motion = new Proxy(custom, {
|
|
434
|
+
get: (_, key) => {
|
|
435
|
+
if (!componentCache.has(key)) {
|
|
436
|
+
componentCache.set(key, custom(key));
|
|
437
|
+
}
|
|
438
|
+
return componentCache.get(key);
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
const MotionCamera = (props) => {
|
|
443
|
+
const { fov = 75, near = 0.1, far = 1000, type = "perspective" } = props, restProps = tslib.__rest(props, ["fov", "near", "far", "type"]);
|
|
444
|
+
const { size, set } = fiber.useThree();
|
|
445
|
+
const camera = react.useMemo(() => {
|
|
446
|
+
const aspect = size.width / size.height;
|
|
447
|
+
let cam;
|
|
448
|
+
if (type === "perspective") {
|
|
449
|
+
cam = new three.PerspectiveCamera(fov, aspect, near, far);
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
const frustumSize = 10;
|
|
453
|
+
cam = new three.OrthographicCamera((-frustumSize * aspect) / 2, (frustumSize * aspect) / 2, frustumSize / 2, -frustumSize / 2, near, far);
|
|
454
|
+
}
|
|
455
|
+
cam.updateProjectionMatrix();
|
|
456
|
+
return cam;
|
|
457
|
+
}, [type, fov, near, far, size.width, size.height]);
|
|
458
|
+
react.useLayoutEffect(() => {
|
|
459
|
+
set({ camera });
|
|
460
|
+
}, [camera, set]);
|
|
461
|
+
return jsxRuntime.jsx(motion.primitive, Object.assign({ object: camera }, restProps));
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
exports.MotionCamera = MotionCamera;
|
|
465
|
+
exports.motion = motion;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { __rest } from 'tslib';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
import { useMemo, useLayoutEffect } from 'react';
|
|
5
|
+
import { useThree } from '@react-three/fiber';
|
|
6
|
+
import { motion } from '../render/motion.mjs';
|
|
7
|
+
import { PerspectiveCamera, OrthographicCamera } from 'three';
|
|
8
|
+
|
|
9
|
+
const MotionCamera = (props) => {
|
|
10
|
+
const { fov = 75, near = 0.1, far = 1000, type = "perspective" } = props, restProps = __rest(props, ["fov", "near", "far", "type"]);
|
|
11
|
+
const { size, set } = useThree();
|
|
12
|
+
const camera = useMemo(() => {
|
|
13
|
+
const aspect = size.width / size.height;
|
|
14
|
+
let cam;
|
|
15
|
+
if (type === "perspective") {
|
|
16
|
+
cam = new PerspectiveCamera(fov, aspect, near, far);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
const frustumSize = 10;
|
|
20
|
+
cam = new OrthographicCamera((-frustumSize * aspect) / 2, (frustumSize * aspect) / 2, frustumSize / 2, -frustumSize / 2, near, far);
|
|
21
|
+
}
|
|
22
|
+
cam.updateProjectionMatrix();
|
|
23
|
+
return cam;
|
|
24
|
+
}, [type, fov, near, far, size.width, size.height]);
|
|
25
|
+
useLayoutEffect(() => {
|
|
26
|
+
set({ camera });
|
|
27
|
+
}, [camera, set]);
|
|
28
|
+
return jsx(motion.primitive, Object.assign({ object: camera }, restProps));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { MotionCamera as default };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRef, useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
function useHover(isStatic, props, options) {
|
|
5
|
+
const { whileHover, onHoverStart, onHoverEnd, onPointerOver, onPointerOut } = props;
|
|
6
|
+
const { captureInstanceState, buildTargetFromState, animateToTarget, resolveVariant, transition, } = options;
|
|
7
|
+
const preHoverStateRef = useRef(null);
|
|
8
|
+
const returnTimeoutRef = useRef(null);
|
|
9
|
+
const handlePointerEnter = useCallback((event) => {
|
|
10
|
+
if (whileHover) {
|
|
11
|
+
if (returnTimeoutRef.current) {
|
|
12
|
+
clearTimeout(returnTimeoutRef.current);
|
|
13
|
+
returnTimeoutRef.current = null;
|
|
14
|
+
}
|
|
15
|
+
if (!preHoverStateRef.current) {
|
|
16
|
+
preHoverStateRef.current = captureInstanceState();
|
|
17
|
+
}
|
|
18
|
+
const targetValues = typeof whileHover === "string"
|
|
19
|
+
? resolveVariant(whileHover)
|
|
20
|
+
: whileHover;
|
|
21
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
22
|
+
}
|
|
23
|
+
onHoverStart === null || onHoverStart === void 0 ? void 0 : onHoverStart(event.nativeEvent, {
|
|
24
|
+
point: {
|
|
25
|
+
x: event.nativeEvent.clientX,
|
|
26
|
+
y: event.nativeEvent.clientY,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
onPointerOver === null || onPointerOver === void 0 ? void 0 : onPointerOver(event);
|
|
30
|
+
}, [
|
|
31
|
+
whileHover,
|
|
32
|
+
onHoverStart,
|
|
33
|
+
onPointerOver,
|
|
34
|
+
captureInstanceState,
|
|
35
|
+
resolveVariant,
|
|
36
|
+
animateToTarget,
|
|
37
|
+
transition,
|
|
38
|
+
]);
|
|
39
|
+
const handlePointerLeave = useCallback((event) => {
|
|
40
|
+
if (whileHover && preHoverStateRef.current) {
|
|
41
|
+
const targetValues = buildTargetFromState(preHoverStateRef.current);
|
|
42
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
43
|
+
returnTimeoutRef.current = setTimeout(() => {
|
|
44
|
+
preHoverStateRef.current = null;
|
|
45
|
+
returnTimeoutRef.current = null;
|
|
46
|
+
}, ((transition === null || transition === void 0 ? void 0 : transition.duration) || 0.2) * 1000);
|
|
47
|
+
}
|
|
48
|
+
onHoverEnd === null || onHoverEnd === void 0 ? void 0 : onHoverEnd(event.nativeEvent, {
|
|
49
|
+
point: {
|
|
50
|
+
x: event.nativeEvent.clientX,
|
|
51
|
+
y: event.nativeEvent.clientY,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
onPointerOut === null || onPointerOut === void 0 ? void 0 : onPointerOut(event);
|
|
55
|
+
}, [
|
|
56
|
+
whileHover,
|
|
57
|
+
onHoverEnd,
|
|
58
|
+
onPointerOut,
|
|
59
|
+
buildTargetFromState,
|
|
60
|
+
animateToTarget,
|
|
61
|
+
transition,
|
|
62
|
+
]);
|
|
63
|
+
const isHoverEnabled = whileHover || onHoverStart || onHoverEnd;
|
|
64
|
+
if (!isHoverEnabled)
|
|
65
|
+
return {};
|
|
66
|
+
return {
|
|
67
|
+
onPointerEnter: handlePointerEnter,
|
|
68
|
+
onPointerLeave: handlePointerLeave,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { useHover };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRef, useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
function useTap(isStatic, props, options) {
|
|
5
|
+
const { whileTap, onTapStart, onTap, onTapCancel, onPointerDown, onPointerUp, } = props;
|
|
6
|
+
const { captureInstanceState, buildTargetFromState, animateToTarget, resolveVariant, transition, } = options;
|
|
7
|
+
const preTapStateRef = useRef(null);
|
|
8
|
+
const returnTimeoutRef = useRef(null);
|
|
9
|
+
const handlePointerDown = useCallback((event) => {
|
|
10
|
+
if (whileTap) {
|
|
11
|
+
if (returnTimeoutRef.current) {
|
|
12
|
+
clearTimeout(returnTimeoutRef.current);
|
|
13
|
+
returnTimeoutRef.current = null;
|
|
14
|
+
}
|
|
15
|
+
if (!preTapStateRef.current) {
|
|
16
|
+
preTapStateRef.current = captureInstanceState();
|
|
17
|
+
}
|
|
18
|
+
const targetValues = typeof whileTap === "string"
|
|
19
|
+
? resolveVariant(whileTap)
|
|
20
|
+
: whileTap;
|
|
21
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
22
|
+
}
|
|
23
|
+
onTapStart === null || onTapStart === void 0 ? void 0 : onTapStart(event.nativeEvent, {
|
|
24
|
+
point: {
|
|
25
|
+
x: event.nativeEvent.clientX,
|
|
26
|
+
y: event.nativeEvent.clientY,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown(event);
|
|
30
|
+
}, [
|
|
31
|
+
whileTap,
|
|
32
|
+
onTapStart,
|
|
33
|
+
onPointerDown,
|
|
34
|
+
captureInstanceState,
|
|
35
|
+
resolveVariant,
|
|
36
|
+
animateToTarget,
|
|
37
|
+
transition,
|
|
38
|
+
]);
|
|
39
|
+
const handlePointerUp = useCallback((event) => {
|
|
40
|
+
if (whileTap && preTapStateRef.current) {
|
|
41
|
+
const targetValues = buildTargetFromState(preTapStateRef.current);
|
|
42
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
43
|
+
returnTimeoutRef.current = setTimeout(() => {
|
|
44
|
+
preTapStateRef.current = null;
|
|
45
|
+
returnTimeoutRef.current = null;
|
|
46
|
+
}, ((transition === null || transition === void 0 ? void 0 : transition.duration) || 0.2) * 1000);
|
|
47
|
+
onTap === null || onTap === void 0 ? void 0 : onTap(event.nativeEvent, {
|
|
48
|
+
point: {
|
|
49
|
+
x: event.nativeEvent.clientX,
|
|
50
|
+
y: event.nativeEvent.clientY,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp(event);
|
|
55
|
+
}, [
|
|
56
|
+
whileTap,
|
|
57
|
+
onTap,
|
|
58
|
+
onPointerUp,
|
|
59
|
+
buildTargetFromState,
|
|
60
|
+
animateToTarget,
|
|
61
|
+
transition,
|
|
62
|
+
]);
|
|
63
|
+
const handlePointerCancel = useCallback((event) => {
|
|
64
|
+
if (whileTap && preTapStateRef.current) {
|
|
65
|
+
const targetValues = buildTargetFromState(preTapStateRef.current);
|
|
66
|
+
animateToTarget(targetValues, transition || { duration: 0.2 });
|
|
67
|
+
returnTimeoutRef.current = setTimeout(() => {
|
|
68
|
+
preTapStateRef.current = null;
|
|
69
|
+
returnTimeoutRef.current = null;
|
|
70
|
+
}, ((transition === null || transition === void 0 ? void 0 : transition.duration) || 0.2) * 1000);
|
|
71
|
+
onTapCancel === null || onTapCancel === void 0 ? void 0 : onTapCancel(event.nativeEvent, {
|
|
72
|
+
point: {
|
|
73
|
+
x: event.nativeEvent.clientX,
|
|
74
|
+
y: event.nativeEvent.clientY,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}, [whileTap, onTapCancel, buildTargetFromState, animateToTarget, transition]);
|
|
79
|
+
const isTapEnabled = onTap || onTapStart || onTapCancel || whileTap;
|
|
80
|
+
if (!isTapEnabled)
|
|
81
|
+
return {};
|
|
82
|
+
return {
|
|
83
|
+
onPointerDown: handlePointerDown,
|
|
84
|
+
onPointerUp: handlePointerUp,
|
|
85
|
+
onPointerCancel: handlePointerCancel,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { useTap };
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { __rest } from 'tslib';
|
|
3
|
+
import { createContext, forwardRef, useRef, useContext, useMemo, useCallback, useEffect, createElement } from 'react';
|
|
4
|
+
import { animate } from 'motion';
|
|
5
|
+
import { useRender } from './use-render.mjs';
|
|
6
|
+
import { useHover } from './gestures/use-hover.mjs';
|
|
7
|
+
import { useTap } from './gestures/use-tap.mjs';
|
|
8
|
+
|
|
9
|
+
const MotionContext = createContext(null);
|
|
10
|
+
function custom(Component) {
|
|
11
|
+
const MotionComponent = forwardRef((props, ref) => {
|
|
12
|
+
const instanceRef = useRef(null);
|
|
13
|
+
const animationRef = useRef(null);
|
|
14
|
+
const childIndexCounterRef = useRef(0);
|
|
15
|
+
const parentContext = useContext(MotionContext);
|
|
16
|
+
const childIndex = useMemo(() => { var _a, _b; return (_b = (_a = parentContext === null || parentContext === void 0 ? void 0 : parentContext.getNextChildIndex) === null || _a === void 0 ? void 0 : _a.call(parentContext)) !== null && _b !== void 0 ? _b : 0; },
|
|
17
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
+
[]);
|
|
19
|
+
const _a = props, { initial: initialProp, animate: animateProp, transition, variants, custom, inherit = true, children } = _a, restProps = __rest(_a, ["initial", "animate", "transition", "variants", "custom", "inherit", "children"]);
|
|
20
|
+
const initial = initialProp !== undefined
|
|
21
|
+
? initialProp
|
|
22
|
+
: inherit && (parentContext === null || parentContext === void 0 ? void 0 : parentContext.initial);
|
|
23
|
+
const animate$1 = animateProp !== undefined
|
|
24
|
+
? animateProp
|
|
25
|
+
: inherit && (parentContext === null || parentContext === void 0 ? void 0 : parentContext.animate);
|
|
26
|
+
const effectiveVariants = variants || (inherit ? parentContext === null || parentContext === void 0 ? void 0 : parentContext.variants : undefined);
|
|
27
|
+
const effectiveCustom = custom !== undefined ? custom : parentContext === null || parentContext === void 0 ? void 0 : parentContext.custom;
|
|
28
|
+
const resolveVariant = useCallback((variantKey) => {
|
|
29
|
+
if (!effectiveVariants || !variantKey)
|
|
30
|
+
return null;
|
|
31
|
+
const variant = effectiveVariants[variantKey];
|
|
32
|
+
return typeof variant === "function"
|
|
33
|
+
? variant(effectiveCustom)
|
|
34
|
+
: variant;
|
|
35
|
+
}, [effectiveVariants, effectiveCustom]);
|
|
36
|
+
const captureInstanceState = useCallback(() => {
|
|
37
|
+
const instance = instanceRef.current;
|
|
38
|
+
if (!instance)
|
|
39
|
+
return null;
|
|
40
|
+
return {
|
|
41
|
+
position: {
|
|
42
|
+
x: instance.position.x,
|
|
43
|
+
y: instance.position.y,
|
|
44
|
+
z: instance.position.z,
|
|
45
|
+
},
|
|
46
|
+
rotation: {
|
|
47
|
+
x: instance.rotation.x,
|
|
48
|
+
y: instance.rotation.y,
|
|
49
|
+
z: instance.rotation.z,
|
|
50
|
+
},
|
|
51
|
+
scale: {
|
|
52
|
+
x: instance.scale.x,
|
|
53
|
+
y: instance.scale.y,
|
|
54
|
+
z: instance.scale.z,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}, []);
|
|
58
|
+
const buildTargetFromState = useCallback((capturedState) => ({
|
|
59
|
+
x: capturedState.position.x,
|
|
60
|
+
y: capturedState.position.y,
|
|
61
|
+
z: capturedState.position.z,
|
|
62
|
+
rotateX: capturedState.rotation.x,
|
|
63
|
+
rotateY: capturedState.rotation.y,
|
|
64
|
+
rotateZ: capturedState.rotation.z,
|
|
65
|
+
scale: capturedState.scale.x,
|
|
66
|
+
}), []);
|
|
67
|
+
const animateToTarget = useCallback((targetValues, options) => {
|
|
68
|
+
var _a;
|
|
69
|
+
const instance = instanceRef.current;
|
|
70
|
+
if (!instance || !targetValues)
|
|
71
|
+
return;
|
|
72
|
+
(_a = animationRef.current) === null || _a === void 0 ? void 0 : _a.stop();
|
|
73
|
+
const convertOptions = (opts) => {
|
|
74
|
+
if (!opts)
|
|
75
|
+
return { type: "tween" };
|
|
76
|
+
const base = {
|
|
77
|
+
type: opts.type === "spring"
|
|
78
|
+
? "spring"
|
|
79
|
+
: opts.type === "inertia"
|
|
80
|
+
? "inertia"
|
|
81
|
+
: "tween",
|
|
82
|
+
};
|
|
83
|
+
const validKeys = [
|
|
84
|
+
"duration",
|
|
85
|
+
"ease",
|
|
86
|
+
"delay",
|
|
87
|
+
"repeat",
|
|
88
|
+
"repeatType",
|
|
89
|
+
"repeatDelay",
|
|
90
|
+
"stiffness",
|
|
91
|
+
"damping",
|
|
92
|
+
"mass",
|
|
93
|
+
];
|
|
94
|
+
return Object.assign(base, ...validKeys
|
|
95
|
+
.filter((key) => opts[key] !== undefined)
|
|
96
|
+
.map((key) => ({ [key]: opts[key] })));
|
|
97
|
+
};
|
|
98
|
+
const getPropertyOpts = (key) => options && typeof options === "object" && key in options
|
|
99
|
+
? convertOptions(options[key])
|
|
100
|
+
: convertOptions(options);
|
|
101
|
+
const transformMap = {
|
|
102
|
+
x: { target: instance.position, prop: "x" },
|
|
103
|
+
y: { target: instance.position, prop: "y" },
|
|
104
|
+
z: { target: instance.position, prop: "z" },
|
|
105
|
+
rotateX: { target: instance.rotation, prop: "x" },
|
|
106
|
+
rotateY: { target: instance.rotation, prop: "y" },
|
|
107
|
+
rotateZ: { target: instance.rotation, prop: "z" },
|
|
108
|
+
scale: { target: instance.scale, prop: "x", multi: true },
|
|
109
|
+
scaleX: { target: instance.scale, prop: "x" },
|
|
110
|
+
scaleY: { target: instance.scale, prop: "y" },
|
|
111
|
+
scaleZ: { target: instance.scale, prop: "z" },
|
|
112
|
+
};
|
|
113
|
+
const animations = [];
|
|
114
|
+
const animateColor = (target, value, opts) => {
|
|
115
|
+
const ColorConstructor = target.constructor;
|
|
116
|
+
const tempColor = new ColorConstructor(value);
|
|
117
|
+
["r", "g", "b"].forEach((channel) => animations.push(animate(target, { [channel]: tempColor[channel] }, opts)));
|
|
118
|
+
};
|
|
119
|
+
Object.entries(targetValues).forEach(([key, value]) => {
|
|
120
|
+
const opts = getPropertyOpts(key);
|
|
121
|
+
const mapping = transformMap[key];
|
|
122
|
+
if (mapping === null || mapping === void 0 ? void 0 : mapping.target) {
|
|
123
|
+
if (mapping.multi) {
|
|
124
|
+
["x", "y", "z"].forEach((axis) => animations.push(animate(mapping.target, { [axis]: value }, opts)));
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
animations.push(animate(mapping.target, { [mapping.prop]: value }, opts));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else if (key === "color" && instance.color) {
|
|
131
|
+
animateColor(instance.color, value, opts);
|
|
132
|
+
}
|
|
133
|
+
else if (key === "emissive" && instance.emissive) {
|
|
134
|
+
animateColor(instance.emissive, value, opts);
|
|
135
|
+
}
|
|
136
|
+
else if (key === "opacity" && instance.opacity !== undefined) {
|
|
137
|
+
animations.push(animate(instance, { opacity: value }, opts));
|
|
138
|
+
}
|
|
139
|
+
else if (key === "emissiveIntensity" &&
|
|
140
|
+
instance.emissiveIntensity !== undefined) {
|
|
141
|
+
animations.push(animate(instance, { emissiveIntensity: value }, opts));
|
|
142
|
+
}
|
|
143
|
+
else if (key === "roughness" &&
|
|
144
|
+
instance.roughness !== undefined) {
|
|
145
|
+
animations.push(animate(instance, { roughness: value }, opts));
|
|
146
|
+
}
|
|
147
|
+
else if (key === "metalness" &&
|
|
148
|
+
instance.metalness !== undefined) {
|
|
149
|
+
animations.push(animate(instance, { metalness: value }, opts));
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
animationRef.current = {
|
|
153
|
+
stop: () => animations.forEach((anim) => { var _a; return (_a = anim.stop) === null || _a === void 0 ? void 0 : _a.call(anim); }),
|
|
154
|
+
};
|
|
155
|
+
}, []);
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
if (!instanceRef.current || !animate$1)
|
|
158
|
+
return;
|
|
159
|
+
const resolved = typeof animate$1 === "string" ? resolveVariant(animate$1) : animate$1;
|
|
160
|
+
if (!resolved)
|
|
161
|
+
return;
|
|
162
|
+
const resolvedObj = resolved;
|
|
163
|
+
const { transition: variantTransition } = resolvedObj, targetValues = __rest(resolvedObj, ["transition"]);
|
|
164
|
+
let effectiveTransition = variantTransition || transition;
|
|
165
|
+
const parentTrans = parentContext === null || parentContext === void 0 ? void 0 : parentContext.transition;
|
|
166
|
+
if ((parentTrans === null || parentTrans === void 0 ? void 0 : parentTrans.delayChildren) !== undefined ||
|
|
167
|
+
(parentTrans === null || parentTrans === void 0 ? void 0 : parentTrans.staggerChildren) !== undefined) {
|
|
168
|
+
const orchestrationDelay = (parentTrans.delayChildren || 0) +
|
|
169
|
+
childIndex * (parentTrans.staggerChildren || 0);
|
|
170
|
+
effectiveTransition = Object.assign(Object.assign({}, effectiveTransition), { delay: ((effectiveTransition === null || effectiveTransition === void 0 ? void 0 : effectiveTransition.delay) || 0) + orchestrationDelay });
|
|
171
|
+
}
|
|
172
|
+
animateToTarget(targetValues, effectiveTransition);
|
|
173
|
+
return () => { var _a; return (_a = animationRef.current) === null || _a === void 0 ? void 0 : _a.stop(); };
|
|
174
|
+
}, [
|
|
175
|
+
animate$1,
|
|
176
|
+
transition,
|
|
177
|
+
resolveVariant,
|
|
178
|
+
animateToTarget,
|
|
179
|
+
childIndex,
|
|
180
|
+
parentContext,
|
|
181
|
+
]);
|
|
182
|
+
const gestureProps = {
|
|
183
|
+
captureInstanceState,
|
|
184
|
+
buildTargetFromState,
|
|
185
|
+
animateToTarget,
|
|
186
|
+
resolveVariant,
|
|
187
|
+
transition,
|
|
188
|
+
};
|
|
189
|
+
const gestureHandlers = Object.assign(Object.assign({}, useHover(false, props, gestureProps)), useTap(false, props, gestureProps));
|
|
190
|
+
const resolvedInitialValues = typeof initial === "string" ? resolveVariant(initial) : initial;
|
|
191
|
+
const element = useRender(Component, Object.assign(Object.assign(Object.assign({}, restProps), gestureHandlers), { children }), ref, instanceRef, resolvedInitialValues);
|
|
192
|
+
const getNextChildIndex = useCallback(() => {
|
|
193
|
+
const index = childIndexCounterRef.current;
|
|
194
|
+
childIndexCounterRef.current += 1;
|
|
195
|
+
return index;
|
|
196
|
+
}, []);
|
|
197
|
+
if (initial !== undefined || animate$1 !== undefined || effectiveVariants) {
|
|
198
|
+
const contextValue = {
|
|
199
|
+
initial,
|
|
200
|
+
animate: animate$1,
|
|
201
|
+
variants: effectiveVariants,
|
|
202
|
+
transition,
|
|
203
|
+
custom: effectiveCustom,
|
|
204
|
+
getNextChildIndex,
|
|
205
|
+
};
|
|
206
|
+
return createElement(MotionContext.Provider, { value: contextValue }, element);
|
|
207
|
+
}
|
|
208
|
+
return element;
|
|
209
|
+
});
|
|
210
|
+
MotionComponent.displayName = `Motion(${Component})`;
|
|
211
|
+
return MotionComponent;
|
|
212
|
+
}
|
|
213
|
+
const componentCache = new Map();
|
|
214
|
+
const motion = new Proxy(custom, {
|
|
215
|
+
get: (_, key) => {
|
|
216
|
+
if (!componentCache.has(key)) {
|
|
217
|
+
componentCache.set(key, custom(key));
|
|
218
|
+
}
|
|
219
|
+
return componentCache.get(key);
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
export { motion };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, createElement } from 'react';
|
|
3
|
+
|
|
4
|
+
const useRender = (Component, props, forwardedRef, instanceRef, initialValues) => {
|
|
5
|
+
/**
|
|
6
|
+
* Create a callback ref that captures the Three.js instance
|
|
7
|
+
*/
|
|
8
|
+
const callbackRef = useCallback((instance) => {
|
|
9
|
+
if (!instance)
|
|
10
|
+
return;
|
|
11
|
+
// Apply initial values immediately to prevent FOUC
|
|
12
|
+
if (initialValues) {
|
|
13
|
+
// Property mapping configuration
|
|
14
|
+
const propertyMap = {
|
|
15
|
+
x: (val) => instance.position && (instance.position.x = val),
|
|
16
|
+
y: (val) => instance.position && (instance.position.y = val),
|
|
17
|
+
z: (val) => instance.position && (instance.position.z = val),
|
|
18
|
+
rotateX: (val) => instance.rotation && (instance.rotation.x = val),
|
|
19
|
+
rotateY: (val) => instance.rotation && (instance.rotation.y = val),
|
|
20
|
+
rotateZ: (val) => instance.rotation && (instance.rotation.z = val),
|
|
21
|
+
scale: (val) => instance.scale &&
|
|
22
|
+
instance.scale.set(val, val, val),
|
|
23
|
+
scaleX: (val) => instance.scale && (instance.scale.x = val),
|
|
24
|
+
scaleY: (val) => instance.scale && (instance.scale.y = val),
|
|
25
|
+
scaleZ: (val) => instance.scale && (instance.scale.z = val),
|
|
26
|
+
color: (val) => {
|
|
27
|
+
const color = instance.color;
|
|
28
|
+
if (color && color.set) {
|
|
29
|
+
color.set(val);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
opacity: (val) => instance.opacity !== undefined &&
|
|
33
|
+
(instance.opacity = val),
|
|
34
|
+
emissive: (val) => {
|
|
35
|
+
const emissive = instance.emissive;
|
|
36
|
+
if (emissive && emissive.set) {
|
|
37
|
+
emissive.set(val);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
emissiveIntensity: (val) => instance.emissiveIntensity !== undefined &&
|
|
41
|
+
(instance.emissiveIntensity = val),
|
|
42
|
+
roughness: (val) => instance.roughness !== undefined &&
|
|
43
|
+
(instance.roughness = val),
|
|
44
|
+
metalness: (val) => instance.metalness !== undefined &&
|
|
45
|
+
(instance.metalness = val),
|
|
46
|
+
};
|
|
47
|
+
for (const key in initialValues) {
|
|
48
|
+
const setter = propertyMap[key];
|
|
49
|
+
if (setter) {
|
|
50
|
+
setter(initialValues[key]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Store instance in the ref so animations can access it
|
|
55
|
+
instanceRef.current = instance;
|
|
56
|
+
// Call the forwarded ref if it exists
|
|
57
|
+
if (typeof forwardedRef === "function") {
|
|
58
|
+
forwardedRef(instance);
|
|
59
|
+
}
|
|
60
|
+
else if (forwardedRef) {
|
|
61
|
+
// eslint-disable-next-line react-hooks/immutability, @typescript-eslint/no-explicit-any
|
|
62
|
+
forwardedRef.current = instance;
|
|
63
|
+
}
|
|
64
|
+
}, [instanceRef, forwardedRef, initialValues]);
|
|
65
|
+
return createElement(Component, Object.assign({ ref: callbackRef }, props));
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export { useRender };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Vector3, Euler, Color, ReactThreeFiber } from '@react-three/fiber';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { ForwardRefExoticComponent, PropsWithoutRef, RefAttributes } from 'react';
|
|
4
|
+
import { MotionValue, MotionProps, ResolvedValues } from 'motion/react';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
type ThreeElement = InstanceType<typeof THREE.Object3D> & Record<string, unknown>;
|
|
8
|
+
interface ThreeMotionProps extends Omit<MotionProps, "style" | "children"> {
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
onInstanceUpdate?: ReactThreeFiber.ThreeElements["object3D"]["onUpdate"];
|
|
11
|
+
}
|
|
12
|
+
interface ThreeRenderState {
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
latestValues?: ResolvedValues;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
type ForwardRefComponent<T, P> = ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>;
|
|
20
|
+
type MotionValueOrNumber = number | MotionValue<number>;
|
|
21
|
+
type MotionValueVector3 = [
|
|
22
|
+
MotionValueOrNumber,
|
|
23
|
+
MotionValueOrNumber,
|
|
24
|
+
MotionValueOrNumber
|
|
25
|
+
];
|
|
26
|
+
type AcceptMotionValues<T> = Omit<T, "position" | "scale" | "rotation" | "color"> & {
|
|
27
|
+
position?: Vector3 | MotionValueVector3 | MotionValueOrNumber;
|
|
28
|
+
scale?: Vector3 | MotionValueVector3 | MotionValueOrNumber;
|
|
29
|
+
rotation?: Euler | MotionValueVector3 | MotionValueOrNumber;
|
|
30
|
+
color?: Color | MotionValue<string>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Motion-optimised versions of React's HTML components.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
type ThreeMotionComponents = {
|
|
38
|
+
[K in keyof ReactThreeFiber.ThreeElements]: ForwardRefComponent<ReactThreeFiber.ThreeElements[K] extends ReactThreeFiber.ThreeElement<infer T extends new (...args: unknown[]) => THREE.Object3D> ? InstanceType<T> : THREE.Object3D, ThreeMotionProps & Omit<AcceptMotionValues<ReactThreeFiber.ThreeElements[K]>, "onUpdate" | "transition">>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
declare const motion: ThreeMotionComponents;
|
|
42
|
+
|
|
43
|
+
interface MotionCameraProps extends ThreeMotionProps {
|
|
44
|
+
fov?: number;
|
|
45
|
+
near?: number;
|
|
46
|
+
far?: number;
|
|
47
|
+
type?: "perspective" | "orthographic";
|
|
48
|
+
}
|
|
49
|
+
declare const MotionCamera: (props: MotionCameraProps) => react_jsx_runtime.JSX.Element;
|
|
50
|
+
|
|
51
|
+
export { MotionCamera, motion };
|
|
52
|
+
export type { AcceptMotionValues, ForwardRefComponent, ThreeElement, ThreeMotionComponents, ThreeMotionProps, ThreeRenderState };
|
package/package.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "r3f-motion",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple and powerful React animation library for @react-three/fiber leveraging motion.dev",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"module": "dist/es/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/cjs/index.js",
|
|
11
|
+
"import": "./dist/es/index.mjs",
|
|
12
|
+
"default": "./dist/cjs/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react animation",
|
|
20
|
+
"react-three-fiber",
|
|
21
|
+
"framer-motion-3d",
|
|
22
|
+
"react",
|
|
23
|
+
"three",
|
|
24
|
+
"3d",
|
|
25
|
+
"animation",
|
|
26
|
+
"gestures",
|
|
27
|
+
"spring",
|
|
28
|
+
"framer"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/couch4/r3f-motion.git"
|
|
34
|
+
},
|
|
35
|
+
"author": "Jon Couch",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"scripts": {
|
|
38
|
+
"lint": "bun eslint src/**/*.ts",
|
|
39
|
+
"test": "vitest",
|
|
40
|
+
"test:ui": "vitest --ui",
|
|
41
|
+
"test:coverage": "vitest --coverage",
|
|
42
|
+
"build": "bun clean && tsc -p . && rollup -c",
|
|
43
|
+
"dev": "bun watch",
|
|
44
|
+
"storybook": "storybook dev -p 6006",
|
|
45
|
+
"build-storybook": "storybook build",
|
|
46
|
+
"clean": "rm -rf types dist lib",
|
|
47
|
+
"prettier": "prettier ./src/* --write",
|
|
48
|
+
"watch": "concurrently -c blue,red -n tsc,rollup --kill-others \"tsc --watch -p . --preserveWatchOutput\" \"rollup --config --watch --no-watch.clearScreen\"",
|
|
49
|
+
"prepack": "bun run build",
|
|
50
|
+
"postpublish": "git push --tags"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"motion": "^12.29.0",
|
|
54
|
+
"react-merge-refs": "^3.0.2"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@react-three/fiber": "^9.5.0",
|
|
58
|
+
"react": "^19.2.3",
|
|
59
|
+
"react-dom": "^19.2.3",
|
|
60
|
+
"three": "^0.182.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@eslint/js": "^9.39.1",
|
|
64
|
+
"@react-three/drei": "^10.7.7",
|
|
65
|
+
"@react-three/fiber": "^9.5.0",
|
|
66
|
+
"@react-three/test-renderer": "^9.1.0",
|
|
67
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
68
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
69
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
70
|
+
"@storybook/addon-essentials": "^8.6.14",
|
|
71
|
+
"@storybook/addon-interactions": "^8.6.14",
|
|
72
|
+
"@storybook/react": "^10.2.0",
|
|
73
|
+
"@storybook/react-vite": "^10.2.0",
|
|
74
|
+
"@storybook/test": "^8.6.15",
|
|
75
|
+
"@testing-library/react": "^16.3.2",
|
|
76
|
+
"@types/node": "^24.10.1",
|
|
77
|
+
"@types/react": "^19.2.9",
|
|
78
|
+
"@types/react-dom": "^19.2.3",
|
|
79
|
+
"@types/three": "^0.182.0",
|
|
80
|
+
"@vitejs/plugin-react": "^5.1.1",
|
|
81
|
+
"@vitest/ui": "^4.0.18",
|
|
82
|
+
"concurrently": "^9.2.1",
|
|
83
|
+
"eslint": "^9.39.2",
|
|
84
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
85
|
+
"eslint-plugin-react-refresh": "^0.4.24",
|
|
86
|
+
"eslint-plugin-storybook": "^10.2.0",
|
|
87
|
+
"globals": "^16.5.0",
|
|
88
|
+
"jsdom": "^27.4.0",
|
|
89
|
+
"prettier": "^3.8.1",
|
|
90
|
+
"rollup": "^4.56.0",
|
|
91
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
92
|
+
"scheduler": "^0.27.0",
|
|
93
|
+
"storybook": "^10.2.0",
|
|
94
|
+
"typescript": "^5.9.3",
|
|
95
|
+
"typescript-eslint": "^8.46.4",
|
|
96
|
+
"vite": "^7.2.4",
|
|
97
|
+
"vitest": "^4.0.18"
|
|
98
|
+
},
|
|
99
|
+
"gitHead": "b40bbecf29630ba30d49f763642eb3314861f08c"
|
|
100
|
+
}
|