feat(web): four themes with a contrast test

- Sonda, Tokyo Night, Gruvbox dark and Gruvbox light as CSS variables with shadcn token names
- contrast test: text 4.5:1, accent and form outlines 3:1 in every theme
- Gruvbox dark muted text lightened to pass
- theme choice is stored in localStorage and survives it being blocked
This commit is contained in:
tgiachi 2026-11-12 11:17:41 +01:00
commit abeef4e30f
7 changed files with 273 additions and 81 deletions

View file

@ -8,26 +8,15 @@
@import "@fontsource/figtree/800.css"; @import "@fontsource/figtree/800.css";
@import "@fontsource/fraunces/600.css"; @import "@fontsource/fraunces/600.css";
@import "@fontsource/fraunces/700.css"; @import "@fontsource/fraunces/700.css";
@import "./theme/themes.css";
@custom-variant dark (&:is(.dark *)); /* The dark variant follows the theme, not the OS setting. */
@custom-variant dark (&:is([data-theme="sonda"] *, [data-theme="tokyo-night"] *, [data-theme="gruvbox-dark"] *));
@theme inline { @theme inline {
--font-heading: var(--font-sans); --font-heading: var(--font-sans);
--font-sans: 'Figtree', system-ui, sans-serif; --font-sans: 'Figtree', system-ui, sans-serif;
--font-display: 'Fraunces', Georgia, serif; --font-display: 'Fraunces', Georgia, serif;
--color-sidebar-ring: var(--sidebar-ring);
--color-sidebar-border: var(--sidebar-border);
--color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
--color-sidebar-accent: var(--sidebar-accent);
--color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
--color-sidebar-primary: var(--sidebar-primary);
--color-sidebar-foreground: var(--sidebar-foreground);
--color-sidebar: var(--sidebar);
--color-chart-5: var(--chart-5);
--color-chart-4: var(--chart-4);
--color-chart-3: var(--chart-3);
--color-chart-2: var(--chart-2);
--color-chart-1: var(--chart-1);
--color-ring: var(--ring); --color-ring: var(--ring);
--color-input: var(--input); --color-input: var(--input);
--color-border: var(--border); --color-border: var(--border);
@ -56,72 +45,7 @@
} }
:root { :root {
--background: oklch(1 0 0);
--foreground: oklch(0.145 0 0);
--card: oklch(1 0 0);
--card-foreground: oklch(0.145 0 0);
--popover: oklch(1 0 0);
--popover-foreground: oklch(0.145 0 0);
--primary: oklch(0.205 0 0);
--primary-foreground: oklch(0.985 0 0);
--secondary: oklch(0.97 0 0);
--secondary-foreground: oklch(0.205 0 0);
--muted: oklch(0.97 0 0);
--muted-foreground: oklch(0.556 0 0);
--accent: oklch(0.97 0 0);
--accent-foreground: oklch(0.205 0 0);
--destructive: oklch(0.577 0.245 27.325);
--border: oklch(0.922 0 0);
--input: oklch(0.922 0 0);
--ring: oklch(0.708 0 0);
--chart-1: oklch(0.87 0 0);
--chart-2: oklch(0.556 0 0);
--chart-3: oklch(0.439 0 0);
--chart-4: oklch(0.371 0 0);
--chart-5: oklch(0.269 0 0);
--radius: 0.625rem; --radius: 0.625rem;
--sidebar: oklch(0.985 0 0);
--sidebar-foreground: oklch(0.145 0 0);
--sidebar-primary: oklch(0.205 0 0);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.97 0 0);
--sidebar-accent-foreground: oklch(0.205 0 0);
--sidebar-border: oklch(0.922 0 0);
--sidebar-ring: oklch(0.708 0 0);
}
.dark {
--background: oklch(0.145 0 0);
--foreground: oklch(0.985 0 0);
--card: oklch(0.205 0 0);
--card-foreground: oklch(0.985 0 0);
--popover: oklch(0.205 0 0);
--popover-foreground: oklch(0.985 0 0);
--primary: oklch(0.922 0 0);
--primary-foreground: oklch(0.205 0 0);
--secondary: oklch(0.269 0 0);
--secondary-foreground: oklch(0.985 0 0);
--muted: oklch(0.269 0 0);
--muted-foreground: oklch(0.708 0 0);
--accent: oklch(0.269 0 0);
--accent-foreground: oklch(0.985 0 0);
--destructive: oklch(0.704 0.191 22.216);
--border: oklch(1 0 0 / 10%);
--input: oklch(1 0 0 / 15%);
--ring: oklch(0.556 0 0);
--chart-1: oklch(0.87 0 0);
--chart-2: oklch(0.556 0 0);
--chart-3: oklch(0.439 0 0);
--chart-4: oklch(0.371 0 0);
--chart-5: oklch(0.269 0 0);
--sidebar: oklch(0.205 0 0);
--sidebar-foreground: oklch(0.985 0 0);
--sidebar-primary: oklch(0.488 0.243 264.376);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.269 0 0);
--sidebar-accent-foreground: oklch(0.985 0 0);
--sidebar-border: oklch(1 0 0 / 10%);
--sidebar-ring: oklch(0.556 0 0);
} }
@layer base { @layer base {
@ -134,4 +58,4 @@
html { html {
@apply font-sans; @apply font-sans;
} }
} }

