-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
37 lines (31 loc) · 1.49 KB
/
Copy paththeme.js
File metadata and controls
37 lines (31 loc) · 1.49 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
(function() {
// 1. Detección inmediata para evitar parpadeo (FOIT)
const savedTheme = localStorage.getItem('theme');
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Priorizamos el tema guardado, si no existe, el valor por defecto es 'dark'.
const initialTheme = savedTheme || 'dark';
document.documentElement.setAttribute('data-theme', initialTheme);
// 2. Configuración una vez cargado el DOM
document.addEventListener('DOMContentLoaded', () => {
const themeToggle = document.getElementById('theme-toggle');
if (!themeToggle) return;
// Función para actualizar el icono
const updateIcon = (theme) => {
const isDark = theme === 'dark';
themeToggle.innerHTML = isDark
? '<i class="fa-solid fa-sun"></i>'
: '<i class="fa-solid fa-moon"></i>';
themeToggle.title = isDark ? 'Cambiar a modo claro' : 'Cambiar a modo oscuro';
};
// Inicializar icono
updateIcon(initialTheme);
// Lógica del switch
themeToggle.addEventListener('click', () => {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
updateIcon(newTheme);
});
});
})();