Skip to content

Commit ed3cc87

Browse files
authored
Merge pull request #646 from ContriFork/fix/theme-toggle-flickering
Fix flickering when toggling theme #643
2 parents 2e8f6ec + c63cf43 commit ed3cc87

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/components/common/BasicScripts.astro

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ import { UI } from 'astrowind:config';
6868
if (defaultTheme.endsWith(':only')) {
6969
return;
7070
}
71+
72+
Observer.removeAnimationDelay();
73+
7174
document.documentElement.classList.toggle('dark');
7275
localStorage.theme = document.documentElement.classList.contains('dark') ? 'dark' : 'light';
7376
});
@@ -167,6 +170,7 @@ import { UI } from 'astrowind:config';
167170
observer: null,
168171
delayBetweenAnimations: 100,
169172
animationCounter: 0,
173+
elements: null,
170174

171175
start() {
172176
const selectors = [
@@ -179,7 +183,7 @@ import { UI } from 'astrowind:config';
179183
'[class$=" intersect"]',
180184
];
181185

182-
const elements = Array.from(document.querySelectorAll(selectors.join(',')));
186+
this.elements = Array.from(document.querySelectorAll(selectors.join(',')));
183187

184188
const getThreshold = (element) => {
185189
if (element.classList.contains('intersect-full')) return 0.99;
@@ -188,7 +192,7 @@ import { UI } from 'astrowind:config';
188192
return 0;
189193
};
190194

191-
elements.forEach((el) => {
195+
this.elements.forEach((el) => {
192196
el.setAttribute('no-intersect', '');
193197
el._intersectionThreshold = getThreshold(el);
194198
});
@@ -241,10 +245,25 @@ import { UI } from 'astrowind:config';
241245

242246
this.observer = new IntersectionObserver(callback.bind(this), { threshold: [0, 0.25, 0.5, 0.99] });
243247

244-
elements.forEach((el) => {
248+
this.elements.forEach((el) => {
245249
this.observer.observe(el);
246250
});
247251
},
252+
253+
/*
254+
REF: #643;
255+
We need to remove the delay to fix flickering/delay
256+
when toggling the theme. Observer only removes them
257+
after data-animated is gone (out of view).
258+
*/
259+
removeAnimationDelay() {
260+
this.elements.forEach((el) => {
261+
if (el.getAttribute('data-animated') === 'true') {
262+
el.style.transitionDelay = '';
263+
el.style.animationDelay = '';
264+
}
265+
});
266+
},
248267
};
249268

250269
Observer.start();

0 commit comments

Comments
 (0)