|
| 1 | +class Theme { |
| 2 | + |
| 3 | + constructor() { |
| 4 | + this.button = document.getElementById('theme-toggle'); |
| 5 | + this.darkModeIcon = '🌙'; |
| 6 | + this.lightModeIcon = '☀️'; |
| 7 | + |
| 8 | + if (this.button) { |
| 9 | + this.init(); |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + init() { |
| 14 | + // Load saved theme preference |
| 15 | + this.loadTheme(); |
| 16 | + |
| 17 | + // Add click event to toggle button |
| 18 | + this.button.addEventListener('click', () => { |
| 19 | + this.toggle(); |
| 20 | + }); |
| 21 | + |
| 22 | + // Add keyboard support |
| 23 | + this.button.addEventListener('keydown', (e) => { |
| 24 | + // Support Enter and Space keys |
| 25 | + if (e.key === 'Enter' || e.key === ' ') { |
| 26 | + e.preventDefault(); |
| 27 | + this.toggle(); |
| 28 | + } |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + loadTheme() { |
| 33 | + const savedTheme = localStorage.getItem('theme'); |
| 34 | + const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 35 | + |
| 36 | + // Apply light mode if saved, otherwise use system preference |
| 37 | + if (savedTheme === 'light' || (savedTheme === null && !prefersDark)) { |
| 38 | + this.setLightMode(false); |
| 39 | + } else { |
| 40 | + this.setDarkMode(false); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + toggle() { |
| 45 | + if (document.body.classList.contains('light-mode')) { |
| 46 | + this.setDarkMode(true); |
| 47 | + localStorage.setItem('theme', 'dark'); |
| 48 | + } else { |
| 49 | + this.setLightMode(true); |
| 50 | + localStorage.setItem('theme', 'light'); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + setLightMode(announce = true) { |
| 55 | + document.body.classList.add('light-mode'); |
| 56 | + this.updateIcon(this.darkModeIcon); |
| 57 | + this.updateAriaLabel('Switch to dark mode', 'Light mode'); |
| 58 | + |
| 59 | + if (announce) { |
| 60 | + this.announceChange('Light mode enabled'); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + setDarkMode(announce = true) { |
| 65 | + document.body.classList.remove('light-mode'); |
| 66 | + this.updateIcon(this.lightModeIcon); |
| 67 | + this.updateAriaLabel('Switch to light mode', 'Dark mode'); |
| 68 | + |
| 69 | + if (announce) { |
| 70 | + this.announceChange('Dark mode enabled'); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + updateIcon(icon) { |
| 75 | + const iconElement = this.button.querySelector('.theme-toggle-icon'); |
| 76 | + if (iconElement) { |
| 77 | + iconElement.textContent = icon; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + updateAriaLabel(label, currentTheme) { |
| 82 | + this.button.setAttribute('aria-label', label); |
| 83 | + |
| 84 | + const visuallyHiddenText = this.button.querySelector('.visually-hidden'); |
| 85 | + if (visuallyHiddenText) { |
| 86 | + visuallyHiddenText.textContent = `Current theme: ${currentTheme}`; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + announceChange(message) { |
| 91 | + // Create a temporary live region for screen readers |
| 92 | + const announcement = document.createElement('div'); |
| 93 | + announcement.setAttribute('role', 'status'); |
| 94 | + announcement.setAttribute('aria-live', 'polite'); |
| 95 | + announcement.classList.add('visually-hidden'); |
| 96 | + announcement.textContent = message; |
| 97 | + |
| 98 | + document.body.appendChild(announcement); |
| 99 | + |
| 100 | + // Remove after announcement |
| 101 | + setTimeout(() => { |
| 102 | + document.body.removeChild(announcement); |
| 103 | + }, 1000); |
| 104 | + } |
| 105 | +} |
0 commit comments