index-main/assets/js/themeToggle.js
Julie Blanc ea648498e2
All checks were successful
Deploy / Deploy to Production (push) Successful in 11s
banner script
2026-01-06 16:15:49 +01:00

26 lines
No EOL
787 B
JavaScript

export function themeToggle() {
const button = document.querySelector('#theme-toggle');
const root = document.documentElement;
if (!button) return;
// Appliquer le thème sauvegardé (si présent)
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
root.setAttribute('data-theme', 'light');
}
button.addEventListener('click', () => {
const isLight = root.getAttribute('data-theme') === 'light';
const nextTheme = isLight ? 'dark' : 'light';
root.setAttribute('data-theme', nextTheme);
localStorage.setItem('theme', nextTheme);
button.setAttribute(
'aria-label',
nextTheme === 'light' ? 'Activer le mode sombre' : 'Activer le mode clair'
);
});
}