Skip to content

Commit 74dc11f

Browse files
committed
Refactor
1 parent 0d3b6c7 commit 74dc11f

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/core/waapi.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CSSProperties } from "react";
2-
import { getKeys, getStyle, toArray, uniqBy } from "./utils";
2+
import { getKeys, uniqBy } from "./utils";
33

44
export type AnimatableCSSProperties = Omit<
55
CSSProperties,
@@ -40,17 +40,6 @@ export interface AnimationOptions
4040
easing?: TypedEasing;
4141
}
4242

43-
export const normalizeKeyframe = <Args>(
44-
el: Element,
45-
keyframe: TypedKeyframe | TypedKeyframe[] | GetKeyframeFunction<Args>,
46-
args: Args
47-
): TypedKeyframe[] => {
48-
if (typeof keyframe === "function") {
49-
return keyframe(getStyle(el), args);
50-
}
51-
return toArray(keyframe);
52-
};
53-
5443
export const getKeyframeKeys = (keyframes: TypedKeyframe[]): string[] =>
5544
uniqBy(keyframes.flatMap(getKeys)).reduce((acc, k) => {
5645
if (["offset", "easing", "composite"].includes(k)) {

src/react/hooks/useAnimation.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { useEffect, useRef } from "react";
22
import type { Expand } from "../../core/types";
3-
import { assign, isSameObject, isSameObjectArray } from "../../core/utils";
3+
import {
4+
assign,
5+
getStyle,
6+
isSameObject,
7+
isSameObjectArray,
8+
toArray,
9+
} from "../../core/utils";
410
import {
511
AnimationOptions,
612
createAnimation,
713
GetKeyframeFunction,
8-
normalizeKeyframe,
914
PlayOptions,
1015
TypedKeyframe,
1116
_cancel,
@@ -84,6 +89,17 @@ export interface AnimationHandle<Args = void>
8489
(ref: Element | null): void;
8590
}
8691

92+
const normalizeKeyframe = <Args>(
93+
el: Element,
94+
keyframe: TypedKeyframe | TypedKeyframe[] | GetKeyframeFunction<Args>,
95+
args: Args
96+
): TypedKeyframe[] => {
97+
if (typeof keyframe === "function") {
98+
return keyframe(getStyle(el), args);
99+
}
100+
return toArray(keyframe);
101+
};
102+
87103
/**
88104
* A basic hook to use Web Animations API. See {@link AnimationHandle}.
89105
* @typeParam Args - argument type
@@ -111,13 +127,8 @@ export const useAnimation = <Args = void>(
111127

112128
const initAnimation = (
113129
el: Element,
114-
opts: { args?: Args } = {}
130+
keyframes: TypedKeyframe[]
115131
): Animation => {
116-
const keyframes = normalizeKeyframe(
117-
el,
118-
keyframeRef.current,
119-
opts.args!
120-
);
121132
const options = optionsRef.current;
122133
if (cache) {
123134
const [prevAnimation, prevKeyframes, prevOptions] = cache;
@@ -145,7 +156,12 @@ export const useAnimation = <Args = void>(
145156
<BaseAnimationHandle<Args>>{
146157
play: (...opts) => {
147158
if (!target) return externalHandle;
148-
_play(initAnimation(target, opts[0] as { args?: Args }), opts[0]);
159+
const keyframes = normalizeKeyframe(
160+
target,
161+
keyframeRef.current,
162+
((opts[0] || {}) as { args?: Args }).args!
163+
);
164+
_play(initAnimation(target, keyframes), opts[0]);
149165
return externalHandle;
150166
},
151167
reverse: () => {

0 commit comments

Comments
 (0)