feat(frontend): user-switchable light/dark theme
Build Art IoT image / build-and-publish (push) Successful in 21s
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:
+1
-1
@@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<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="manifest" href="/manifest.json" />
|
||||
<title>Art IoT</title>
|
||||
|
||||
@@ -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() {
|
||||
const me = useApp((s) => s.me)!;
|
||||
const toast = useApp((s) => s.toast);
|
||||
@@ -91,6 +116,7 @@ export default function Account() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<AppearanceSection />
|
||||
<MyPermissions />
|
||||
<section className="panel panel--narrow">
|
||||
<header className="panel__head">
|
||||
|
||||
@@ -1,11 +1,29 @@
|
||||
import { create } from "zustand";
|
||||
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. */
|
||||
interface AppState {
|
||||
me: Me | null;
|
||||
authChecked: boolean; // finished the initial /api/me probe
|
||||
setMe: (me: Me | null) => void;
|
||||
theme: Theme;
|
||||
setTheme: (theme: Theme) => void;
|
||||
toasts: { id: number; text: string; tone: "info" | "error" }[];
|
||||
toast: (text: string, tone?: "info" | "error") => void;
|
||||
dismissToast: (id: number) => void;
|
||||
@@ -17,6 +35,12 @@ export const useApp = create<AppState>((set) => ({
|
||||
me: null,
|
||||
authChecked: false,
|
||||
setMe: (me) => set({ me, authChecked: true }),
|
||||
theme: initialTheme,
|
||||
setTheme: (theme) => {
|
||||
localStorage.setItem(THEME_KEY, theme);
|
||||
applyTheme(theme);
|
||||
set({ theme });
|
||||
},
|
||||
toasts: [],
|
||||
toast: (text, tone = "info") => {
|
||||
const id = ++toastId;
|
||||
|
||||
+32
-4
@@ -32,8 +32,9 @@
|
||||
--label-track: 0.11em;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
/* Dark is opt-in via the Account switch (persisted in localStorage);
|
||||
light is the default regardless of the OS preference. */
|
||||
:root[data-theme="dark"] {
|
||||
--paper: #131816;
|
||||
--panel: #1b211f;
|
||||
--panel-inset: #161c1a;
|
||||
@@ -50,11 +51,11 @@
|
||||
--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; }
|
||||
|
||||
html { color-scheme: light dark; }
|
||||
html { color-scheme: light; }
|
||||
html[data-theme="dark"] { color-scheme: dark; }
|
||||
|
||||
body {
|
||||
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; }
|
||||
|
||||
/* 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 input { accent-color: var(--copper); width: 16px; height: 16px; flex: none; }
|
||||
.check--strong { font-weight: 600; }
|
||||
|
||||
Reference in New Issue
Block a user