Add dark mode theme switcher

This commit is contained in:
André Rüegger 2026-06-01 08:27:50 +02:00
parent 3cf07d0e1b
commit 4bf7dc2586
2 changed files with 224 additions and 22 deletions

View File

@ -1,41 +1,146 @@
/* App-wide theme tokens
Keep Open Props' sizing/spacing, but pin semantic colors to a readable
light theme so pages do not inherit low-contrast dark-mode surfaces. */
/* App-wide theme tokens.
Dark is the default. The data-theme attribute is set by js/auth.js. */
:root {
color-scheme: dark;
--app-bg: #121820;
--app-panel: #1c232b;
--app-panel-soft: #242d37;
--app-border: #46515f;
--app-text: #f4f7fb;
--app-text-muted: #b8c4d3;
--app-link: #8cb4ff;
--app-accent: #5d7cff;
--app-focus: #8cb4ff;
--surface-1: var(--app-panel);
--surface-2: var(--app-panel-soft);
--surface-3: #303a46;
--surface-4: var(--app-border);
--text-1: var(--app-text);
--text-2: var(--app-text-muted);
}
html[data-theme="light"] {
color-scheme: light;
--surface-1: #ffffff;
--surface-2: #f8fafc;
--surface-3: #eef2f7;
--surface-4: #cbd5e1;
--text-1: #17202a;
--text-2: #3d4b5c;
--muted-text: #64748b;
--app-bg: #f8fafc;
--app-panel: #ffffff;
--app-panel-soft: #f1f5f9;
--app-border: #cbd5e1;
--app-text: #17202a;
--app-text-muted: #526174;
--app-link: #3157d5;
--app-accent: #3157d5;
--app-focus: #3157d5;
--surface-1: var(--app-panel);
--surface-2: var(--app-panel-soft);
--surface-3: #e2e8f0;
--surface-4: var(--app-border);
--text-1: var(--app-text);
--text-2: var(--app-text-muted);
}
@media (prefers-color-scheme: light) {
html[data-theme="system"] {
color-scheme: light;
--app-bg: #f8fafc;
--app-panel: #ffffff;
--app-panel-soft: #f1f5f9;
--app-border: #cbd5e1;
--app-text: #17202a;
--app-text-muted: #526174;
--app-link: #3157d5;
--app-accent: #3157d5;
--app-focus: #3157d5;
--surface-1: var(--app-panel);
--surface-2: var(--app-panel-soft);
--surface-3: #e2e8f0;
--surface-4: var(--app-border);
--text-1: var(--app-text);
--text-2: var(--app-text-muted);
}
}
body,
.post-card,
.editor-form,
.login-container,
.comments-section,
.marker-card,
.sharing-panel,
.sharing-user,
.post,
.comment-item,
.sidebar .about {
background: var(--app-panel) !important;
color: var(--app-text) !important;
}
body {
background: var(--gray-0);
color: var(--text-1);
background: var(--app-bg) !important;
}
.site-header {
background: var(--app-panel) !important;
border-color: var(--app-border) !important;
}
.site-title a,
.site-nav a:hover,
.site-nav a.active,
a {
color: var(--app-link) !important;
}
.site-nav a,
.user-menu .username,
.post-meta,
.post-card-meta,
.post-card-excerpt,
.marker-date,
.marker-coords,
.comment-meta,
.sharing-help,
.empty-share-list,
.timeline-event .date {
color: var(--app-text-muted) !important;
}
.post-title,
.post-card-title a,
.chapter-header h3,
.login-header h1,
label,
.marker-header h4,
.post h3,
.sidebar h3,
.chapter-content,
.post-description,
.comment-text {
color: var(--app-text) !important;
}
input,
select,
textarea,
button {
color: var(--text-1);
textarea {
background: var(--app-panel-soft) !important;
border-color: var(--app-border) !important;
color: var(--app-text) !important;
}
input::placeholder,
textarea::placeholder {
color: var(--app-text-muted) !important;
}
input[type="file"] {
background: var(--surface-2);
color: var(--text-1);
background: var(--app-panel-soft) !important;
color: var(--app-text) !important;
}
input[type="file"]::file-selector-button {
border: 1px solid var(--app-border);
border-radius: 6px;
background: var(--surface-1);
color: var(--text-1);
background: var(--app-panel);
color: var(--app-text);
padding: 0.35rem 0.7rem;
margin-right: 0.75rem;
cursor: pointer;
@ -45,6 +150,11 @@ input[type="file"]::file-selector-button:hover {
background: var(--surface-3);
}
.badge {
background: color-mix(in srgb, var(--app-accent) 18%, transparent) !important;
color: var(--app-text) !important;
}
input:focus,
select:focus,
textarea:focus {
@ -52,6 +162,38 @@ textarea:focus {
outline-offset: 2px;
}
.theme-switcher {
display: inline-flex;
align-items: center;
gap: 0.4rem;
}
.theme-switcher label {
color: var(--app-text-muted) !important;
font-size: 0.85rem;
margin: 0;
}
.theme-select {
width: auto;
min-width: 6.5rem;
padding: 0.35rem 0.55rem;
border-radius: 6px;
font-size: 0.9rem;
}
.theme-switcher-floating {
position: fixed;
top: 1rem;
right: 1rem;
z-index: 1200;
background: var(--app-panel) !important;
border: 1px solid var(--app-border);
border-radius: 8px;
padding: 0.45rem 0.6rem;
box-shadow: var(--shadow-3);
}
/* Mobile-first approach */
.map-container {
height: 100vh;
@ -76,7 +218,8 @@ textarea:focus {
border-left: 3px solid #3498db;
padding: 15px 20px;
margin: 20px 0;
background-color: #f9f9f9;
background-color: var(--app-panel-soft);
color: var(--app-text);
}
.timeline-event .date {
@ -86,7 +229,7 @@ textarea:focus {
.timeline-event .location {
font-size: 0.9em;
color: #2c3e50;
color: var(--app-text-muted);
}
.images-container {

View File

@ -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);