-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle-theme.js
More file actions
28 lines (22 loc) · 883 Bytes
/
Copy pathtoggle-theme.js
File metadata and controls
28 lines (22 loc) · 883 Bytes
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
// Seleciona todos os heart-switch inputs
const hearts = document.querySelectorAll('.heart-switch input[type="checkbox"]');
// Função para aplicar tema baseado no localStorage
function applySavedTheme() {
const savedTheme = localStorage.getItem('theme');
const isDark = savedTheme === 'dark';
document.body.classList.toggle('dark', isDark);
// Atualiza todos os checkboxes com base no tema salvo
hearts.forEach(input => {
input.checked = isDark;
});
}
// Quando um checkbox for alterado
hearts.forEach(input => {
input.addEventListener('change', () => {
const anyChecked = Array.from(hearts).some(i => i.checked);
document.body.classList.toggle('dark', anyChecked);
localStorage.setItem('theme', anyChecked ? 'dark' : 'light');
});
});
// Aplica o tema salvo ao carregar a página
window.addEventListener('DOMContentLoaded', applySavedTheme);