1- import { forwardRef , useImperativeHandle } from "react" ;
1+ import { forwardRef , useImperativeHandle , useRef } from "react" ;
22import type { AnimatedIconHandle , AnimatedIconProps } from "./types" ;
33import { motion , useAnimate } from "motion/react" ;
44
@@ -8,30 +8,38 @@ const AlarmClockPlusIcon = forwardRef<AnimatedIconHandle, AnimatedIconProps>(
88 ref ,
99 ) => {
1010 const [ scope , animate ] = useAnimate ( ) ;
11+ const animationControls = useRef < Array < ReturnType < typeof animate > > > ( [ ] ) ;
1112
1213 const start = async ( ) => {
13- animate (
14- ".clock" ,
15- {
16- y : - 1.5 ,
17- x : [ - 1 , 1 , - 1 , 1 , - 1 , 0 ] ,
18- } ,
19- {
20- y : { duration : 0.2 , type : "spring" , stiffness : 200 , damping : 25 } ,
21- x : { duration : 0.3 , repeat : Infinity , ease : "linear" } ,
22- } ,
14+ animationControls . current . forEach ( ( control ) => control . stop ( ) ) ;
15+ animationControls . current = [ ] ;
16+
17+ animationControls . current . push (
18+ animate (
19+ ".clock" ,
20+ {
21+ y : - 1.5 ,
22+ x : [ - 1 , 1 , - 1 , 1 , - 1 , 0 ] ,
23+ } ,
24+ {
25+ y : { duration : 0.2 , type : "spring" , stiffness : 200 , damping : 25 } ,
26+ x : { duration : 0.3 , repeat : Infinity , ease : "linear" } ,
27+ } ,
28+ ) ,
2329 ) ;
2430
25- animate (
26- ".bells" ,
27- {
28- y : - 2.5 ,
29- x : [ - 2 , 2 , - 2 , 2 , - 2 , 0 ] ,
30- } ,
31- {
32- y : { duration : 0.2 , type : "spring" , stiffness : 200 , damping : 25 } ,
33- x : { duration : 0.3 , repeat : Infinity , ease : "linear" } ,
34- } ,
31+ animationControls . current . push (
32+ animate (
33+ ".bells" ,
34+ {
35+ y : - 2.5 ,
36+ x : [ - 2 , 2 , - 2 , 2 , - 2 , 0 ] ,
37+ } ,
38+ {
39+ y : { duration : 0.2 , type : "spring" , stiffness : 200 , damping : 25 } ,
40+ x : { duration : 0.3 , repeat : Infinity , ease : "linear" } ,
41+ } ,
42+ ) ,
3543 ) ;
3644
3745 await animate (
@@ -42,6 +50,9 @@ const AlarmClockPlusIcon = forwardRef<AnimatedIconHandle, AnimatedIconProps>(
4250 } ;
4351
4452 const stop = ( ) => {
53+ animationControls . current . forEach ( ( control ) => control . stop ( ) ) ;
54+ animationControls . current = [ ] ;
55+
4556 animate ( ".clock" , { y : 0 , x : 0 } , { duration : 0.2 } ) ;
4657 animate ( ".bells" , { y : 0 , x : 0 } , { duration : 0.2 } ) ;
4758 } ;
0 commit comments