View file

@ -2,6 +2,9 @@ import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client' import { createRoot } from 'react-dom/client'
import './index.css' import './index.css'
import App from './App.tsx' import App from './App.tsx'
import { initTheme } from './theme/theme'
initTheme()
createRoot(document.getElementById('root')!).render( createRoot(document.getElementById('root')!).render(
<StrictMode> <StrictMode>

View file

@ -0,0 +1,55 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { describe, expect, test } from 'vitest'
import { THEMES } from './theme'
// Vitest runs from web/. (import.meta.url is not a file URL under jsdom, and ?raw gives empty CSS.)
const css = readFileSync(resolve(process.cwd(), 'src/theme/themes.css'), 'utf8')
function parseTheme(id: string): Record<string, string> {
const block = new RegExp(`\\[data-theme="${id}"\\]\\s*\\{([^}]*)\\}`).exec(css)
if (!block) throw new Error(`themes.css has no block for ${id}`)
const tokens: Record<string, string> = {}
for (const m of block[1].matchAll(/--([a-z-]+):\s*(#[0-9a-fA-F]{6})\s*;/g)) tokens[m[1]] = m[2]
return tokens
}
function luminance(hex: string): number {
const channel = (i: number) => {
const c = parseInt(hex.slice(1 + i * 2, 3 + i * 2), 16) / 255
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
}
return 0.2126 * channel(0) + 0.7152 * channel(1) + 0.0722 * channel(2)
}
function ratio(a: string, b: string): number {
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x)
return (hi + 0.05) / (lo + 0.05)
}
// [foreground token, background token, minimum ratio]
const pairs: [string, string, number][] = [
['foreground', 'background', 4.5],
['foreground', 'card', 4.5],
['muted-foreground', 'background', 4.5],
['muted-foreground', 'card', 4.5],
['primary-foreground', 'primary', 4.5],
['primary', 'background', 3],
['input', 'background', 3],
['input', 'card', 3],
['ring', 'background', 3],
]
describe.each(THEMES.map((t) => t.id))('theme %s', (id) => {
const tokens = parseTheme(id)
test.each(pairs)('%s on %s is at least %d:1', (fg, bg, min) => {
expect(tokens[fg], `--${fg} missing`).toBeDefined()
expect(tokens[bg], `--${bg} missing`).toBeDefined()
expect(ratio(tokens[fg], tokens[bg])).toBeGreaterThanOrEqual(min)
})
test('defines a divider colour', () => {
expect(tokens['border']).toBeDefined()
})
})

View file

@ -0,0 +1,55 @@
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
import { getTheme, initTheme, setTheme, THEMES } from './theme'
beforeEach(() => {
delete document.documentElement.dataset.theme
})
afterEach(() => {
vi.restoreAllMocks()
})
test('there are four themes and sonda comes first', () => {
expect(THEMES.map((t) => t.id)).toEqual(['sonda', 'tokyo-night', 'gruvbox-dark', 'gruvbox-light'])
})
test('initTheme with nothing stored uses sonda', () => {
initTheme()
expect(document.documentElement.dataset.theme).toBe('sonda')
expect(getTheme()).toBe('sonda')
})
test('initTheme uses the stored theme', () => {
localStorage.setItem('sonda.theme', 'gruvbox-light')
initTheme()
expect(document.documentElement.dataset.theme).toBe('gruvbox-light')
})
test('initTheme falls back to sonda for a value it does not know', () => {
localStorage.setItem('sonda.theme', 'garbage')
initTheme()
expect(document.documentElement.dataset.theme).toBe('sonda')
})
test('setTheme stores the choice and sets data-theme', () => {
setTheme('tokyo-night')
expect(localStorage.getItem('sonda.theme')).toBe('tokyo-night')
expect(document.documentElement.dataset.theme).toBe('tokyo-night')
expect(getTheme()).toBe('tokyo-night')
})
test('setTheme still works when storage throws', () => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error('blocked')
})
expect(() => setTheme('gruvbox-dark')).not.toThrow()
expect(document.documentElement.dataset.theme).toBe('gruvbox-dark')
})
test('initTheme still works when reading storage throws', () => {
vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
throw new Error('blocked')
})
expect(() => initTheme()).not.toThrow()
expect(document.documentElement.dataset.theme).toBe('sonda')
})

62
web/src/theme/theme.ts Normal file
View file

