@@ -111,6 +111,7 @@ export const useAnimation = <Args = void>(
111111 let element : Element | null = null ;
112112 let active : AnimationObject | undefined ;
113113
114+ const currentAnimation = ( ) => active && getAnimation ( active ) ;
114115 const init = ( args : Args ) => {
115116 if ( ! element ) return ;
116117 const [ keyframe , _options = { } ] = argsRef . current ;
@@ -149,31 +150,35 @@ export const useAnimation = <Args = void>(
149150 return externalHandle ;
150151 } ,
151152 reverse : ( ) => {
152- if ( active ) {
153- _reverse ( getAnimation ( active ) ) ;
153+ const animation = currentAnimation ( ) ;
154+ if ( animation ) {
155+ _reverse ( animation ) ;
154156 }
155157 return externalHandle ;
156158 } ,
157159 cancel : ( ) => {
158- if ( active ) {
159- _cancel ( getAnimation ( active ) ) ;
160+ const animation = currentAnimation ( ) ;
161+ if ( animation ) {
162+ _cancel ( animation ) ;
160163 }
161164 return externalHandle ;
162165 } ,
163166 finish : ( ) => {
164- if ( active ) {
165- _finish ( getAnimation ( active ) ) ;
167+ const animation = currentAnimation ( ) ;
168+ if ( animation ) {
169+ _finish ( animation ) ;
166170 }
167171 return externalHandle ;
168172 } ,
169173 pause : ( ) => {
170- if ( active ) {
171- _pause ( getAnimation ( active ) ) ;
174+ const animation = currentAnimation ( ) ;
175+ if ( animation ) {
176+ _pause ( animation ) ;
172177 }
173178 return externalHandle ;
174179 } ,
175180 setTime : ( time ) => {
176- let animation = active && getAnimation ( active ) ;
181+ let animation = currentAnimation ( ) ;
177182 if ( ! animation ) {
178183 const [ keyframe ] = argsRef . current ;
179184 if ( typeof keyframe === "function" ) {
@@ -182,20 +187,22 @@ export const useAnimation = <Args = void>(
182187 // Init animation in setTime to start animation without calling play
183188 animation = init ( undefined ! ) ;
184189 }
185- _setTime ( animation , time ) ;
190+ if ( animation ) {
191+ _setTime ( animation , time ) ;
192+ }
186193
187194 return externalHandle ;
188195 } ,
189196 setPlaybackRate : ( rate ) => {
190- if ( active ) {
191- _setRate ( getAnimation ( active ) , rate ) ;
197+ const animation = currentAnimation ( ) ;
198+ if ( animation ) {
199+ _setRate ( animation , rate ) ;
192200 }
193201 return externalHandle ;
194202 } ,
195203 waitFor : ( event ) => {
196- return _waitFor ( active && getAnimation ( active ) , event ) . then (
197- ( ) => externalHandle
198- ) ;
204+ const animation = currentAnimation ( ) ;
205+ return _waitFor ( animation , event ) . then ( ( ) => externalHandle ) ;
199206 } ,
200207 } satisfies BaseAnimationHandle < Args >
201208 ) ;
0 commit comments