/
useWebAnimations.ts
157 lines (136 loc) 路 4.33 KB
/
useWebAnimations.ts
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
import { RefObject, useState, useRef, useCallback, useEffect } from "react";
import useDeepCompareEffect from "use-deep-compare-effect";
import useLatest from "./useLatest";
export const polyfillErr =
"馃挕 use-web-animations: please install polyfill to use this hook. See https://github.com/wellyshen/use-web-animations##use-polyfill";
export const eventErr = (type: string): string =>
`馃挕 use-web-animations: the browser doesn't support ${type} event, please use "onUpdate" to monitor the animation's state instead. See https://github.com/wellyshen/use-web-animations#basic-usage`;
type Keyframes = Keyframe[] | PropertyIndexedKeyframes;
type PlayState = string | null;
interface AnimConf {
id?: string;
playbackRate?: number;
autoPlay?: boolean;
animationOptions?:
| number
| (KeyframeAnimationOptions & { pseudoElement?: string });
}
interface Animate {
(args: AnimConf & { keyframes: Keyframes }): void;
}
interface Callback {
(event: {
playState: PlayState;
animate: Animate;
animation: Animation;
}): void;
}
export interface Options<T> extends AnimConf {
ref?: RefObject<T>;
keyframes?: Keyframes;
onReady?: Callback;
onUpdate?: Callback;
onFinish?: Callback;
}
interface Return<T> {
ref: RefObject<T>;
playState: PlayState;
getAnimation: () => Animation | undefined;
animate: Animate;
}
const useWebAnimations = <T extends HTMLElement>({
ref: refOpt,
id,
playbackRate,
autoPlay,
keyframes,
animationOptions,
onReady,
onUpdate,
onFinish,
}: Options<T> = {}): Return<T> => {
const [playState, setPlayState] = useState<PlayState>(null);
const animRef = useRef<Animation>();
const prevPendingRef = useRef<boolean>();
const prevPlayStateRef = useRef<string>();
const onReadyRef = useLatest(onReady);
const onUpdateRef = useLatest(onUpdate);
const onFinishRef = useLatest(onFinish);
const refVar = useRef<T>(null);
const ref = refOpt || refVar;
const getAnimation = useCallback(() => animRef.current, []);
const animate: Animate = useCallback(
(args) => {
if (!ref.current || !args.keyframes) return;
if (!ref.current.animate) {
console.error(polyfillErr);
return;
}
animRef.current = ref.current.animate(
args.keyframes,
args.animationOptions
);
const { current: anim } = animRef;
if (args.autoPlay === false) anim.pause();
if (args.id) anim.id = args.id;
if (args.playbackRate) anim.playbackRate = args.playbackRate;
if (onReadyRef.current) {
if (anim.ready) {
anim.ready.then((animation) => {
// @ts-expect-error
onReadyRef.current({
playState: animation.playState,
animate,
animation,
});
});
} else {
console.error(eventErr("onReady"));
}
}
if (onFinishRef.current) {
if (anim.finished) {
anim.finished.then((animation) => {
// @ts-expect-error
onFinishRef.current({
playState: animation.playState,
animate,
animation,
});
});
} else {
console.error(eventErr("onFinish"));
}
}
prevPlayStateRef.current = undefined;
},
[onFinishRef, onReadyRef, ref]
);
useDeepCompareEffect(() => {
// @ts-expect-error
animate({ id, playbackRate, autoPlay, keyframes, animationOptions });
}, [id, playbackRate, autoPlay, keyframes, animationOptions, animate]);
useEffect(() => {
const update = () => {
const animation = getAnimation();
if (animation) {
const { pending, playState: curPlayState } = animation;
if (prevPlayStateRef.current !== curPlayState)
setPlayState(curPlayState);
if (
onUpdateRef.current &&
(prevPendingRef.current !== pending ||
prevPlayStateRef.current !== curPlayState ||
curPlayState === "running")
)
onUpdateRef.current({ playState: curPlayState, animate, animation });
prevPendingRef.current = pending;
prevPlayStateRef.current = curPlayState;
}
requestAnimationFrame(update);
};
requestAnimationFrame(update);
}, [animate, getAnimation, onUpdateRef]);
return { ref, playState, getAnimation, animate };
};
export default useWebAnimations;