Skip to content

Commit f2ec024

Browse files
committed
Rename commit method to persist and also cancel animation in it
1 parent 972883d commit f2ec024

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

src/hooks/core.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,22 @@ export const createHandle = () => {
8181
if (!animation) return;
8282
animation.pause();
8383
},
84-
_commit: (
84+
_persist: (
8585
animation: Animation | undefined,
8686
el: HTMLElement,
8787
keyframes: TypedKeyframe[]
8888
) => {
8989
if (!animation) return;
9090
if (animation.commitStyles) {
9191
animation.commitStyles();
92-
return;
92+
} else {
93+
// Fallback for commitStyles
94+
const computedStyle = getComputedStyle(el);
95+
getKeyframeKeys(keyframes).forEach((k) => {
96+
(el.style as any)[k] = (computedStyle as any)[k];
97+
});
9398
}
94-
// Fallback for commitStyles
95-
const computedStyle = getComputedStyle(el);
96-
getKeyframeKeys(keyframes).forEach((k) => {
97-
(el.style as any)[k] = (computedStyle as any)[k];
98-
});
99+
animation.cancel();
99100
},
100101
_setTime: (animation: Animation | undefined, time: number) => {
101102
if (!animation) return;

src/hooks/useAnimation.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type AnimationHandle = {
1414
cancel: () => AnimationHandle;
1515
finish: () => AnimationHandle;
1616
pause: () => AnimationHandle;
17-
commit: () => AnimationHandle;
17+
persist: () => AnimationHandle;
1818
setTime: (time: number) => AnimationHandle;
1919
setPlaybackRate: (
2020
rate: number | ((prevRate: number) => number)
@@ -90,8 +90,8 @@ export const useAnimation = (
9090
handle._pause(getAnimation());
9191
return externalHandle;
9292
},
93-
commit: () => {
94-
handle._commit(getAnimation(), getTarget()!, getKeyframes());
93+
persist: () => {
94+
handle._persist(getAnimation(), getTarget()!, getKeyframes());
9595
return externalHandle;
9696
},
9797
setTime: (time) => {

src/hooks/useAnimations.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type AnimationsHandle<ID extends string> = {
1515
cancel: (name: ID) => AnimationsHandle<ID>;
1616
finish: (name: ID) => AnimationsHandle<ID>;
1717
pause: (name: ID) => AnimationsHandle<ID>;
18-
commit: (name: ID) => AnimationsHandle<ID>;
18+
persist: (name: ID) => AnimationsHandle<ID>;
1919
setTime: (name: ID, time: number) => AnimationsHandle<ID>;
2020
setPlaybackRate: (
2121
name: ID,
@@ -97,8 +97,8 @@ export const useAnimations = <ID extends string>(
9797
handle._pause(getAnimation(name));
9898
return externalHandle;
9999
},
100-
commit: (name) => {
101-
handle._commit(
100+
persist: (name) => {
101+
handle._persist(
102102
getAnimation(name),
103103
getTarget()!,
104104
toArray(getKeyframesAndOptions(name)[0])

stories/hooks/useAnimations.stories.tsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -231,27 +231,24 @@ export const Sequence: StoryObj = {
231231
});
232232

233233
const onClickRed = useCallback(async () => {
234-
animate.cancelAll();
235234
try {
236235
await animate.play("red").end("red");
237236
} finally {
238-
animate.commit("red");
237+
animate.persist("red");
239238
}
240239
}, []);
241240
const onClickBlue = useCallback(async () => {
242-
animate.cancelAll();
243241
try {
244242
await animate.play("blue").end("blue");
245243
} finally {
246-
animate.commit("blue");
244+
animate.persist("blue");
247245
}
248246
}, []);
249247
const onClickGreen = useCallback(async () => {
250-
animate.cancelAll();
251248
try {
252249
await animate.play("green").end("green");
253250
} finally {
254-
animate.commit("green");
251+
animate.persist("green");
255252
}
256253
}, []);
257254
const onClickAll = useCallback(async () => {

0 commit comments

Comments
 (0)