|
1 | 1 | // DSPy UI JavaScript |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Initialize theme on page load |
| 5 | + * Reads theme preference from localStorage or falls back to system preference |
| 6 | + */ |
| 7 | +function initTheme() { |
| 8 | + // Check localStorage for saved preference |
| 9 | + const savedTheme = localStorage.getItem('theme'); |
| 10 | + |
| 11 | + if (savedTheme) { |
| 12 | + // Use saved preference |
| 13 | + document.documentElement.setAttribute('data-theme', savedTheme); |
| 14 | + } else { |
| 15 | + // Use system preference if no saved preference |
| 16 | + const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 17 | + const theme = prefersDark ? 'dark' : 'light'; |
| 18 | + document.documentElement.setAttribute('data-theme', theme); |
| 19 | + } |
| 20 | + |
| 21 | + // Update toggle button icon if it exists |
| 22 | + updateThemeIcon(); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Toggle between light and dark themes |
| 27 | + */ |
| 28 | +function toggleTheme() { |
| 29 | + const currentTheme = document.documentElement.getAttribute('data-theme'); |
| 30 | + const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; |
| 31 | + |
| 32 | + // Update theme |
| 33 | + document.documentElement.setAttribute('data-theme', newTheme); |
| 34 | + |
| 35 | + // Save preference |
| 36 | + localStorage.setItem('theme', newTheme); |
| 37 | + |
| 38 | + // Update icon |
| 39 | + updateThemeIcon(); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Update theme toggle button icon |
| 44 | + */ |
| 45 | +function updateThemeIcon() { |
| 46 | + const themeIcon = document.getElementById('themeIcon'); |
| 47 | + if (!themeIcon) return; |
| 48 | + |
| 49 | + const currentTheme = document.documentElement.getAttribute('data-theme'); |
| 50 | + // Show moon for light mode (click to go dark), sun for dark mode (click to go light) |
| 51 | + themeIcon.textContent = currentTheme === 'dark' ? '☀️' : '🌙'; |
| 52 | +} |
| 53 | + |
3 | 54 | /** |
4 | 55 | * Initialize the program page with event handlers and log loading |
5 | 56 | */ |
|
0 commit comments