Add dark mode theme switcher
This commit is contained in:
1 parent
3cf07d0e1b
commit
4bf7dc2586
2 files changed
+224
-22
No files matched your search
+60
-1
@@ -1,6 +1,57 @@
|
||||
const API_BASE = "http://127.0.0.1:5000/api";
|
||||
const THEME_STORAGE_KEY = "journey-theme";
|
||||
const DEFAULT_THEME = "dark";
|
||||
const VALID_THEMES = ["dark", "light", "system"];
|
||||
let currentUser = null;
|
||||
|
||||
function getThemePreference() {
|
||||
const storedTheme = localStorage.getItem(THEME_STORAGE_KEY);
|
||||
return VALID_THEMES.includes(storedTheme) ? storedTheme : DEFAULT_THEME;
|
||||
}
|
||||
|
||||
function applyThemePreference(theme = getThemePreference()) {
|
||||
const selectedTheme = VALID_THEMES.includes(theme) ? theme : DEFAULT_THEME;
|
||||
document.documentElement.dataset.theme = selectedTheme;
|
||||
}
|
||||
|
||||
function setThemePreference(theme) {
|
||||
const selectedTheme = VALID_THEMES.includes(theme) ? theme : DEFAULT_THEME;
|
||||
localStorage.setItem(THEME_STORAGE_KEY, selectedTheme);
|
||||
applyThemePreference(selectedTheme);
|
||||
}
|
||||
|
||||
function getThemeControlHtml() {
|
||||
return `
|
||||
<div class="theme-switcher">
|
||||
<label for="theme-select">Theme</label>
|
||||
<select id="theme-select" class="theme-select" aria-label="Theme">
|
||||
<option value="dark">Dark</option>
|
||||
<option value="light">Light</option>
|
||||
<option value="system">System</option>
|
||||
</select>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function attachThemeControl(root = document) {
|
||||
const select = root.querySelector("#theme-select");
|
||||
if (!select) return;
|
||||
select.value = getThemePreference();
|
||||
select.addEventListener("change", () => setThemePreference(select.value));
|
||||
}
|
||||
|
||||
function ensureStandaloneThemeControl() {
|
||||
if (document.getElementById("user-menu") || document.getElementById("theme-switcher-floating")) return;
|
||||
const container = document.createElement("div");
|
||||
container.id = "theme-switcher-floating";
|
||||
container.className = "theme-switcher-floating";
|
||||
container.innerHTML = getThemeControlHtml();
|
||||
document.body.appendChild(container);
|
||||
attachThemeControl(container);
|
||||
}
|
||||
|
||||
applyThemePreference();
|
||||
|
||||
async function checkAuth() {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/me`, { credentials: "include" });
|
||||
@@ -28,12 +79,18 @@ function updateUserMenu() {
|
||||
if (!container) return;
|
||||
if (currentUser) {
|
||||
container.innerHTML = `
|
||||
${getThemeControlHtml()}
|
||||
<span class="username"><i class="fas fa-user"></i> ${escapeHtml(currentUser.username)}</span>
|
||||
<button id="logout-btn" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</button>
|
||||
`;
|
||||
attachThemeControl(container);
|
||||
document.getElementById("logout-btn")?.addEventListener("click", logout);
|
||||
} else {
|
||||
container.innerHTML = `<button id="login-open-btn" class="login-btn"><i class="fas fa-sign-in-alt"></i> Login</button>`;
|
||||
container.innerHTML = `
|
||||
${getThemeControlHtml()}
|
||||
<button id="login-open-btn" class="login-btn"><i class="fas fa-sign-in-alt"></i> Login</button>
|
||||
`;
|
||||
attachThemeControl(container);
|
||||
document.getElementById("login-open-btn")?.addEventListener("click", () => {
|
||||
window.location.href = "login.html";
|
||||
});
|
||||
@@ -65,3 +122,5 @@ function showToast(msg, isError = false) {
|
||||
toast.style.display = "none";
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", ensureStandaloneThemeControl);
|
||||
Reference in new issue
Block a user