Files
breadcrumb-the-shire/web/js/components/app-theme-toggle.js

118 lines
3.2 KiB
JavaScript
Raw Normal View History

2026-02-11 19:28:12 +01:00
import { optionalEl, warnOnce } from '../core/app-dom.js';
2026-02-04 23:31:53 +01:00
const setTheme = (theme) => {
2026-02-11 19:28:12 +01:00
document.documentElement.dataset.theme = theme;
2026-02-04 23:31:53 +01:00
};
2026-02-11 19:28:12 +01:00
const isDarkTheme = (theme) => theme && theme.startsWith('dark');
const setIcon = (iconEl, theme) => {
if (!iconEl) {return;}
2026-02-11 19:28:12 +01:00
iconEl.classList.remove('bi-sun-fill', 'bi-moon-stars-fill');
iconEl.classList.add(isDarkTheme(theme) ? 'bi-moon-stars-fill' : 'bi-sun-fill');
2026-02-04 23:31:53 +01:00
};
2026-02-11 19:28:12 +01:00
const updateTheme = async (menu, theme) => {
const url = menu.dataset.themeUrl;
const csrfKey = menu.dataset.csrfKey;
const csrfToken = menu.dataset.csrfToken;
2026-02-04 23:31:53 +01:00
if (!url || !csrfKey || !csrfToken) {
return true;
}
const body = new URLSearchParams({ theme, [csrfKey]: csrfToken });
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'X-Requested-With': 'fetch'
},
body
});
return response.ok;
};
2026-02-11 19:28:12 +01:00
const initThemeMenu = () => {
const menu = optionalEl('[data-theme-menu]');
if (!menu) {return;}
2026-02-11 19:28:12 +01:00
const icon = menu.querySelector('[data-theme-icon]');
const options = Array.from(menu.querySelectorAll('[data-theme-option]'));
if (!options.length) {
warnOnce('UI_EL_MISSING', 'Missing theme options', { module: 'theme-toggle' });
return;
}
let pending = false;
2026-02-11 19:28:12 +01:00
const getCurrent = () => document.documentElement.dataset.theme || '';
const setActive = (theme) => {
options.forEach((option) => {
const isActive = option.dataset.themeValue === theme;
option.classList.toggle('active', isActive);
if (isActive) {
option.setAttribute('aria-current', 'true');
} else {
option.removeAttribute('aria-current');
}
});
};
setIcon(icon, getCurrent());
setActive(getCurrent());
options.forEach((option) => {
option.addEventListener('click', async (event) => {
event.preventDefault();
if (pending) {return;}
2026-02-11 19:28:12 +01:00
const next = option.dataset.themeValue || '';
if (!next) {return;}
2026-02-11 19:28:12 +01:00
const current = getCurrent();
if (next === current) {return;}
pending = true;
2026-02-11 19:28:12 +01:00
setTheme(next);
setIcon(icon, next);
setActive(next);
const ok = await updateTheme(menu, next);
if (!ok) {
setTheme(current);
setIcon(icon, current);
setActive(current);
}
pending = false;
2026-02-11 19:28:12 +01:00
});
});
};
2026-02-04 23:31:53 +01:00
const initThemeToggle = () => {
2026-02-11 19:28:12 +01:00
const button = optionalEl('[data-theme-toggle]');
if (!button) {return;}
2026-02-11 19:28:12 +01:00
const icon = button.querySelector('i');
let pending = false;
2026-02-11 19:28:12 +01:00
const getCurrent = () => document.documentElement.dataset.theme || '';
setIcon(icon, getCurrent());
2026-02-04 23:31:53 +01:00
button.addEventListener('click', async () => {
if (pending) {return;}
2026-02-04 23:31:53 +01:00
const current = getCurrent();
2026-02-11 19:28:12 +01:00
const next = isDarkTheme(current) ? 'light' : 'dark';
pending = true;
2026-02-04 23:31:53 +01:00
setTheme(next);
2026-02-11 19:28:12 +01:00
setIcon(icon, next);
2026-02-04 23:31:53 +01:00
const ok = await updateTheme(button, next);
if (!ok) {
setTheme(current);
2026-02-11 19:28:12 +01:00
setIcon(icon, current);
2026-02-04 23:31:53 +01:00
}
pending = false;
2026-02-04 23:31:53 +01:00
});
};
if (document.readyState === 'loading') {
2026-02-11 19:28:12 +01:00
document.addEventListener('DOMContentLoaded', () => {
initThemeMenu();
initThemeToggle();
});
2026-02-04 23:31:53 +01:00
} else {
2026-02-11 19:28:12 +01:00
initThemeMenu();
2026-02-04 23:31:53 +01:00
initThemeToggle();
}