Skip to content

Commit 0df240b

Browse files
authored
Merge pull request #475 from wellyshen/fix/on-finish
fix: `onFinish` event not working properly
2 parents 343d8ce + 9f7f0c2 commit 0df240b

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Using [Web Animations API](https://developer.mozilla.org/en-US/docs/Web/API/Web_
3434
- 🔩 Supports custom `refs` for [some reasons](#use-your-own-ref).
3535
- 📜 Supports [TypeScript](https://www.typescriptlang.org) type definition.
3636
- 🗄️ Server-side rendering compatibility.
37-
- 🦔 Tiny size ([~ 4.4KB gzipped](https://bundlephobia.com/result?p=@wellyshen/use-web-animations)). No external dependencies, aside for the `react`.
37+
- 🦔 Tiny size ([~ 4.4kB gzipped](https://bundlephobia.com/result?p=@wellyshen/use-web-animations)). No external dependencies, aside for the `react`.
3838

3939
## Requirement
4040

src/__tests__/useWebAnimations.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe("useWebAnimations", () => {
3333
pending: true,
3434
playState: "pause",
3535
ready: Promise.resolve(e),
36-
finished: Promise.resolve(e),
3736
pause: jest.fn(),
3837
finish: jest.fn(),
3938
cancel: jest.fn(),
@@ -69,8 +68,9 @@ describe("useWebAnimations", () => {
6968
it("should call onFinish correctly", async () => {
7069
console.error = jest.fn();
7170
const onFinish = jest.fn();
72-
const { waitForNextUpdate } = renderHelper({ onFinish });
73-
await waitForNextUpdate();
71+
renderHelper({ onFinish });
72+
// @ts-expect-error
73+
el.animate.mock.results[0].value.onfinish({ target: e });
7474
expect(onFinish).toHaveBeenCalledWith({
7575
playState: e.playState,
7676
animation: e,
@@ -228,11 +228,6 @@ describe("useWebAnimations", () => {
228228
// @ts-expect-error
229229
animation.ready = null;
230230
renderHelper({ onReady: () => null });
231-
expect(console.error).toHaveBeenCalledWith(eventErr("onReady"));
232-
233-
// @ts-expect-error
234-
animation.finished = null;
235-
renderHelper({ onFinish: () => null });
236-
expect(console.error).toHaveBeenLastCalledWith(eventErr("onFinish"));
231+
expect(console.error).toHaveBeenCalledWith(eventErr);
237232
});
238233
});

src/useWebAnimations.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import useLatest from "./useLatest";
44

55
export const polyfillErr =
66
"💡 use-web-animations: please install polyfill to use this hook. See https://github.com/wellyshen/use-web-animations##use-polyfill";
7-
export const eventErr = (type: string): string =>
8-
`💡 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`;
7+
export const eventErr = `💡 use-web-animations: the browser doesn't support "onReady" event, please use "onUpdate" to monitor the animation's state instead. See https://github.com/wellyshen/use-web-animations#basic-usage`;
98

109
type Keyframes = Keyframe[] | PropertyIndexedKeyframes;
1110
type PlayState = AnimationPlayState | undefined;
@@ -96,25 +95,23 @@ const useWebAnimations = <T extends HTMLElement | null>({
9695
});
9796
});
9897
} else {
99-
console.error(eventErr("onReady"));
98+
console.error(eventErr);
10099
}
101100
}
102101

103102
if (onFinishRef.current) {
104-
if (anim.finished) {
105-
anim.finished.then((animation) => {
106-
if (!hasUnmountedRef.current) {
107-
// @ts-expect-error
108-
onFinishRef.current({
109-
playState: animation.playState,
110-
animate,
111-
animation,
112-
});
113-
}
114-
});
115-
} else {
116-
console.error(eventErr("onFinish"));
117-
}
103+
anim.onfinish = (e) => {
104+
const animation = e.target as Animation;
105+
106+
if (!hasUnmountedRef.current) {
107+
// @ts-expect-error
108+
onFinishRef.current({
109+
playState: animation.playState,
110+
animate,
111+
animation,
112+
});
113+
}
114+
};
118115
}
119116

120117
prevPlayStateRef.current = undefined;

0 commit comments

Comments
 (0)