From 9c7fb2926c093f1afc49cef6f0d529f64fe1319b Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 14 Oct 2026 06:42:32 +0000 Subject: [PATCH] Add a sun/moon button in the app bar that switches between light and dark theme in one tap Co-Authored-By: Claude Sonnet 5.5 --- src/App.tsx | 15 ++++-- src/ThemeToggle.test.tsx | 109 +++++++++++++++++++++++++++++++++++++++ src/icons.tsx | 2 + src/index.css | 6 ++- 4 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 src/ThemeToggle.test.tsx diff --git a/src/App.tsx b/src/App.tsx index d2b21fb..3335c55 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -23,7 +23,7 @@ import { Welcome, type StartLevel } from './pages/Welcome' import { lessonById, LESSONS, UNITS } from './path/course' import { getProgress, learnedItems } from './path/progress' import { FURIGANA, FuriganaContext, ROMAJI, type Furigana, type Romaji } from './path/ui/furigana' -import { readBool, readStr, writeBool, writeStr } from './settings' +import { applyTheme, readBool, readStr, THEMES, writeBool, writeStr, type Theme } from './settings' import { maybeSignedIn, recovery, sync } from './sync' type Tab = 'today' | 'path' | 'review' | 'lookup' | 'more' @@ -67,6 +67,8 @@ export default function App() { const [known, setKnown] = useState>(new Set()) // kanji the learner has had a kanji lesson for const [romaji, setRomaji] = useState(() => readStr('nihongo.romaji', ROMAJI, 'auto')) const [kanaDone, setKanaDone] = useState(false) // every kana lesson done (or tested out): romaji goes, by default + const [theme, setTheme] = useState(() => readStr('nihongo.theme', THEMES, 'system')) + const osDark = useMedia('(prefers-color-scheme: dark)') const phone = !useMedia('(min-width: 48rem)') // bottom tabs, and a top bar for the logo and account const sidebar = useMedia('(min-width: 64rem)') // navigation down the left, More's pages listed in it const [gen, setGen] = useState(0) // bumped when a background sync changes local data: screens remount and reload @@ -140,6 +142,11 @@ export default function App() { const unit = view === 'test' ? UNITS.find((u) => u.id === testUnit) : undefined const focused = !!lesson || !!unit || view === 'study' || view === 'drill' || view === 'reading' || view === 'kana' // lessons and reviews are full-screen: no tabs to wander off to + const dark = theme === 'system' ? osDark : theme === 'dark' // what is showing now + const pickTheme = (t: Theme) => { setTheme(t); writeStr('nihongo.theme', t); applyTheme(t) } + const themeBtn = ( + + ) const account = maybeSignedIn() ? : @@ -160,7 +167,7 @@ export default function App() { {!focused && phone && (
- {account} + {themeBtn}{account}
)}
@@ -191,7 +198,7 @@ export default function App() { ) : view === 'stats' ? ( ) : view === 'settings' ? ( - { setAutoplay(v); writeBool('nihongo.autoplay', v) }} + { setAutoplay(v); writeBool('nihongo.autoplay', v) }} furigana={furigana} onFurigana={(f) => { setFurigana(f); writeStr('nihongo.furigana', f) }} romaji={romaji} onRomaji={(r) => { setRomaji(r); writeStr('nihongo.romaji', r) }} onSynced={rerender} /> ) : view === 'account' ? ( @@ -227,7 +234,7 @@ export default function App() { ))} )} - {!phone && account} + {!phone && {themeBtn}{account}} )} diff --git a/src/ThemeToggle.test.tsx b/src/ThemeToggle.test.tsx new file mode 100644 index 0000000..0e48064 --- /dev/null +++ b/src/ThemeToggle.test.tsx @@ -0,0 +1,109 @@ +// @vitest-environment jsdom +import 'fake-indexeddb/auto' +import { cleanup, render, screen } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import { afterEach, beforeEach, expect, it } from 'vitest' +import App from './App' + +const media = (o: { dark: boolean; wide: boolean }) => { + window.matchMedia = ((q: string) => ({ + matches: q.includes('prefers-color-scheme') ? o.dark : q.includes('min-width') ? o.wide : false, + media: q, addEventListener() {}, removeEventListener() {}, addListener() {}, removeListener() {}, onchange: null, dispatchEvent: () => false, + })) as unknown as typeof matchMedia +} +beforeEach(() => { localStorage.clear(); localStorage.setItem('nihongo.onboarded', '1'); document.documentElement.removeAttribute('data-theme'); media({ dark: false, wide: false }) }) +afterEach(cleanup) + +const toDark = () => screen.queryByRole('button', { name: 'Switch to dark theme' }) +const toLight = () => screen.queryByRole('button', { name: 'Switch to light theme' }) + +it('phone: system with OS light -> button says dark; tap saves dark, applies, and flips label', async () => { + const user = userEvent.setup() + render() + const b = await screen.findByRole('button', { name: 'Switch to dark theme' }) + expect(b.tagName).toBe('BUTTON') + expect(b.closest('header.appbar')).toBeTruthy() + await user.click(b) + expect(localStorage.getItem('nihongo.theme')).toBe('dark') + expect(document.documentElement.dataset.theme).toBe('dark') + expect(toLight()).toBeTruthy() + expect(toDark()).toBeNull() + await user.click(toLight()!) + expect(localStorage.getItem('nihongo.theme')).toBe('light') + expect(document.documentElement.dataset.theme).toBe('light') +}) + +it('system with OS dark switches to Light', async () => { + media({ dark: true, wide: false }) + const user = userEvent.setup() + render() + await user.click(await screen.findByRole('button', { name: 'Switch to light theme' })) + expect(localStorage.getItem('nihongo.theme')).toBe('light') +}) + +it('explicit saved theme wins over the OS', async () => { + localStorage.setItem('nihongo.theme', 'light') + media({ dark: true, wide: false }) + render() + expect(await screen.findByRole('button', { name: 'Switch to dark theme' })).toBeTruthy() +}) + +it('invalid saved value is treated as system', async () => { + localStorage.setItem('nihongo.theme', 'purple') + media({ dark: true, wide: false }) + render() + expect(await screen.findByRole('button', { name: 'Switch to light theme' })).toBeTruthy() +}) + +it('survives a reload', async () => { + const user = userEvent.setup() + const { unmount } = render() + await user.click(await screen.findByRole('button', { name: 'Switch to dark theme' })) + unmount() + render() + expect(await screen.findByRole('button', { name: 'Switch to light theme' })).toBeTruthy() +}) + +it('wide screens: the button is in the navigation', async () => { + media({ dark: false, wide: true }) + const user = userEvent.setup() + render() + const b = await screen.findByRole('button', { name: 'Switch to dark theme' }) + expect(b.closest('nav')).toBeTruthy() + expect(document.querySelector('header.appbar')).toBeNull() + await user.click(b) + expect(localStorage.getItem('nihongo.theme')).toBe('dark') +}) + +it('is keyboard reachable and activates with Enter', async () => { + const user = userEvent.setup() + render() + const b = await screen.findByRole('button', { name: 'Switch to dark theme' }) + b.focus() + expect(document.activeElement).toBe(b) + await user.keyboard('{Enter}') + expect(localStorage.getItem('nihongo.theme')).toBe('dark') +}) + +it('Settings theme select agrees with the toggle and can return to System', async () => { + const user = userEvent.setup() + render() + await user.click(await screen.findByRole('button', { name: 'Switch to dark theme' })) + await user.click(screen.getByRole('button', { name: /^More$/ })) + await user.click(screen.getByRole('button', { name: /^Settings/ })) + const sel = (await screen.findByLabelText(/^Theme/)) as HTMLSelectElement + expect(sel.value).toBe('dark') + expect(Array.from(sel.options).map((o) => o.value)).toEqual(['system', 'light', 'dark']) + await user.selectOptions(sel, 'system') + expect(localStorage.getItem('nihongo.theme')).toBe('system') + expect(document.documentElement.hasAttribute('data-theme')).toBe(false) +}) + +it('hidden during a full-screen lesson', async () => { + const user = userEvent.setup() + render() + await user.click(await screen.findByRole('button', { name: /^Next lesson:/ })) + await screen.findByRole('button', { name: 'Got it' }) + expect(toDark()).toBeNull() + expect(toLight()).toBeNull() +}) diff --git a/src/icons.tsx b/src/icons.tsx index 437ee5d..b0aba88 100644 --- a/src/icons.tsx +++ b/src/icons.tsx @@ -23,6 +23,8 @@ const P = { cards: 'M7 3h11a2 2 0 0 1 2 2v13M4 7h11a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2z', turtle: 'M2.5 15.5a7.5 7 0 0 1 15 0zM6.5 15.5l2-4h4l2 4M19.5 14.5a2 2 0 1 0 0-4 2 2 0 0 0 0 4M17.5 13.5h.5M5.5 15.5v3M14.5 15.5v3', user: 'M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM4.5 20.5a7.5 7.5 0 0 1 15 0', + sun: 'M12 16a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4', + moon: 'M20 14.5A8.5 8.5 0 0 1 9.5 4 8.5 8.5 0 1 0 20 14.5z', shuffle: 'M16 3h5v5M4 20 21 3M21 16v5h-5M15 15l6 6M4 4l5 5', } as const export type IconName = keyof typeof P diff --git a/src/index.css b/src/index.css index bc14efb..72ab23f 100644 --- a/src/index.css +++ b/src/index.css @@ -103,6 +103,8 @@ header.appbar { position: sticky; top: 0; z-index: var(--z-nav); display: flex; background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); } header.appbar + main { padding-top: .5rem; } button.account-btn:not(.icon-btn) { white-space: nowrap; min-height: 2.75rem; padding: .35rem 1rem; font-size: .92rem; font-weight: 650; border-radius: 999px; } +span.bar-actions { display: inline-flex; align-items: center; gap: .25rem; flex: none; } +@media (max-width: 23rem) { header.appbar .brand small { display: none; } } /* 320px: room for the Sign in pill */ button.account-btn.icon-btn { color: var(--fg); } button.account-btn[aria-current="page"] { border-color: var(--accent); color: var(--accent); } @@ -117,7 +119,7 @@ nav.tabs button[aria-current="page"] .icon { background: var(--accent-soft); bor nav.tabs { inset: 0 0 auto 0; display: flex; align-items: center; gap: .25rem; padding: .45rem 1.25rem; border-top: 0; border-bottom: 1px solid var(--line); } nav.tabs .brand { display: flex; margin-right: auto; } nav.tabs .brand small { display: none; } /* room for five tabs on a tablet */ - nav.tabs .account-btn { margin-left: auto; } + nav.tabs .bar-actions { margin-left: auto; } nav.tabs button:not(.account-btn) { flex-direction: row; justify-content: center; gap: .45rem; font-size: .95rem; min-height: 2.75rem; padding: .5rem .8rem; } nav.tabs button:not(.account-btn):hover { background: color-mix(in srgb, var(--fg) 5%, transparent); color: var(--fg); } nav.tabs button[aria-current="page"], nav.tabs button[aria-current="page"]:hover { background: var(--accent-soft); color: color-mix(in srgb, var(--accent) 75%, var(--fg)); } /* plain accent on its soft tint is under 4.5:1 */ @@ -135,7 +137,7 @@ nav.tabs button[aria-current="page"] .icon { background: var(--accent-soft); bor .nav-more { display: grid; gap: .15rem; margin-top: .9rem; padding-top: .9rem; border-top: 1px solid var(--line); } .nav-more button:not(.account-btn) { font-size: .92rem; min-height: 2.5rem; padding-left: 1rem; } nav.tabs { backdrop-filter: none; -webkit-backdrop-filter: none; } /* a filter would trap the fixed account button inside the sidebar */ - nav.tabs .account-btn { position: fixed; top: 1rem; right: 1.5rem; margin: 0; } /* top right, above the content */ + nav.tabs .bar-actions { position: fixed; top: 1rem; right: 1.5rem; margin: 0; } /* top right, above the content */ html { scroll-padding-top: 1.5rem; } main.with-nav { max-width: 46rem; padding-top: 4.5rem; margin-left: max(calc(var(--sidebar) + 1rem), calc(var(--sidebar) + (100vw - var(--sidebar) - 46rem) / 2)); } }