-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
46 lines (38 loc) · 1.5 KB
/
theme.js
File metadata and controls
46 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// theme.js - Gestión del tema claro/oscuro
function initTheme() {
const themeToggle = document.getElementById('themeToggle');
if (!themeToggle) return;
// Detectar preferencia del sistema
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const systemTheme = systemPrefersDark ? 'dark' : 'light';
// Aplicar tema del sistema
applyTheme(systemTheme);
// Configurar evento del botón
themeToggle.addEventListener('click', function() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
applyTheme(newTheme);
});
// Escuchar cambios en el tema del sistema
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
mediaQuery.addEventListener('change', function(e) {
const newSystemTheme = e.matches ? 'dark' : 'light';
applyTheme(newSystemTheme);
});
function applyTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
updateThemeIcon(theme);
}
function updateThemeIcon(theme) {
const themeIcon = themeToggle.querySelector('.theme-icon');
if (themeIcon) {
themeIcon.textContent = theme === 'dark' ? '🌙' : '☀️';
}
}
}
// Esperar a que el DOM esté listo
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initTheme);
} else {
initTheme();
}