|
1 | 1 | import { useMemo, useRef, useImperativeHandle, useEffect } from 'react'
|
2 |
| -import Ctrl from './animated/Controller' |
3 |
| -import { callProp, fillArray, is, toArray } from './shared/helpers' |
| 2 | +import { callProp, fillArray, is, toArray, usePrev } from './shared/helpers' |
| 3 | +import Controller from './animated/Controller' |
4 | 4 |
|
5 | 5 | /** API
|
6 | 6 | * const props = useSprings(number, [{ ... }, { ... }, ...])
|
7 | 7 | * const [props, set] = useSprings(number, (i, controller) => ({ ... }))
|
8 | 8 | */
|
9 | 9 |
|
10 | 10 | export const useSprings = (length, propsArg) => {
|
11 |
| - const mounted = useRef(false) |
12 |
| - const ctrl = useRef() |
| 11 | + const hasNewSprings = length !== usePrev(length) |
13 | 12 | const isFn = is.fun(propsArg)
|
14 | 13 |
|
15 | 14 | // The `propsArg` coerced into an array
|
16 | 15 | const props = isFn ? [] : propsArg
|
17 | 16 |
|
18 |
| - // The controller maintains the animation values, starts and stops animations |
19 |
| - const [controllers, setProps, ref, api] = useMemo(() => { |
20 |
| - let ref, controllers |
21 |
| - return [ |
22 |
| - // Recreate the controllers whenever `length` changes |
23 |
| - (controllers = fillArray(length, i => { |
24 |
| - const c = new Ctrl() |
25 |
| - const p = props[i] || (props[i] = callProp(propsArg, i, c)) |
26 |
| - if (i === 0) ref = p.ref |
27 |
| - return c.update(p) |
28 |
| - })), |
29 |
| - // This updates the controllers with new props |
30 |
| - props => { |
| 17 | + // Recreate the controllers whenever `length` changes |
| 18 | + const springsRef = useRef() |
| 19 | + const springs = useMemo( |
| 20 | + () => |
| 21 | + fillArray(length, i => { |
| 22 | + const s = new Controller() |
| 23 | + const p = props[i] || (props[i] = callProp(propsArg, i, s)) |
| 24 | + return s.update(p) |
| 25 | + }), |
| 26 | + [length] |
| 27 | + ) |
| 28 | + |
| 29 | + const ref = springs[0].props.ref |
| 30 | + const { start, update, stop } = useMemo( |
| 31 | + () => ({ |
| 32 | + /** Apply any pending updates */ |
| 33 | + start: () => |
| 34 | + Promise.all( |
| 35 | + springsRef.current.map(s => new Promise(done => s.start(done))) |
| 36 | + ), |
| 37 | + /** Update the spring controllers */ |
| 38 | + update: props => { |
31 | 39 | const isFn = is.fun(props)
|
32 | 40 | if (!isFn) props = toArray(props)
|
33 |
| - controllers.forEach((c, i) => { |
34 |
| - c.update(isFn ? callProp(props, i, c) : props[i]) |
35 |
| - if (!ref) c.start() |
| 41 | + springsRef.current.forEach((spring, i) => { |
| 42 | + spring.update(isFn ? callProp(props, i, spring) : props[i]) |
| 43 | + if (!ref) spring.start() |
36 | 44 | })
|
37 | 45 | },
|
38 |
| - // The imperative API is accessed via ref |
39 |
| - ref, |
40 |
| - ref && { |
41 |
| - start: () => |
42 |
| - Promise.all(controllers.map(c => new Promise(r => c.start(r)))), |
43 |
| - stop: finished => controllers.forEach(c => c.stop(finished)), |
44 |
| - controllers, |
45 |
| - }, |
46 |
| - ] |
47 |
| - }, [length]) |
| 46 | + /** Stop one key or all keys from animating */ |
| 47 | + stop: (...args) => springsRef.current.forEach(s => s.stop(...args)), |
| 48 | + }), |
| 49 | + [] |
| 50 | + ) |
48 | 51 |
|
49 |
| - // Attach the imperative API to its ref |
50 |
| - useImperativeHandle(ref, () => api, [api]) |
| 52 | + useImperativeHandle(ref, () => ({ start, stop })) |
51 | 53 |
|
52 | 54 | // Once mounted, update the local state and start any animations.
|
53 | 55 | useEffect(() => {
|
54 |
| - if (!isFn || ctrl.current !== controllers) { |
55 |
| - controllers.forEach((c, i) => { |
56 |
| - const p = props[i] |
57 |
| - // Set the default props for async updates |
58 |
| - c.setProp('config', p.config) |
59 |
| - c.setProp('immediate', p.immediate) |
| 56 | + if (!isFn || hasNewSprings) { |
| 57 | + props.forEach((p, i) => { |
| 58 | + // Set default props for async updates |
| 59 | + springs[i].setProp('config', p.config) |
| 60 | + springs[i].setProp('immediate', p.immediate) |
60 | 61 | })
|
61 | 62 | }
|
62 |
| - |
63 |
| - if (ctrl.current !== controllers) { |
64 |
| - if (ctrl.current) ctrl.current.forEach(c => c.destroy()) |
65 |
| - ctrl.current = controllers |
66 |
| - } |
67 |
| - |
68 |
| - if (mounted.current) { |
69 |
| - if (!isFn) setProps(props) |
70 |
| - } else if (!ref) { |
71 |
| - controllers.forEach(c => c.start()) |
| 63 | + if (hasNewSprings) { |
| 64 | + if (springsRef.current) { |
| 65 | + springsRef.current.forEach(s => s.destroy()) |
| 66 | + } |
| 67 | + springsRef.current = springs |
| 68 | + if (!ref) { |
| 69 | + springs.forEach(s => s.start()) |
| 70 | + } |
| 71 | + } else if (!isFn) { |
| 72 | + update(props) |
72 | 73 | }
|
73 | 74 | })
|
74 | 75 |
|
75 |
| - // Update mounted flag and destroy controller on unmount |
| 76 | + // Destroy the controllers on unmount |
76 | 77 | useEffect(() => {
|
77 |
| - mounted.current = true |
78 |
| - return () => ctrl.current.forEach(c => c.destroy()) |
| 78 | + return () => springsRef.current.forEach(s => s.destroy()) |
79 | 79 | }, [])
|
80 | 80 |
|
81 |
| - // Return animated props, or, anim-props + the update-setter above |
82 |
| - const animatedProps = controllers.map(c => c.animated) |
83 |
| - return isFn |
84 |
| - ? [ |
85 |
| - animatedProps, |
86 |
| - setProps, |
87 |
| - (...args) => ctrl.current.forEach(c => c.stop(...args)), |
88 |
| - ] |
89 |
| - : animatedProps |
| 81 | + const values = springs.map(s => s.animated) |
| 82 | + return isFn ? [values, update, stop] : values |
90 | 83 | }
|
0 commit comments