|
2 | 2 | (function() { |
3 | 3 | let compareChart = null; |
4 | 4 | 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(); |
6 | 12 |
|
7 | 13 | function getLeaderboardData() { |
8 | 14 | const dataScript = document.getElementById('leaderboard-data'); |
|
76 | 82 | } |
77 | 83 | const modal = document.getElementById('compare-modal'); |
78 | 84 | if (!modal) return; |
| 85 | + |
| 86 | + // Sync chart theme with global theme when opening modal |
| 87 | + chartTheme = getGlobalTheme(); |
| 88 | + updateThemeButton(); |
| 89 | + |
79 | 90 | modal.classList.add('show'); |
80 | 91 | modal.setAttribute('aria-hidden', 'false'); |
81 | 92 | renderChart(); |
|
322 | 333 | } |
323 | 334 |
|
324 | 335 | 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(); |
326 | 345 | updateThemeButton(); |
327 | 346 | renderChart(); |
328 | 347 | } |
|
392 | 411 | } |
393 | 412 |
|
394 | 413 | 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 | + |
395 | 432 | // Open via delegated event to handle dynamic rendering |
396 | 433 | document.addEventListener('click', (e) => { |
397 | 434 | const trigger = e.target && typeof e.target.closest === 'function' ? e.target.closest('#compare-btn') : null; |
|
0 commit comments