feat: add light/dark/auto theme

This commit is contained in:
Steffen Becker committed 2026-05-25 20:50:08 +02:00
1 parent c51bac2e7f
commit c9196d252d
6 files changed
+191 -10

No files matched your search

+1 -1
View File
@@ -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
+136 -4
View File
@@ -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 @@
<main>
<header>
<Toolbar {editor} {tick} />
<button class="export-btn" onclick={handleExport} disabled={!editor}>
Download .odt
</button>
<div class="header-actions">
<div class="theme-wrap" use:clickOutside>
<button
class="theme-btn"
onclick={() => (themeOpen = !themeOpen)}
title="Appearance"
aria-haspopup="true"
aria-expanded={themeOpen}
>
{#if themeMode === 'light'}
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<circle cx="8" cy="8" r="2.8" stroke="currentColor" stroke-width="1.5"/>
<line x1="8" y1="1" x2="8" y2="3" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="8" y1="13" x2="8" y2="15" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="1" y1="8" x2="3" y2="8" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="13" y1="8" x2="15" y2="8" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="2.93" y1="2.93" x2="4.34" y2="4.34" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="11.66" y1="11.66" x2="13.07" y2="13.07" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="2.93" y1="13.07" x2="4.34" y2="11.66" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="11.66" y1="4.34" x2="13.07" y2="2.93" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
</svg>
{:else if themeMode === 'dark'}
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<path d="M13.5 10A6 6 0 0 1 6 2.5a6 6 0 1 0 7.5 7.5z" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/>
</svg>
{:else}
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<rect x="1" y="2" width="14" height="9" rx="1.5" stroke="currentColor" stroke-width="1.5"/>
<line x1="5.5" y1="14" x2="10.5" y2="14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
<line x1="8" y1="11" x2="8" y2="14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
</svg>
{/if}
</button>
{#if themeOpen}
<div class="theme-dropdown" role="menu">
{#each ([['light', 'Light'], ['dark', 'Dark'], ['auto', 'Auto']] as const) as [m, label]}
<button
class="theme-option"
class:selected={themeMode === m}
onclick={() => selectTheme(m)}
role="menuitem"
>{label}</button>
{/each}
</div>
{/if}
</div>
<button class="export-btn" onclick={handleExport} disabled={!editor}>
Download .odt
</button>
</div>
</header>
<EditorComponent bind:editor bind:tick />
</main>
@@ -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;
+1 -1
View File
@@ -165,7 +165,7 @@
}
button:hover:not(:disabled) {
background: #cbd5e1;
background: var(--color-btn-hover);
}
button.active {
+24
View File
@@ -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));
}
+9
View File
@@ -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')! });
+20 -4
View File
@@ -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 {