Add user-created custom design themes

Lets users build their own theme packages from Options > Design: pick a
structural base (Standard or Nova), write free-form CSS, and save/select
it locally. Supports duplicate, export/import (JSON), and delete.

Custom CSS is injected via a dedicated <style> tag while the theme is
active. Fixes the built-in theme variable rules (.ts-app,
.ts-app.ts-theme-dark, .ts-app.ts-design-nova) so a plain .ts-app { ... }
override in custom CSS always wins, by wrapping them in :where() to
strip their specificity - previously the compound selectors always beat
a same-specificity custom rule regardless of injection order.

Standard/Nova behavior is unchanged when no custom theme is selected.
This commit is contained in:
Moepchi committed 2026-08-29 04:05:18 +02:00
1 parent 3ad086aedd
commit 8c954f9540
3 files changed
+385 -24

No files matched your search

+78 -3
View File
@@ -1,4 +1,11 @@
.ts-app {
/* Wrapped in :where() so these three variable-defining rules carry ZERO
specificity. That's deliberate: a user-authored custom theme (injected
later as a plain `.ts-app { --accent: ... }` rule) has real specificity
and must always win here, regardless of which of these three blocks would
otherwise apply. Every other selector in this file is untouched, so the
normal cascade elsewhere (including .ts-app.ts-design-nova's non-variable
rules further down) still works exactly as before. */
:where(.ts-app) {
--bg: #ffffff;
--bg-toolbar-1: #f5f6f8;
--bg-toolbar-2: #dfe2e6;
@@ -40,7 +47,7 @@
0 0 40px rgba(0, 0, 0, 0.5);
}
.ts-app.ts-theme-dark {
:where(.ts-app.ts-theme-dark) {
--bg: #1e1f22;
--bg-toolbar-1: #2b2d31;
--bg-toolbar-2: #232428;
@@ -67,7 +74,7 @@
/* Nova: alternate design, independent of the light/dark toggle (always dark). */
@import url("https://fonts.googleapis.com/css2?family=Manrope:wght@500;600;700;800&display=swap");
.ts-app.ts-design-nova {
:where(.ts-app.ts-design-nova) {
--success: oklch(0.75 0.15 150);
--bg: oklch(0.17 0.014 258);
--bg-toolbar-1: oklch(0.205 0.015 258);
@@ -2419,6 +2426,74 @@
color: var(--text-muted);
}
/* Custom-theme cards are a <div> (built-ins are a plain <button>) so the select
area and the edit/duplicate/export/delete row can sit side by side without
nesting a button inside a button. */
.ts-design-theme-card-custom {
position: relative;
}
.ts-design-theme-card-select {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.4rem;
width: 100%;
padding: 0;
border: none;
background: transparent;
color: inherit;
cursor: pointer;
text-align: left;
font: inherit;
}
.ts-design-theme-swatch-custom {
background: conic-gradient(from 180deg, #7fb0e8, #ff8fab, #ffd166, #7fb0e8);
}
.ts-design-theme-card-actions {
display: flex;
gap: 0.3rem;
margin-top: 0.5rem;
}
.ts-design-theme-card-actions button {
flex: 1;
padding: 0.25rem 0;
border: 1px solid var(--border-soft);
border-radius: 3px;
background: var(--input-bg);
color: var(--text-muted);
cursor: pointer;
font-size: 12px;
}
.ts-design-theme-card-actions button:hover {
color: var(--text);
border-color: var(--accent);
}
.ts-design-custom-actions {
display: flex;
gap: 0.5rem;
margin-top: 0.75rem;
}
.ts-custom-theme-css {
width: 100%;
min-height: 220px;
font-family: "IBM Plex Mono", ui-monospace, Consolas, monospace;
font-size: 12px;
padding: 0.5rem;
border: 1px solid var(--border);
border-radius: 4px;
background: var(--input-bg);
color: var(--input-text);
resize: vertical;
box-sizing: border-box;
}
.ts-options-columns {
display: flex;
gap: 1rem;
+275 -21
View File
@@ -138,13 +138,56 @@ const NOISE_SUPPRESSION_KEY = "webspeak3:noise-suppression";
const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation";
const VAD_HANGOVER_KEY = "webspeak3:vad-hangover";
const DESIGN_THEME_KEY = "webspeak3:design-theme";
// The user's actual pick - "standard" | "nova" | "custom:<id>". DESIGN_THEME_KEY above
// keeps tracking just the resolved structural base, since a couple of early call sites
// (e.g. the host-field default below) need that cheaply, before customThemes is loaded.
const DESIGN_SELECTION_KEY = "webspeak3:design-selection";
const CUSTOM_THEMES_KEY = "webspeak3:custom-themes";
type DesignTheme = "standard" | "nova";
/** A user-authored theme package: a name, which built-in layout it behaves like
* (Standard's plain chrome vs Nova's collapsed menu/splash behavior), and raw CSS
* that gets injected while it's active - free-form, so it can restyle or even
* reflow the existing markup (e.g. via flex `order`, `display:none`, overlays). */
type CustomTheme = {
id: string;
name: string;
baseTheme: DesignTheme;
css: string;
};
function loadDesignTheme(): DesignTheme {
return localStorage.getItem(DESIGN_THEME_KEY) === "nova" ? "nova" : "standard";
}
function loadCustomThemes(): CustomTheme[] {
try {
const raw = localStorage.getItem(CUSTOM_THEMES_KEY);
if (!raw) return [];
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
return parsed.filter(
(v): v is CustomTheme =>
v &&
typeof v.id === "string" &&
typeof v.name === "string" &&
(v.baseTheme === "standard" || v.baseTheme === "nova") &&
typeof v.css === "string"
);
} catch {
return [];
}
}
function saveCustomThemes(list: CustomTheme[]) {
localStorage.setItem(CUSTOM_THEMES_KEY, JSON.stringify(list));
}
function loadDesignSelection(): string {
return localStorage.getItem(DESIGN_SELECTION_KEY) ?? loadDesignTheme();
}
function loadBoolPref(key: string, fallback: boolean): boolean {
const raw = localStorage.getItem(key);
return raw === null ? fallback : raw === "1";
@@ -3571,34 +3614,185 @@ function AnwendungPanel() {
);
}
type CustomThemeDraft = { id: string | null; name: string; baseTheme: DesignTheme; css: string };
function DesignPanel({
designTheme,
onDesignThemeChange,
designSelection,
onSelectionChange,
customThemes,
onSaveCustomTheme,
onDeleteCustomTheme,
}: {
designTheme: DesignTheme;
onDesignThemeChange: (next: DesignTheme) => void;
designSelection: string;
onSelectionChange: (next: string) => void;
customThemes: CustomTheme[];
onSaveCustomTheme: (theme: CustomTheme) => void;
onDeleteCustomTheme: (id: string) => void;
}) {
const t = useT();
const themes: { id: DesignTheme; name: string; desc: string }[] = [
const [editing, setEditing] = useState<CustomThemeDraft | null>(null);
const importInputRef = useRef<HTMLInputElement | null>(null);
const builtins: { id: DesignTheme; name: string; desc: string }[] = [
{ id: "standard", name: t("design.theme.standard"), desc: t("design.theme.standard.desc") },
{ id: "nova", name: t("design.theme.nova"), desc: t("design.theme.nova.desc") },
];
const handleSaveEditing = () => {
if (!editing || !editing.name.trim()) return;
const id = editing.id ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
onSaveCustomTheme({ id, name: editing.name.trim(), baseTheme: editing.baseTheme, css: editing.css });
onSelectionChange(`custom:${id}`);
setEditing(null);
};
const handleExport = (theme: CustomTheme) => {
const blob = new Blob(
[JSON.stringify({ name: theme.name, baseTheme: theme.baseTheme, css: theme.css }, null, 2)],
{ type: "application/json" }
);
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `${theme.name.replace(/[^a-z0-9-_]+/gi, "_") || "theme"}.webspeak3theme.json`;
a.click();
URL.revokeObjectURL(url);
};
const handleImportSelected = async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
e.target.value = "";
if (!file) return;
try {
const parsed = JSON.parse(await file.text());
if (
!parsed ||
typeof parsed.name !== "string" ||
(parsed.baseTheme !== "standard" && parsed.baseTheme !== "nova") ||
typeof parsed.css !== "string"
) {
window.alert(t("design.custom.importError"));
return;
}
const id = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
onSaveCustomTheme({ id, name: parsed.name, baseTheme: parsed.baseTheme, css: parsed.css });
onSelectionChange(`custom:${id}`);
} catch {
window.alert(t("design.custom.importError"));
}
};
if (editing) {
return (
<>
<h3>{editing.id ? t("design.custom.editTitle") : t("design.custom.newTitle")}</h3>
<label className="ts-options-field">
{t("design.custom.name")}
<input value={editing.name} onChange={(e) => setEditing({ ...editing, name: e.target.value })} autoFocus />
</label>
<label className="ts-options-field">
{t("design.custom.base")}
<select
value={editing.baseTheme}
onChange={(e) => setEditing({ ...editing, baseTheme: e.target.value as DesignTheme })}
>
<option value="standard">{t("design.theme.standard")}</option>
<option value="nova">{t("design.theme.nova")}</option>
</select>
</label>
<label className="ts-options-field">
{t("design.custom.css")}
<textarea
className="ts-custom-theme-css"
spellCheck={false}
value={editing.css}
onChange={(e) => setEditing({ ...editing, css: e.target.value })}
placeholder={".ts-app {\n --accent: #ff6b6b;\n}"}
/>
</label>
<p className="ts-options-subtitle">{t("design.custom.hint")}</p>
<div className="ts-dialog-buttons-right">
<button onClick={() => setEditing(null)}>{t("connect.cancel")}</button>
<button onClick={handleSaveEditing} disabled={!editing.name.trim()}>
{t("design.custom.save")}
</button>
</div>
</>
);
}
return (
<>
<h3>{t("options.section.design")}</h3>
<p className="ts-options-subtitle">{t("design.subtitle")}</p>
<div className="ts-design-theme-grid">
{themes.map((th) => (
{builtins.map((th) => (
<button
key={th.id}
className={`ts-design-theme-card${th.id === designTheme ? " ts-design-theme-card-active" : ""}`}
onClick={() => onDesignThemeChange(th.id)}
className={`ts-design-theme-card${th.id === designSelection ? " ts-design-theme-card-active" : ""}`}
onClick={() => onSelectionChange(th.id)}
>
<span className={`ts-design-theme-swatch ts-design-theme-swatch-${th.id}`} />
<span className="ts-design-theme-card-name">{th.name}</span>
<span className="ts-design-theme-card-desc">{th.desc}</span>
</button>
))}
{customThemes.map((theme) => (
<div
key={theme.id}
className={`ts-design-theme-card ts-design-theme-card-custom${
designSelection === `custom:${theme.id}` ? " ts-design-theme-card-active" : ""
}`}
>
<button className="ts-design-theme-card-select" onClick={() => onSelectionChange(`custom:${theme.id}`)}>
<span className="ts-design-theme-swatch ts-design-theme-swatch-custom" />
<span className="ts-design-theme-card-name">{theme.name}</span>
<span className="ts-design-theme-card-desc">
{theme.baseTheme === "nova" ? t("design.theme.nova") : t("design.theme.standard")}
</span>
</button>
<div className="ts-design-theme-card-actions">
<button
onClick={() => setEditing({ id: theme.id, name: theme.name, baseTheme: theme.baseTheme, css: theme.css })}
title={t("design.custom.edit")}
>
✎
</button>
<button
onClick={() =>
setEditing({ id: null, name: t("design.custom.copyOf", { name: theme.name }), baseTheme: theme.baseTheme, css: theme.css })
}
title={t("design.custom.duplicate")}
>
⧉
</button>
<button onClick={() => handleExport(theme)} title={t("design.custom.export")}>
⭳
</button>
<button
onClick={() => {
if (window.confirm(t("design.custom.deleteConfirm"))) onDeleteCustomTheme(theme.id);
}}
title={t("design.custom.delete")}
>
🗑
</button>
</div>
</div>
))}
</div>
<div className="ts-design-custom-actions">
<button onClick={() => setEditing({ id: null, name: "", baseTheme: "standard", css: "" })}>
{t("design.custom.new")}
</button>
<button onClick={() => importInputRef.current?.click()}>{t("design.custom.import")}</button>
<input
ref={importInputRef}
type="file"
accept="application/json,.json"
style={{ display: "none" }}
onChange={handleImportSelected}
/>
</div>
</>
);
@@ -3871,15 +4065,21 @@ function OptionsDialog({
onSectionChange,
onClose,
audio,
designTheme,
onDesignThemeChange,
designSelection,
onDesignSelectionChange,
customThemes,
onSaveCustomTheme,
onDeleteCustomTheme,
}: {
section: string;
onSectionChange: (id: string) => void;
onClose: () => void;
audio: AudioSettings;
designTheme: DesignTheme;
onDesignThemeChange: (next: DesignTheme) => void;
designSelection: string;
onDesignSelectionChange: (next: string) => void;
customThemes: CustomTheme[];
onSaveCustomTheme: (theme: CustomTheme) => void;
onDeleteCustomTheme: (id: string) => void;
}) {
const t = useT();
const active = OPTIONS_SECTIONS.find((s) => s.id === section) ?? OPTIONS_SECTIONS[0];
@@ -3916,7 +4116,13 @@ function OptionsDialog({
) : active.id === "sounds" ? (
<SoundsPanel />
) : active.id === "design" ? (
<DesignPanel designTheme={designTheme} onDesignThemeChange={onDesignThemeChange} />
<DesignPanel
designSelection={designSelection}
onSelectionChange={onDesignSelectionChange}
customThemes={customThemes}
onSaveCustomTheme={onSaveCustomTheme}
onDeleteCustomTheme={onDeleteCustomTheme}
/>
) : active.id === "nachrichten" ? (
<NachrichtenPanel />
) : (
@@ -4284,11 +4490,55 @@ function AppInner() {
const [theme, setTheme] = useState<"light" | "dark">(() =>
window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light"
);
const [designTheme, setDesignTheme] = useState<DesignTheme>(() => loadDesignTheme());
const handleDesignThemeChange = (next: DesignTheme) => {
setDesignTheme(next);
localStorage.setItem(DESIGN_THEME_KEY, next);
const [customThemes, setCustomThemes] = useState<CustomTheme[]>(() => loadCustomThemes());
const [designSelection, setDesignSelection] = useState<string>(() => loadDesignSelection());
const activeCustomTheme = designSelection.startsWith("custom:")
? customThemes.find((th) => `custom:${th.id}` === designSelection) ?? null
: null;
// Structural base - everywhere else in the app that branches on "nova" behavior
// (splash, hamburger menu, ...) keeps working unchanged, whether that behavior
// came from picking Nova directly or from a custom theme built on top of it.
const designTheme: DesignTheme = activeCustomTheme ? activeCustomTheme.baseTheme : designSelection === "nova" ? "nova" : "standard";
const handleDesignSelectionChange = (next: string) => {
setDesignSelection(next);
localStorage.setItem(DESIGN_SELECTION_KEY, next);
const base = next.startsWith("custom:")
? customThemes.find((th) => `custom:${th.id}` === next)?.baseTheme ?? "standard"
: next === "nova"
? "nova"
: "standard";
localStorage.setItem(DESIGN_THEME_KEY, base);
};
const handleSaveCustomTheme = (nextTheme: CustomTheme) => {
setCustomThemes((prev) => {
const idx = prev.findIndex((th) => th.id === nextTheme.id);
const next = idx === -1 ? [...prev, nextTheme] : prev.map((th, i) => (i === idx ? nextTheme : th));
saveCustomThemes(next);
return next;
});
};
const handleDeleteCustomTheme = (id: string) => {
setCustomThemes((prev) => {
const next = prev.filter((th) => th.id !== id);
saveCustomThemes(next);
return next;
});
if (designSelection === `custom:${id}`) handleDesignSelectionChange("standard");
};
// Custom themes ship as free-form CSS - inject/replace it in a dedicated <style>
// tag while active, and drop it the moment no custom theme is selected.
useEffect(() => {
const css = activeCustomTheme?.css ?? "";
const existing = document.getElementById("ts-custom-theme-style") as HTMLStyleElement | null;
if (!css) {
existing?.remove();
return;
}
const el = existing ?? document.createElement("style");
el.id = "ts-custom-theme-style";
el.textContent = css;
if (!existing) document.head.appendChild(el);
}, [activeCustomTheme?.id, activeCustomTheme?.css]);
const [micOn, setMicOn] = useState(false);
const [talkers, setTalkers] = useState<Set<number>>(new Set());
const [selfActive, setSelfActive] = useState(false);
@@ -6096,8 +6346,9 @@ function AppInner() {
return (
<div
className={`ts-app ts-theme-${theme}${designTheme === "nova" ? " ts-design-nova" : ""}${
demoForceMobile ? " ts-force-mobile" : ""
}${novaSplash ? " ts-nova-splash" : ""}`}
activeCustomTheme ? " ts-design-custom" : ""
}${demoForceMobile ? " ts-force-mobile" : ""}${novaSplash ? " ts-nova-splash" : ""}`}
data-custom-theme={activeCustomTheme?.id}
>
{DEMO_MODE && (
<div className="ts-demo-banner">
@@ -7029,8 +7280,11 @@ function AppInner() {
section={optionsSection}
onSectionChange={setOptionsSection}
onClose={() => setOptionsDialogOpen(false)}
designTheme={designTheme}
onDesignThemeChange={handleDesignThemeChange}
designSelection={designSelection}
onDesignSelectionChange={handleDesignSelectionChange}
customThemes={customThemes}
onSaveCustomTheme={handleSaveCustomTheme}
onDeleteCustomTheme={handleDeleteCustomTheme}
audio={{
outputDevices,
outputDeviceId,
+32
View File
@@ -228,6 +228,22 @@ const translations: Record<Lang, Record<string, string>> = {
"design.theme.nova": "Nova",
"design.theme.nova.desc": "Moderneres, dunkles Design mit abgerundeten Elementen.",
"design.status.connected": "Verbunden",
"design.custom.new": "+ Neues Design",
"design.custom.import": "Design importieren",
"design.custom.edit": "Bearbeiten",
"design.custom.duplicate": "Duplizieren",
"design.custom.export": "Exportieren",
"design.custom.delete": "Löschen",
"design.custom.deleteConfirm": "Dieses Design wirklich löschen?",
"design.custom.newTitle": "Neues Design erstellen",
"design.custom.editTitle": "Design bearbeiten",
"design.custom.name": "Name",
"design.custom.base": "Basis-Layout",
"design.custom.css": "CSS",
"design.custom.hint": "Dieses CSS wird geladen, solange das Design aktiv ist. Nutze .ts-app { ... } und die vorhandenen CSS-Variablen (--accent, --bg, --text, --border, ...), um Farben, Radien und Abstände anzupassen - mit .ts-app .irgendeine-klasse { display: none } oder order lässt sich auch das Layout verändern.",
"design.custom.save": "Speichern",
"design.custom.copyOf": "Kopie von {name}",
"design.custom.importError": "Diese Datei konnte nicht als WebSpeak3-Design gelesen werden.",
"options.section.erweiterungen": "Erweiterungen",
"options.section.hotkeys": "Hotkeys",
"options.section.whispern": "Whispern",
@@ -734,6 +750,22 @@ const translations: Record<Lang, Record<string, string>> = {
"design.theme.nova": "Nova",
"design.theme.nova.desc": "A more modern, dark design with rounded elements.",
"design.status.connected": "Connected",
"design.custom.new": "+ New design",
"design.custom.import": "Import design",
"design.custom.edit": "Edit",
"design.custom.duplicate": "Duplicate",
"design.custom.export": "Export",
"design.custom.delete": "Delete",
"design.custom.deleteConfirm": "Really delete this design?",
"design.custom.newTitle": "Create a new design",
"design.custom.editTitle": "Edit design",
"design.custom.name": "Name",
"design.custom.base": "Base layout",
"design.custom.css": "CSS",
"design.custom.hint": "This CSS loads while the design is active. Use .ts-app { ... } and the existing CSS variables (--accent, --bg, --text, --border, ...) to adjust colors, radii and spacing - with .ts-app .some-class { display: none } or order you can even change the layout.",
"design.custom.save": "Save",
"design.custom.copyOf": "Copy of {name}",
"design.custom.importError": "This file couldn't be read as a WebSpeak3 design.",
"options.section.erweiterungen": "Addons",
"options.section.hotkeys": "Hotkeys",
"options.section.whispern": "Whisper",