|
1 | 1 | import { useEffect, useRef } from "react"; |
2 | 2 | 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"; |
4 | 10 | import { |
5 | 11 | AnimationOptions, |
6 | 12 | createAnimation, |
7 | 13 | GetKeyframeFunction, |
8 | | - normalizeKeyframe, |
9 | 14 | PlayOptions, |
10 | 15 | TypedKeyframe, |
11 | 16 | _cancel, |
@@ -84,6 +89,17 @@ export interface AnimationHandle<Args = void> |
84 | 89 | (ref: Element | null): void; |
85 | 90 | } |
86 | 91 |
|
| 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 | + |
87 | 103 | /** |
88 | 104 | * A basic hook to use Web Animations API. See {@link AnimationHandle}. |
89 | 105 | * @typeParam Args - argument type |
@@ -111,13 +127,8 @@ export const useAnimation = <Args = void>( |
111 | 127 |
|
112 | 128 | const initAnimation = ( |
113 | 129 | el: Element, |
114 | | - opts: { args?: Args } = {} |
| 130 | + keyframes: TypedKeyframe[] |
115 | 131 | ): Animation => { |
116 | | - const keyframes = normalizeKeyframe( |
117 | | - el, |
118 | | - keyframeRef.current, |
119 | | - opts.args! |
120 | | - ); |
121 | 132 | const options = optionsRef.current; |
122 | 133 | if (cache) { |
123 | 134 | const [prevAnimation, prevKeyframes, prevOptions] = cache; |
@@ -145,7 +156,12 @@ export const useAnimation = <Args = void>( |
145 | 156 | <BaseAnimationHandle<Args>>{ |
146 | 157 | play: (...opts) => { |
147 | 158 | 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]); |
149 | 165 | return externalHandle; |
150 | 166 | }, |
151 | 167 | reverse: () => { |
|
0 commit comments