@ -0,0 +1,62 @@
import { useSyncExternalStore } from 'react'
export type ThemeId = 'sonda' | 'tokyo-night' | 'gruvbox-dark' | 'gruvbox-light'
export const THEMES: readonly { id: ThemeId; labelKey: string }[] = [
{ id: 'sonda', labelKey: 'theme.sonda' },
{ id: 'tokyo-night', labelKey: 'theme.tokyo-night' },
{ id: 'gruvbox-dark', labelKey: 'theme.gruvbox-dark' },
{ id: 'gruvbox-light', labelKey: 'theme.gruvbox-light' },
]
const KEY = 'sonda.theme'
const DEFAULT: ThemeId = 'sonda'
let current: ThemeId = DEFAULT
const listeners = new Set<() => void>()
function isThemeId(value: unknown): value is ThemeId {
return THEMES.some((t) => t.id === value)
}
function readStored(): string | null {
try {
return localStorage.getItem(KEY)
} catch {
return null
}
}
function apply(id: ThemeId) {
current = id
document.documentElement.dataset.theme = id
listeners.forEach((fn) => fn())
}
export function getTheme(): ThemeId {
return current
}
export function setTheme(id: ThemeId): void {
apply(id)
try {
localStorage.setItem(KEY, id)
} catch {
// The choice still applies for this page view.
}
}
export function initTheme(): void {
const stored = readStored()
apply(isThemeId(stored) ? stored : DEFAULT)
}
function subscribe(fn: () => void): () => void {
listeners.add(fn)
return () => listeners.delete(fn)
}
export function useTheme(): { theme: ThemeId; setTheme: (id: ThemeId) => void } {
const theme = useSyncExternalStore(subscribe, getTheme)
return { theme, setTheme }
}

93
web/src/theme/themes.css Normal file
View file

@ -0,0 +1,93 @@
/*
* The four themes. Every colour is a plain hex value so that contrast.test.ts can check it.
* Token names are the ones shadcn/ui uses.
* --border is a quiet divider; --input is the outline of a form control and has to be seen (3:1).
*/
[data-theme="sonda"] {
color-scheme: dark;
--background: #0a1218;
--foreground: #e8f1f2;
--card: #111c24;
--card-foreground: #e8f1f2;
--popover: #111c24;
--popover-foreground: #e8f1f2;
--primary: #3dd6b0;
--primary-foreground: #04231c;
--secondary: #182630;
--secondary-foreground: #e8f1f2;
--muted: #182630;
--muted-foreground: #93a7ae;
--accent: #182630;
--accent-foreground: #e8f1f2;
--destructive: #ff7a8a;
--border: #1e2f3a;
--input: #5e7a87;
--ring: #3dd6b0;
}
[data-theme="tokyo-night"] {
color-scheme: dark;
--background: #1a1b26;
--foreground: #c0caf5;
--card: #24283b;
--card-foreground: #c0caf5;
--popover: #24283b;
--popover-foreground: #c0caf5;
--primary: #7aa2f7;
--primary-foreground: #1a1b26;
--secondary: #2f3549;
--secondary-foreground: #c0caf5;
--muted: #2f3549;
--muted-foreground: #9aa5ce;
--accent: #2f3549;
--accent-foreground: #c0caf5;
--destructive: #f7768e;
--border: #3b4261;
--input: #7982aa;
--ring: #7aa2f7;
}
[data-theme="gruvbox-dark"] {
color-scheme: dark;
--background: #282828;
--foreground: #ebdbb2;
--card: #3c3836;
--card-foreground: #ebdbb2;
--popover: #3c3836;
--popover-foreground: #ebdbb2;
--primary: #fabd2f;
--primary-foreground: #282828;
--secondary: #504945;
--secondary-foreground: #ebdbb2;
--muted: #504945;
--muted-foreground: #bdae93;
--accent: #504945;
--accent-foreground: #ebdbb2;
--destructive: #fb4934;
--border: #504945;
--input: #928374;
--ring: #fabd2f;
}
[data-theme="gruvbox-light"] {
color-scheme: light;
--background: #fbf1c7;
--foreground: #3c3836;
--card: #ebdbb2;
--card-foreground: #3c3836;
--popover: #ebdbb2;
--popover-foreground: #3c3836;
--primary: #af3a03;
--primary-foreground: #fbf1c7;
--secondary: #d5c4a1;
--secondary-foreground: #3c3836;
--muted: #d5c4a1;
--muted-foreground: #665c54;
--accent: #d5c4a1;
--accent-foreground: #3c3836;
--destructive: #9d0006;
--border: #d5c4a1;
--input: #7c6f64;
--ring: #af3a03;
}

View file

@ -4,7 +4,7 @@
"target": "es2023", "target": "es2023",
"lib": ["ES2023", "DOM"], "lib": ["ES2023", "DOM"],
"module": "esnext", "module": "esnext",
"types": ["vite/client"], "types": ["vite/client", "node"],
"allowArbitraryExtensions": true, "allowArbitraryExtensions": true,
"paths": { "@/*": ["./src/*"] }, "paths": { "@/*": ["./src/*"] },
"skipLibCheck": true, "skipLibCheck": true,