feat(frontend): user-switchable light/dark theme
Build Art IoT image / build-and-publish (push) Successful in 21s

- dark tokens move from prefers-color-scheme to :root[data-theme="dark"];
  light is the default for everyone regardless of OS preference
- choice persisted in localStorage (artiot-theme), applied to <html> at
  module load so the login screen themes before first paint
- Account page gains an Appearance section with a squared slide switch;
  meta theme-color follows the active theme
This commit is contained in:
2026-07-18 10:00:12 +02:00
parent 96ea568e53
commit 272a6ad97d
4 changed files with 98 additions and 20 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" /> <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="theme-color" content="#171d1b" /> <meta name="theme-color" content="#f9faf9" />
<link rel="icon" href="/icon.svg" type="image/svg+xml" /> <link rel="icon" href="/icon.svg" type="image/svg+xml" />
<link rel="manifest" href="/manifest.json" /> <link rel="manifest" href="/manifest.json" />
<title>Art IoT</title> <title>Art IoT</title>
+26
View File
@@ -61,6 +61,31 @@ function MyPermissions() {
); );
} }
function AppearanceSection() {
const theme = useApp((s) => s.theme);
const setTheme = useApp((s) => s.setTheme);
return (
<section className="panel panel--narrow">
<header className="panel__head">
<h2>Appearance</h2>
</header>
<label className="switch">
<span className="switch__label">Dark theme</span>
<input
type="checkbox"
role="switch"
checked={theme === "dark"}
onChange={(e) => setTheme(e.target.checked ? "dark" : "light")}
/>
<span className="switch__track" aria-hidden>
<span className="switch__thumb" />
</span>
</label>
<p className="panel__note">Saved on this device.</p>
</section>
);
}
export default function Account() { export default function Account() {
const me = useApp((s) => s.me)!; const me = useApp((s) => s.me)!;
const toast = useApp((s) => s.toast); const toast = useApp((s) => s.toast);
@@ -91,6 +116,7 @@ export default function Account() {
return ( return (
<> <>
<AppearanceSection />
<MyPermissions /> <MyPermissions />
<section className="panel panel--narrow"> <section className="panel panel--narrow">
<header className="panel__head"> <header className="panel__head">
+24
View File
@@ -1,11 +1,29 @@
import { create } from "zustand"; import { create } from "zustand";
import type { Me } from "./types"; import type { Me } from "./types";
export type Theme = "light" | "dark";
const THEME_KEY = "artiot-theme";
function applyTheme(theme: Theme) {
document.documentElement.dataset.theme = theme;
document
.querySelector('meta[name="theme-color"]')
?.setAttribute("content", theme === "dark" ? "#1b211f" : "#f9faf9");
}
// Light unless the user chose dark before; applied at module load, before
// the first render, so the login screen is themed too.
const initialTheme: Theme = localStorage.getItem(THEME_KEY) === "dark" ? "dark" : "light";
applyTheme(initialTheme);
/** Auth + app-shell state (zustand). Server data lives in TanStack Query. */ /** Auth + app-shell state (zustand). Server data lives in TanStack Query. */
interface AppState { interface AppState {
me: Me | null; me: Me | null;
authChecked: boolean; // finished the initial /api/me probe authChecked: boolean; // finished the initial /api/me probe
setMe: (me: Me | null) => void; setMe: (me: Me | null) => void;
theme: Theme;
setTheme: (theme: Theme) => void;
toasts: { id: number; text: string; tone: "info" | "error" }[]; toasts: { id: number; text: string; tone: "info" | "error" }[];
toast: (text: string, tone?: "info" | "error") => void; toast: (text: string, tone?: "info" | "error") => void;
dismissToast: (id: number) => void; dismissToast: (id: number) => void;
@@ -17,6 +35,12 @@ export const useApp = create<AppState>((set) => ({
me: null, me: null,
authChecked: false, authChecked: false,
setMe: (me) => set({ me, authChecked: true }), setMe: (me) => set({ me, authChecked: true }),
theme: initialTheme,
setTheme: (theme) => {
localStorage.setItem(THEME_KEY, theme);
applyTheme(theme);
set({ theme });
},
toasts: [], toasts: [],
toast: (text, tone = "info") => { toast: (text, tone = "info") => {
const id = ++toastId; const id = ++toastId;
+47 -19
View File
@@ -32,29 +32,30 @@
--label-track: 0.11em; --label-track: 0.11em;
} }
@media (prefers-color-scheme: dark) { /* Dark is opt-in via the Account switch (persisted in localStorage);
:root { light is the default regardless of the OS preference. */
--paper: #131816; :root[data-theme="dark"] {
--panel: #1b211f; --paper: #131816;
--panel-inset: #161c1a; --panel: #1b211f;
--ink: #e4e9e7; --panel-inset: #161c1a;
--ink-dim: #93a09b; --ink: #e4e9e7;
--line: #2c3531; --ink-dim: #93a09b;
--copper: #d07a3e; --line: #2c3531;
--copper-deep: #b25c28; --copper: #d07a3e;
--live: #2fae7c; --copper-deep: #b25c28;
--blue: #4f8ee8; --live: #2fae7c;
--amber: #d99a1b; --blue: #4f8ee8;
--red: #d8503f; --amber: #d99a1b;
--lamp-off: #39433f; --red: #d8503f;
--shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 4px 18px rgb(0 0 0 / 0.35); --lamp-off: #39433f;
--dropshadow: drop-shadow(0 1px 2px rgb(0 0 0 / 0.45)) drop-shadow(0 4px 10px rgb(0 0 0 / 0.35)); --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 4px 18px rgb(0 0 0 / 0.35);
} --dropshadow: drop-shadow(0 1px 2px rgb(0 0 0 / 0.45)) drop-shadow(0 4px 10px rgb(0 0 0 / 0.35));
} }
* { box-sizing: border-box; margin: 0; } * { box-sizing: border-box; margin: 0; }
html { color-scheme: light dark; } html { color-scheme: light; }
html[data-theme="dark"] { color-scheme: dark; }
body { body {
background: var(--paper); background: var(--paper);
@@ -391,6 +392,33 @@ button, input, select { font: inherit; color: inherit; border-radius: 0; }
.formrow { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); margin-bottom: 12px; } .formrow { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); margin-bottom: 12px; }
/* Slide switch, squared like everything else — a slider on the panel face. */
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0; cursor: pointer; }
.switch__label { font-weight: 500; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch__track {
width: 42px; height: 22px; flex: none;
border: 1px solid var(--line);
background: var(--panel-inset);
padding: 2px;
display: inline-flex;
transition: background 0.15s, border-color 0.15s;
}
.switch__thumb {
width: 16px; height: 16px;
background: var(--ink-dim);
transition: transform 0.15s, background 0.15s;
}
.switch input:checked + .switch__track {
background: color-mix(in srgb, var(--copper) 15%, transparent);
border-color: var(--copper);
}
.switch input:checked + .switch__track .switch__thumb {
transform: translateX(20px);
background: var(--copper);
}
.switch input:focus-visible + .switch__track { outline: 2px solid var(--copper); outline-offset: 2px; }
.check { display: flex; align-items: center; gap: 9px; padding: 4px 0; cursor: pointer; } .check { display: flex; align-items: center; gap: 9px; padding: 4px 0; cursor: pointer; }
.check input { accent-color: var(--copper); width: 16px; height: 16px; flex: none; } .check input { accent-color: var(--copper); width: 16px; height: 16px; flex: none; }
.check--strong { font-weight: 600; } .check--strong { font-weight: 600; }