MCPcopy
hub / github.com/beekai-oss/react-simple-animate / useAnimate

Function useAnimate

src/useAnimate.ts:16–81  ·  view source on GitHub ↗
(props: UseAnimateProps)

Source from the content-addressed store, hash-verified

14 | 'easeType'
15>;
16export default function useAnimate(props: UseAnimateProps): {
17 isPlaying: boolean;
18 style: React.CSSProperties;
19 play: (isPlaying: boolean) => void;
20} {
21 const {
22 start,
23 end,
24 complete,
25 onComplete,
26 delay = 0,
27 duration = DEFAULT_DURATION,
28 easeType = DEFAULT_EASE_TYPE,
29 } = props;
30 const transition = React.useMemo(
31 () => `${ALL} ${duration}s ${easeType} ${delay}s`,
32 [duration, easeType, delay],
33 );
34 const [animate, setAnimate] = React.useState<{
35 isPlaying: boolean;
36 style: React.CSSProperties;
37 }>({
38 isPlaying: false,
39 style: { ...start, transition },
40 });
41 const { isPlaying, style } = animate;
42 const onCompleteTimeRef = React.useRef<NodeJS.Timeout>();
43
44 React.useEffect(() => {
45 if ((onComplete || complete) && isPlaying) {
46 onCompleteTimeRef.current = setTimeout(() => {
47 if (onComplete) {
48 onComplete();
49 }
50
51 if (complete) {
52 setAnimate((animate) => ({
53 ...animate,
54 style: complete,
55 }));
56 }
57 }, secToMs(delay + duration));
58 }
59
60 return () =>
61 onCompleteTimeRef.current && clearTimeout(onCompleteTimeRef.current);
62 }, [animate, complete, delay, duration, isPlaying, onComplete]);
63
64 return {
65 isPlaying,
66 style,
67 play: React.useCallback(
68 (isPlaying) => {
69 setAnimate((animate) => ({
70 ...animate,
71 style: {
72 ...(isPlaying ? end : start),
73 transition,

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected