feat(theme): red/blue palette switch with a nav + mobile-sheet color menu

Second axis on <html>: data-palette (Portfolio type Palette = 'red' | 'blue',
localStorage key portfolio-palette, default red so existing visitors see no
change). Both attributes are written before paint by the index.html init
script and re-written during render in useTheme.

- src/types/index.ts, src/data/palettes.ts: Palette + paletteIds/paletteMeta
  (Record<Language, string>, indexed inline - no lookup helper)
- src/hooks/useTheme.tsx: palette state, data-palette write during render,
  persistence, setPalette; no cyclePalette
- src/components/layout/ThemePaletteMenu.tsx: disclosure semantics
  (aria-expanded/aria-controls, aria-current), variant nav|sheet, panel ids
  derived from variant, Escape closes and refocuses the trigger
- src/components/layout/Navigation.tsx: nav instance in .nav-actions, sheet
  instance after the mobile link list, remounted closed via key
- src/index.css: blue dark/light blocks (0,3,0 selectors) + preview tokens;
  red --accent-soft/--accent-glow re-derived from the accent in force
- src/App.css: .nav-palette* block (token-only), nav hidden at 1024px,
  sheet options inline
- specs: palette-switch spec v0.3.0 with the implementation record; corrected
  the false AA claim in spec-v3-theme-colors.md (3.90:1, not ~4.8), the
  composite values in spec-v3-theme-palette-gasatrya.md, the Footer consumer
  claim in spec-v3-theme-switch.md; AGENTS.md Theming section documents the
  palette axis

Verified: tsc exit 0; vite build green (CSS 31.80 kB); oxlint unchanged at
11 errors / 9 warnings; hex gate over src/App.css 0 matches; inline type
styles 0; contrast minima 4.96 / 4.63 / 4.81 / 4.64 with zero AA failures.
This commit is contained in:
dong.nguyen committed 2026-10-17 00:32:49 +07:00
1 parent 5b4b301ef1
commit f2c920d6ad
14 files changed
+1033 -34

No files matched your search

+5
View File
@@ -4,6 +4,7 @@ import { useTheme } from '@/hooks/useTheme'
import { useLanguage } from '@/hooks/useLanguage'
import { strings } from '@/i18n/strings'
import { RollingTextLink } from '@/components/ui/RollingTextLink'
import { ThemePaletteMenu } from '@/components/layout/ThemePaletteMenu'
const navPaths = ['/', '/about', '/experience', '/projects', '/contact', '/philosophy'] as const
export function Navigation() {
const { cycleTheme, resolvedTheme } = useTheme()
@@ -72,6 +73,8 @@ export function Navigation() {
)}
</button>
<ThemePaletteMenu variant="nav" />
<button
className="lang-toggle"
onClick={toggleLang}
@@ -118,6 +121,8 @@ export function Navigation() {
)
})}
</ul>
<ThemePaletteMenu key={location.pathname} variant="sheet" />
</div>
</>
)
@@ -0,0 +1,79 @@
import { useCallback, useRef, useState } from 'react'
import { useTheme } from '@/hooks/useTheme'
import { useLanguage } from '@/hooks/useLanguage'
import { useClickOutside } from '@/hooks/useClickOutside'
import { paletteIds, paletteMeta } from '@/data/palettes'
import { strings } from '@/i18n/strings'
interface ThemePaletteMenuProps {
variant: 'nav' | 'sheet'
}
export function ThemePaletteMenu({ variant }: ThemePaletteMenuProps) {
const { palette, setPalette } = useTheme()
const { lang } = useLanguage()
const [open, setOpen] = useState(false)
const containerRef = useRef<HTMLDivElement>(null)
const triggerRef = useRef<HTMLButtonElement>(null)
const close = useCallback(() => setOpen(false), [])
useClickOutside(containerRef, close)
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key !== 'Escape') return
close()
triggerRef.current?.focus()
}
return (
<div ref={containerRef} className={`nav-palette nav-palette-${variant}`} onKeyDown={handleKeyDown}>
<button
ref={triggerRef}
type="button"
className={`nav-palette-trigger ${open ? 'open' : ''}`}
aria-expanded={open}
aria-controls={`palette-menu-${variant}`}
aria-label={`${strings.nav.palette[lang]}: ${paletteMeta[palette].label[lang]}`}
title={strings.nav.paletteAria[lang]}
onClick={() => setOpen((prev) => !prev)}
>
<span className="nav-palette-swatch" aria-hidden="true" />
<span className="nav-palette-group">{strings.nav.palette[lang]}</span>
<span className="nav-palette-current">{paletteMeta[palette].label[lang]}</span>
<svg
className="chevron"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<polyline points="6 9 12 15 18 9" />
</svg>
</button>
<div id={`palette-menu-${variant}`} className={`nav-palette-panel ${open ? 'open' : ''}`}>
{paletteIds.map((id) => (
<button
key={id}
type="button"
className={`nav-palette-option ${id === palette ? 'active' : ''}`}
aria-current={id === palette ? 'true' : undefined}
onClick={() => {
setPalette(id)
setOpen(false)
}}
>
<span className="nav-palette-preview" data-preview={id} aria-hidden="true" />
<span className="nav-palette-name">{paletteMeta[id].label[lang]}</span>
<span className="nav-palette-hint">{paletteMeta[id].hint[lang]}</span>
</button>
))}
</div>
</div>
)
}