From c9196d252d7657b92a849c73220e7814f220bc0d Mon Sep 17 00:00:00 2001 From: Steffen Becker Date: Mon, 25 May 2026 13:40:41 +0200 Subject: [PATCH] feat: add light/dark/auto theme --- CHANGELOG.md | 2 +- src/App.svelte | 140 +++++++++++++++++++++++++++++++++- src/lib/editor/Toolbar.svelte | 2 +- src/lib/storage/theme.ts | 24 ++++++ src/main.ts | 9 +++ src/styles/global.css | 24 +++++- 6 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 src/lib/storage/theme.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cdfba3..33843ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,9 +22,9 @@ - Filename derived from first H1/H2/H3 heading, fallback `document.odt` - Favicon (hand-coded SVG, no external assets) - Lists (ordered / unordered) +- Light / Dark / Auto appearance mode (settings menu in toolbar) ### Not yet implemented -- light/dark and auto appearance mode in a settings menu - ODT import / open existing file - font size - Images, tables diff --git a/src/App.svelte b/src/App.svelte index 3ea1676..e817fa6 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -3,10 +3,29 @@ import EditorComponent from './lib/editor/Editor.svelte'; import Toolbar from './lib/editor/Toolbar.svelte'; import { exportToOdt } from './lib/export/odt'; + import { loadTheme, saveTheme, applyTheme, type ThemeMode } from './lib/storage/theme'; let editor: Editor | null = $state(null); let tick: number = $state(0); + let themeMode: ThemeMode = $state(loadTheme()); + let themeOpen = $state(false); + + function selectTheme(m: ThemeMode) { + themeMode = m; + saveTheme(m); + applyTheme(m); + themeOpen = false; + } + + function clickOutside(node: HTMLElement) { + function handler(e: MouseEvent) { + if (!node.contains(e.target as Node)) themeOpen = false; + } + window.addEventListener('mousedown', handler); + return { destroy() { window.removeEventListener('mousedown', handler); } }; + } + async function handleExport() { if (editor) await exportToOdt(editor); } @@ -15,9 +34,56 @@
- +
+
+ + {#if themeOpen} + + {/if} +
+ +
@@ -37,9 +103,75 @@ box-shadow: var(--shadow); } - .export-btn { + .header-actions { + display: flex; + align-items: center; + gap: 0.5rem; margin-left: auto; margin-right: 1rem; + } + + .theme-wrap { + position: relative; + } + + .theme-btn { + display: inline-flex; + align-items: center; + justify-content: center; + width: 2rem; + height: 2rem; + border: none; + border-radius: var(--radius); + background: transparent; + color: var(--color-text); + cursor: pointer; + transition: background 0.15s; + } + + .theme-btn:hover { + background: var(--color-btn-hover); + } + + .theme-dropdown { + position: absolute; + top: calc(100% + 0.4rem); + right: 0; + background: var(--color-surface); + border: 1px solid var(--color-border); + border-radius: var(--radius); + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12); + min-width: 100px; + z-index: 100; + overflow: hidden; + } + + .theme-option { + display: block; + width: 100%; + padding: 0.5rem 0.75rem; + border: none; + background: transparent; + color: var(--color-text); + font-size: 0.85rem; + font-family: var(--font-sans); + cursor: pointer; + text-align: left; + transition: background 0.1s; + } + + .theme-option:hover { + background: var(--color-btn-hover); + } + + .theme-option.selected { + color: var(--color-primary); + font-weight: 600; + } + + .export-btn { + margin-left: 0; + margin-right: 0; padding: 0.4rem 1rem; background: var(--color-primary); color: white; diff --git a/src/lib/editor/Toolbar.svelte b/src/lib/editor/Toolbar.svelte index 9a3b6a4..1ea538f 100644 --- a/src/lib/editor/Toolbar.svelte +++ b/src/lib/editor/Toolbar.svelte @@ -165,7 +165,7 @@ } button:hover:not(:disabled) { - background: #cbd5e1; + background: var(--color-btn-hover); } button.active { diff --git a/src/lib/storage/theme.ts b/src/lib/storage/theme.ts new file mode 100644 index 0000000..5658c02 --- /dev/null +++ b/src/lib/storage/theme.ts @@ -0,0 +1,24 @@ +export type ThemeMode = 'light' | 'dark' | 'auto'; + +const STORAGE_KEY = 'odf-editor-theme'; + +export function loadTheme(): ThemeMode { + const saved = localStorage.getItem(STORAGE_KEY); + if (saved === 'light' || saved === 'dark' || saved === 'auto') return saved; + return 'auto'; +} + +export function saveTheme(mode: ThemeMode): void { + localStorage.setItem(STORAGE_KEY, mode); +} + +function resolveMode(mode: ThemeMode): 'light' | 'dark' { + if (mode === 'auto') { + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; + } + return mode; +} + +export function applyTheme(mode: ThemeMode): void { + document.documentElement.setAttribute('data-theme', resolveMode(mode)); +} diff --git a/src/main.ts b/src/main.ts index 021fd2c..059206a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,15 @@ import { mount } from 'svelte'; import App from './App.svelte'; import './styles/global.css'; +import { loadTheme, applyTheme } from './lib/storage/theme'; + +// Apply saved theme before mount to prevent flash of wrong theme +applyTheme(loadTheme()); + +// Keep 'auto' in sync when system theme changes +window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { + if (loadTheme() === 'auto') applyTheme('auto'); +}); const app = mount(App, { target: document.getElementById('app')! }); diff --git a/src/styles/global.css b/src/styles/global.css index e98e365..7acb04c 100644 --- a/src/styles/global.css +++ b/src/styles/global.css @@ -7,6 +7,13 @@ } :root { + --radius: 6px; + --shadow: 0 1px 3px rgba(0, 0, 0, 0.08); + --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; + --font-serif: Georgia, 'Times New Roman', serif; + } + + [data-theme="light"] { --color-bg: #f1f5f9; --color-surface: #ffffff; --color-border: #e2e8f0; @@ -15,10 +22,19 @@ --color-primary: #3b82f6; --color-primary-hover: #2563eb; --color-toolbar-bg: #ffffff; - --radius: 6px; - --shadow: 0 1px 3px rgba(0, 0, 0, 0.08); - --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; - --font-serif: Georgia, 'Times New Roman', serif; + --color-btn-hover: #cbd5e1; + } + + [data-theme="dark"] { + --color-bg: #0d1117; + --color-surface: #161b22; + --color-border: #30363d; + --color-text: #e6edf3; + --color-text-muted: #7d8590; + --color-primary: #2f81f7; + --color-primary-hover: #388bfd; + --color-toolbar-bg: #161b22; + --color-btn-hover: #3b4350; } html, body {