|
1 | 1 | import { useRef, useLayoutEffect, useCallback, MutableRefObject } from "react"; |
2 | 2 |
|
3 | | -const DEFAULT_DURATION = 750; |
4 | | - |
5 | 3 | export type AnimationOptions = { |
6 | 4 | duration?: number; |
7 | 5 | infinite?: boolean; |
@@ -30,53 +28,73 @@ export const useWebAnimation = ({ |
30 | 28 | const animation = useRef<Animation | undefined>(); |
31 | 29 | const reverse = useRef(false); |
32 | 30 |
|
33 | | - const animate = useCallback(() => { |
34 | | - const timingObject: KeyframeAnimationOptions = { |
35 | | - duration: duration || DEFAULT_DURATION, |
36 | | - iterations: 1, |
37 | | - delay: delay || 0, |
38 | | - easing, |
39 | | - direction: "normal" |
40 | | - }; |
41 | | - reverse.current = false; |
| 31 | + const animate = useCallback( |
| 32 | + (onComplete?: (() => void)) => { |
| 33 | + if (!ref.current || !ref.current.animate) { |
| 34 | + if (process.env.NODE_ENV !== 'production') { |
| 35 | + throw new Error('Please apply the ref to a dom-element.'); |
| 36 | + } |
| 37 | + return; |
| 38 | + } |
42 | 39 |
|
43 | | - callback.current = () => { |
44 | | - if (infinite) { |
45 | | - timingObject.direction = reverse.current ? "reverse" : "normal"; |
46 | | - reverse.current = !reverse.current; |
| 40 | + if (!ref.current.animate) { |
| 41 | + ref.current!.style[property as any] = getValue(to); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const timingObject: KeyframeAnimationOptions = { |
| 46 | + duration: duration || 750, |
| 47 | + iterations: 1, |
| 48 | + delay, |
| 49 | + easing, |
| 50 | + }; |
| 51 | + |
| 52 | + callback.current = () => { |
| 53 | + if (infinite) { |
| 54 | + timingObject.direction = reverse.current ? "reverse" : "normal"; |
| 55 | + reverse.current = !reverse.current; |
47 | 56 |
|
48 | | - animation.current = ref.current!.animate( |
49 | | - [{ [property]: getValue(from) }, { [property]: getValue(to) }], |
50 | | - timingObject |
51 | | - ); |
52 | | - animation.current.addEventListener("finish", callback.current); |
53 | | - } else { |
54 | | - if (animation.current) { |
55 | | - if (animation.current && callback.current) { |
56 | | - animation.current!.removeEventListener("finish", callback.current); |
57 | | - } |
58 | | - animation.current = undefined; |
59 | | - callback.current = undefined; |
60 | | - } else { |
61 | 57 | animation.current = ref.current!.animate( |
62 | 58 | [{ [property]: getValue(from) }, { [property]: getValue(to) }], |
63 | 59 | timingObject |
64 | 60 | ); |
65 | 61 | animation.current.addEventListener("finish", callback.current); |
| 62 | + } else { |
| 63 | + if (animation.current) { |
| 64 | + ref.current!.style[property as any] = getValue(to); |
| 65 | + if (onComplete) { |
| 66 | + onComplete(); |
| 67 | + } |
| 68 | + if (animation.current && callback.current) { |
| 69 | + animation.current!.removeEventListener( |
| 70 | + "finish", |
| 71 | + callback.current |
| 72 | + ); |
| 73 | + } |
| 74 | + animation.current = undefined; |
| 75 | + callback.current = undefined; |
| 76 | + } else { |
| 77 | + animation.current = ref.current!.animate( |
| 78 | + [{ [property]: getValue(from) }, { [property]: getValue(to) }], |
| 79 | + timingObject |
| 80 | + ); |
| 81 | + animation.current.addEventListener("finish", callback.current); |
| 82 | + } |
66 | 83 | } |
67 | | - } |
68 | | - }; |
| 84 | + }; |
69 | 85 |
|
70 | | - callback.current(); |
71 | | - }, [delay, duration, easing, from, infinite, property, to]); |
| 86 | + callback.current(); |
| 87 | + }, |
| 88 | + [delay, duration, easing, from, infinite, property, to] |
| 89 | + ); |
72 | 90 |
|
73 | 91 | useLayoutEffect(() => { |
74 | 92 | if (!pause) { |
75 | 93 | animate(); |
76 | 94 | } |
77 | 95 |
|
78 | 96 | return () => { |
79 | | - if (animation.current) { |
| 97 | + if (animation.current && !pause) { |
80 | 98 | if (animation.current && callback.current) { |
81 | 99 | animation.current!.removeEventListener("finish", callback.current); |
82 | 100 | } |
|
0 commit comments