Skip to content

Commit 7d92633

Browse files
committed
Remove commit method from return value of useAnimationFunction
1 parent 88efcc8 commit 7d92633

File tree

2 files changed

+82
-70
lines changed

2 files changed

+82
-70
lines changed

src/hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export { useAnimation } from "./useAnimation";
88
export type { AnimationHandle, WithRef } from "./useAnimation";
99
export { useAnimationFunction } from "./useAnimationFunction";
1010
export type {
11+
AnimationFunctionHandle,
1112
AnimationFunction,
1213
ComputedTimingContext,
1314
} from "./useAnimationFunction";

src/hooks/useAnimationFunction.ts

+81-70
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import { useEffect, useLayoutEffect, useRef, useState } from "react";
22
import { isSameObject } from "../utils";
33
import { AnimationOptions, createAnimation, createHandle } from "./core";
4-
import type { AnimationHandle } from "./useAnimation";
4+
5+
export type AnimationFunctionHandle = {
6+
play: () => AnimationFunctionHandle;
7+
replay: () => AnimationFunctionHandle;
8+
reverse: () => AnimationFunctionHandle;
9+
cancel: () => AnimationFunctionHandle;
10+
finish: () => AnimationFunctionHandle;
11+
pause: () => AnimationFunctionHandle;
12+
setTime: (time: number) => AnimationFunctionHandle;
13+
setPlaybackRate: (
14+
rate: number | ((prevRate: number) => number)
15+
) => AnimationFunctionHandle;
16+
end: () => Promise<void>;
17+
};
518

619
export type ComputedTimingContext = Required<
720
{
@@ -31,82 +44,80 @@ const bindUpdateFunction = (
3144
export const useAnimationFunction = (
3245
onUpdate: AnimationFunction,
3346
options?: AnimationOptions
34-
): AnimationHandle => {
47+
): AnimationFunctionHandle => {
3548
const onUpdateRef = useRef(onUpdate);
3649
const optionsRef = useRef(options);
3750

38-
const [animation, cleanup] = useState<[AnimationHandle, () => void]>(() => {
39-
const getOnUpdate = () => onUpdateRef.current;
40-
const getOptions = () => optionsRef.current;
51+
const [animation, cleanup] = useState<[AnimationFunctionHandle, () => void]>(
52+
() => {
53+
const getOnUpdate = () => onUpdateRef.current;
54+
const getOptions = () => optionsRef.current;
4155

42-
let cache: [Animation, AnimationOptions | undefined] | undefined;
43-
const initAnimation = (
44-
options: AnimationOptions | undefined
45-
): Animation => {
46-
if (cache) {
47-
const [prevAnimation, prevOptions] = cache;
48-
if (isSameObject(options, prevOptions)) {
49-
if (prevAnimation.playState !== "running") {
50-
bindUpdateFunction(prevAnimation, getOnUpdate);
56+
let cache: [Animation, AnimationOptions | undefined] | undefined;
57+
const initAnimation = (
58+
options: AnimationOptions | undefined
59+
): Animation => {
60+
if (cache) {
61+
const [prevAnimation, prevOptions] = cache;
62+
if (isSameObject(options, prevOptions)) {
63+
if (prevAnimation.playState !== "running") {
64+
bindUpdateFunction(prevAnimation, getOnUpdate);
65+
}
66+
return prevAnimation;
5167
}
52-
return prevAnimation;
68+
prevAnimation.cancel();
5369
}
54-
prevAnimation.cancel();
55-
}
56-
const animation = createAnimation(null, null, options);
57-
bindUpdateFunction(animation, getOnUpdate);
58-
cache = [animation, options];
59-
return animation;
60-
};
61-
const getAnimation = () => cache?.[0];
62-
const handle = createHandle();
70+
const animation = createAnimation(null, null, options);
71+
bindUpdateFunction(animation, getOnUpdate);
72+
cache = [animation, options];
73+
return animation;
74+
};
75+
const getAnimation = () => cache?.[0];
76+
const handle = createHandle();
6377

64-
const externalHandle: AnimationHandle = {
65-
play: () => {
66-
handle._play(initAnimation(getOptions()));
67-
return externalHandle;
68-
},
69-
replay: () => {
70-
handle._replay(initAnimation(getOptions()));
71-
return externalHandle;
72-
},
73-
reverse: () => {
74-
handle._reverse(initAnimation(getOptions()));
75-
return externalHandle;
76-
},
77-
cancel: () => {
78-
handle._cancel(getAnimation());
79-
return externalHandle;
80-
},
81-
finish: () => {
82-
handle._finish(getAnimation());
83-
return externalHandle;
84-
},
85-
pause: () => {
86-
handle._pause(getAnimation());
87-
return externalHandle;
88-
},
89-
commit: () => {
90-
handle._commit(getAnimation());
91-
return externalHandle;
92-
},
93-
setTime: (time) => {
94-
handle._setTime(getAnimation(), time);
95-
return externalHandle;
96-
},
97-
setPlaybackRate: (rate) => {
98-
handle._setRate(getAnimation(), rate);
99-
return externalHandle;
100-
},
101-
end: () => handle._end(getAnimation()),
102-
};
103-
return [
104-
externalHandle,
105-
() => {
106-
handle._cancel(getAnimation());
107-
},
108-
];
109-
})[0];
78+
const externalHandle: AnimationFunctionHandle = {
79+
play: () => {
80+
handle._play(initAnimation(getOptions()));
81+
return externalHandle;
82+
},
83+
replay: () => {
84+
handle._replay(initAnimation(getOptions()));
85+
return externalHandle;
86+
},
87+
reverse: () => {
88+
handle._reverse(initAnimation(getOptions()));
89+
return externalHandle;
90+
},
91+
cancel: () => {
92+
handle._cancel(getAnimation());
93+
return externalHandle;
94+
},
95+
finish: () => {
96+
handle._finish(getAnimation());
97+
return externalHandle;
98+
},
99+
pause: () => {
100+
handle._pause(getAnimation());
101+
return externalHandle;
102+
},
103+
setTime: (time) => {
104+
handle._setTime(getAnimation(), time);
105+
return externalHandle;
106+
},
107+
setPlaybackRate: (rate) => {
108+
handle._setRate(getAnimation(), rate);
109+
return externalHandle;
110+
},
111+
end: () => handle._end(getAnimation()),
112+
};
113+
return [
114+
externalHandle,
115+
() => {
116+
handle._cancel(getAnimation());
117+
},
118+
];
119+
}
120+
)[0];
110121

111122
useLayoutEffect(() => {
112123
onUpdateRef.current = onUpdate;

0 commit comments

Comments
 (0)