-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-manager.js
More file actions
34 lines (31 loc) · 1.3 KB
/
Copy paththeme-manager.js
File metadata and controls
34 lines (31 loc) · 1.3 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
export function initTheme() {
// Apply initial theme from LS
const shouldDark = localStorage.getItem('bookflow_theme') === 'dark';
document.documentElement.classList.toggle('dark-theme', shouldDark);
// Update label if button exists
const btn = document.getElementById('btnToggleTheme');
// Helper to update label text using translation if available
const updateLabel = () => {
if (!btn) return;
const isDark = document.documentElement.classList.contains('dark-theme');
btn.innerHTML = isDark
? `<i class="ph-bold ph-sun"></i> ${window?.t?.('light_mode') ?? 'Light'}`
: `<i class="ph-bold ph-moon"></i> ${window?.t?.('dark_mode') ?? 'Dark'}`;
};
// Ensure label reflects current state on load
updateLabel();
// Bind once
if (!btn?.dataset?.bookflowBound) {
btn.dataset.bookflowBound = '1';
btn.addEventListener('click', () => {
const isDark = document.documentElement.classList.contains('dark-theme');
document.documentElement.classList.toggle('dark-theme', !isDark);
localStorage.setItem('bookflow_theme', !isDark ? 'dark' : 'light');
updateLabel();
});
}
}
export function applyThemeFromLS() {
const shouldDark = localStorage.getItem('bookflow_theme') === 'dark';
document.documentElement.classList.toggle('dark-theme', shouldDark);
}