@@ -23,37 +23,39 @@ const Hamburger = () => {
23
23
const [ isExpanded , setisExpanded ] = useState ( false ) ;
24
24
const [ hidden , setHidden ] = useState ( 'invisible' ) ;
25
25
const [ isAnimating , setIsAnimating ] = useState ( false ) ;
26
- const animationTimeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
26
+ const animationTimeoutRef = useRef < ReturnType < typeof setTimeout > | null > (
27
+ null ,
28
+ ) ;
27
29
28
30
useEffect ( ( ) => {
29
31
if ( isExpanded ) {
30
32
setHidden ( '' ) ;
31
33
setIsAnimating ( true ) ;
32
-
34
+
33
35
// Clear any existing timeout
34
36
if ( animationTimeoutRef . current ) {
35
37
clearTimeout ( animationTimeoutRef . current ) ;
36
38
}
37
-
39
+
38
40
// Set a timeout for the animation duration
39
41
animationTimeoutRef . current = setTimeout ( ( ) => {
40
42
setIsAnimating ( false ) ;
41
43
} , 1000 ) ; // Match this with the animation duration
42
44
} else {
43
45
setIsAnimating ( true ) ;
44
-
46
+
45
47
// Clear any existing timeout
46
48
if ( animationTimeoutRef . current ) {
47
49
clearTimeout ( animationTimeoutRef . current ) ;
48
50
}
49
-
51
+
50
52
// Set a timeout for the animation duration and hiding
51
53
animationTimeoutRef . current = setTimeout ( ( ) => {
52
54
setHidden ( 'invisible' ) ;
53
55
setIsAnimating ( false ) ;
54
56
} , 1000 ) ; // Match this with the animation duration
55
57
}
56
-
58
+
57
59
// Cleanup function to clear timeout when component unmounts
58
60
return ( ) => {
59
61
if ( animationTimeoutRef . current ) {
@@ -67,7 +69,7 @@ const Hamburger = () => {
67
69
if ( isAnimating ) {
68
70
return ;
69
71
}
70
-
72
+
71
73
/**
72
74
* Anti-pattern: setisExpanded(!isExpanded)
73
75
* Even if your state updates are batched and multiple updates to the enabled/disabled state are made together
@@ -136,7 +138,10 @@ const Hamburger = () => {
136
138
} }
137
139
onKeyDown = { ( event ) => {
138
140
// 'Enter' key or 'Space' key
139
- if ( ( event . key === 'Enter' || event . key === ' ' ) && ! isAnimating ) {
141
+ if (
142
+ ( event . key === 'Enter' || event . key === ' ' ) &&
143
+ ! isAnimating
144
+ ) {
140
145
setisExpanded ( ( prevExpanded ) => ! prevExpanded ) ;
141
146
}
142
147
} }
0 commit comments