@@ -12,7 +12,6 @@ const EXIT_DURATION_MS = 500
1212
1313const RE_ENTER_OPACITIES = [ 1 , 0.8 , 0.6 , 0.9 ] as const
1414
15-
1615function setBlockOpacity ( el : HTMLSpanElement | null , opacity : number , animate : boolean ) {
1716 if ( ! el ) return
1817 el . style . transition = animate ? `opacity ${ ENTER_DURATION_MS } ms ease-out` : 'none'
@@ -37,7 +36,11 @@ export function AnimatedColorBlocks() {
3736
3837 if ( prefersReducedMotion ) {
3938 blockRefs . current . forEach ( ( el ) => setBlockOpacity ( el , 1 , false ) )
40- return
39+ return ( ) => {
40+ mounted . current = false
41+ timers . current . forEach ( clearTimeout )
42+ timers . current = [ ]
43+ }
4144 }
4245
4346 blockRefs . current . forEach ( ( el ) => setBlockOpacity ( el , 0 , false ) )
@@ -97,7 +100,7 @@ export function AnimatedColorBlocks() {
97100 blockRefs . current [ i ] = el
98101 } }
99102 className = 'inline-block h-3 w-3'
100- style = { { backgroundColor : color , opacity : 0 } }
103+ style = { { backgroundColor : color , opacity : prefersReducedMotion ? 1 : 0 } }
101104 />
102105 ) ) }
103106 </ div >
@@ -112,28 +115,37 @@ export function AnimatedColorBlocksVertical() {
112115
113116 const verticalColors = [ COLORS [ 0 ] , COLORS [ 1 ] , COLORS [ 2 ] ] as const
114117
118+ function schedule ( fn : ( ) => void , ms : number ) {
119+ const id = setTimeout ( fn , ms )
120+ timers . current . push ( id )
121+ return id
122+ }
123+
115124 useEffect ( ( ) => {
116125 mounted . current = true
117126 timers . current = [ ]
118127
119128 if ( prefersReducedMotion ) {
120129 blockRefs . current . forEach ( ( el ) => setBlockOpacity ( el , 1 , false ) )
121- return
130+ return ( ) => {
131+ mounted . current = false
132+ timers . current . forEach ( clearTimeout )
133+ timers . current = [ ]
134+ }
122135 }
123136
124137 blockRefs . current . forEach ( ( el ) => setBlockOpacity ( el , 0 , false ) )
125138
126139 const baseDelay = COLORS . length * ENTER_STAGGER_MS + 100
127140
128141 verticalColors . forEach ( ( _ , i ) => {
129- const id = setTimeout (
142+ schedule (
130143 ( ) => {
131144 if ( ! mounted . current ) return
132145 setBlockOpacity ( blockRefs . current [ i ] , 1 , true )
133146 } ,
134147 baseDelay + i * ENTER_STAGGER_MS
135148 )
136- timers . current . push ( id )
137149 } )
138150
139151 return ( ) => {
@@ -152,7 +164,7 @@ export function AnimatedColorBlocksVertical() {
152164 blockRefs . current [ i ] = el
153165 } }
154166 className = 'inline-block h-3 w-3'
155- style = { { backgroundColor : color , opacity : 0 } }
167+ style = { { backgroundColor : color , opacity : prefersReducedMotion ? 1 : 0 } }
156168 />
157169 ) ) }
158170 </ div >
0 commit comments