Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -67,6 +67,8 @@ export default function App() {
const [known, setKnown] = useState<ReadonlySet<string>>(new Set()) // kanji the learner has had a kanji lesson for
const [romaji, setRomaji] = useState<Romaji>(() => 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<Theme>(() => 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
Expand Down Expand Up @@ -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 = (
<button className="icon-btn account-btn" aria-label={dark ? 'Switch to light theme' : 'Switch to dark theme'} onClick={() => pickTheme(dark ? 'light' : 'dark')}><Icon name={dark ? 'sun' : 'moon'} /></button>
)
const account = maybeSignedIn()
? <button className="icon-btn account-btn" aria-label="Account and sync" aria-current={view === 'account' ? 'page' : undefined} onClick={() => go('account')}><Icon name="user" /></button>
: <button className="account-btn" aria-current={view === 'account' ? 'page' : undefined} onClick={() => go('account')}>Sign in</button>
Expand All @@ -160,7 +167,7 @@ export default function App() {
{!focused && phone && (
<header className="appbar">
<span className="brand" aria-hidden="true"><Logo /> Nihongo <small lang="ja">日本語</small></span>
{account}
<span className="bar-actions">{themeBtn}{account}</span>
</header>
)}
<main key={gen} className={focused ? 'focused' : 'with-nav'}>
Expand Down Expand Up @@ -191,7 +198,7 @@ export default function App() {
) : view === 'stats' ? (
<Stats db={db} />
) : view === 'settings' ? (
<Settings db={db} autoplay={autoplay} onAutoplay={(v) => { setAutoplay(v); writeBool('nihongo.autoplay', v) }}
<Settings key={theme} db={db} autoplay={autoplay} onAutoplay={(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' ? (
Expand Down Expand Up @@ -227,7 +234,7 @@ export default function App() {
))}
</div>
)}
{!phone && account}
{!phone && <span className="bar-actions">{themeBtn}{account}</span>}
</nav>
)}
</FuriganaContext.Provider>
Expand Down
109 changes: 109 additions & 0 deletions src/ThemeToggle.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<App />)
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(<App />)
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(<App />)
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(<App />)
expect(await screen.findByRole('button', { name: 'Switch to light theme' })).toBeTruthy()
})

it('survives a reload', async () => {
const user = userEvent.setup()
const { unmount } = render(<App />)
await user.click(await screen.findByRole('button', { name: 'Switch to dark theme' }))
unmount()
render(<App />)
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(<App />)
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(<App />)
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(<App />)
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(<App />)
await user.click(await screen.findByRole('button', { name: /^Next lesson:/ }))
await screen.findByRole('button', { name: 'Got it' })
expect(toDark()).toBeNull()
expect(toLight()).toBeNull()
})
2 changes: 2 additions & 0 deletions src/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand All @@ -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 */
Expand All @@ -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)); }
}
Expand Down
Loading