Skip to content

Commit f407608

Browse files
committed
Fix issues with light/dark mode
1 parent b01fe9c commit f407608

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

js/analysis.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
(function() {
33
let compareChart = null;
44
let resizeObserver = null;
5-
let chartTheme = 'dark'; // 'light' or 'dark'
5+
6+
// Initialize chartTheme based on global dark mode state
7+
function getGlobalTheme() {
8+
return document.body.classList.contains('dark-mode') ? 'dark' : 'light';
9+
}
10+
11+
let chartTheme = getGlobalTheme();
612

713
function getLeaderboardData() {
814
const dataScript = document.getElementById('leaderboard-data');
@@ -76,6 +82,11 @@
7682
}
7783
const modal = document.getElementById('compare-modal');
7884
if (!modal) return;
85+
86+
// Sync chart theme with global theme when opening modal
87+
chartTheme = getGlobalTheme();
88+
updateThemeButton();
89+
7990
modal.classList.add('show');
8091
modal.setAttribute('aria-hidden', 'false');
8192
renderChart();
@@ -322,7 +333,15 @@
322333
}
323334

324335
function toggleChartTheme() {
325-
chartTheme = chartTheme === 'light' ? 'dark' : 'light';
336+
// Toggle global dark mode
337+
document.body.classList.toggle('dark-mode');
338+
339+
// Update localStorage to persist the preference
340+
const isDarkMode = document.body.classList.contains('dark-mode');
341+
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
342+
343+
// Update chart theme to match global theme
344+
chartTheme = getGlobalTheme();
326345
updateThemeButton();
327346
renderChart();
328347
}
@@ -392,6 +411,24 @@
392411
}
393412

394413
function initEvents() {
414+
// Listen for global theme changes (e.g., from sidebar toggle)
415+
const observer = new MutationObserver((mutations) => {
416+
mutations.forEach((mutation) => {
417+
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
418+
const newTheme = getGlobalTheme();
419+
if (newTheme !== chartTheme) {
420+
chartTheme = newTheme;
421+
updateThemeButton();
422+
// Only re-render if modal is open
423+
if (document.getElementById('compare-modal')?.classList.contains('show')) {
424+
renderChart();
425+
}
426+
}
427+
}
428+
});
429+
});
430+
observer.observe(document.body, { attributes: true });
431+
395432
// Open via delegated event to handle dynamic rendering
396433
document.addEventListener('click', (e) => {
397434
const trigger = e.target && typeof e.target.closest === 'function' ? e.target.closest('#compare-btn') : null;

0 commit comments

Comments
 (0)