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

+183
View File
@@ -181,6 +181,189 @@
.mobile-menu-btn {
display: flex;
}
.nav-palette-nav {
display: none;
}
}
/* ===== Theme palette menu ===== */
.nav-palette {
position: relative;
display: flex;
}
.nav-palette-trigger {
display: flex;
align-items: center;
gap: 8px;
background: none;
border: none;
color: var(--text-muted);
cursor: pointer;
padding: 8px;
border-radius: var(--radius-md);
font-size: 14px;
transition: color var(--transition-fast), background var(--transition-fast);
}
.nav-palette-trigger:hover,
.nav-palette-trigger.open {
color: var(--text-primary);
background: var(--bg-elevated);
}
.nav-palette-trigger:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.nav-palette-swatch {
flex: none;
width: 14px;
height: 14px;
border-radius: 50%;
background: var(--accent);
box-shadow: inset 0 0 0 1px var(--border-strong);
}
/* The group name is redundant in the bar (the trigger is labelled) but needed in the sheet. */
.nav-palette-group {
display: none;
}
.nav-palette-trigger .chevron {
transition: transform 0.2s;
}
.nav-palette-trigger.open .chevron {
transform: rotate(180deg);
}
.nav-palette-panel {
display: none;
position: absolute;
top: calc(100% + 4px);
right: 0;
width: max-content;
min-width: 220px;
padding: 4px;
background: var(--bg-elevated);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
box-shadow: 0 4px 12px var(--shadow);
z-index: 100;
}
.nav-palette-panel.open {
display: block;
}
.nav-palette-option {
display: flex;
align-items: center;
gap: 10px;
width: 100%;
min-height: 44px;
padding: 8px 12px;
background: none;
border: none;
border-left: 2px solid transparent;
border-radius: var(--radius-sm);
color: var(--text-primary);
text-align: left;
cursor: pointer;
transition: background 0.15s, color 0.15s;
}
.nav-palette-option.active {
border-left-color: var(--accent);
}
.nav-palette-option.active::after {
content: '✓';
margin-left: 8px;
color: var(--accent);
}
.nav-palette-option:hover {
background: var(--accent);
color: var(--text-on-accent);
}
.nav-palette-option:hover .nav-palette-hint,
.nav-palette-option.active:hover::after {
color: var(--text-on-accent);
opacity: 0.75;
}
.nav-palette-option:focus-visible {
outline: 2px solid var(--accent);
outline-offset: -2px;
}
.nav-palette-preview {
flex: none;
width: 18px;
height: 18px;
border-radius: 50%;
border: 2px solid;
}
.nav-palette-preview[data-preview='red'] {
background: var(--preview-red-accent);
border-color: var(--preview-red-bg);
}
.nav-palette-preview[data-preview='blue'] {
background: var(--preview-blue-accent);
border-color: var(--preview-blue-bg);
}
.nav-palette-name {
font-size: 14px;
font-weight: 500;
}
.nav-palette-hint {
margin-left: auto;
font-size: 12px;
color: var(--text-muted);
}
/* Sheet variant: the options expand inline inside the mobile menu, never as a
second overlay. .mobile-menu is display:none unless .open, so this block is
already unreachable above 1024px. */
.nav-palette-sheet {
display: block;
margin-top: 16px;
padding-top: 16px;
border-top: 1px solid var(--border);
}
.nav-palette-sheet .nav-palette-trigger {
width: 100%;
min-height: 44px;
padding: 12px 0;
font-size: var(--text-body);
}
.nav-palette-sheet .nav-palette-group {
display: inline;
font-size: var(--text-meta);
color: var(--text-muted);
}
.nav-palette-sheet .nav-palette-panel {
position: static;
width: 100%;
min-width: 0;
padding: 0;
box-shadow: none;
}
.nav-palette-sheet .nav-palette-option {
border-radius: 0;
}
/* ===== Buttons ===== */
+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>
)
}
+8
View File
@@ -0,0 +1,8 @@
import type { Language, Palette } from '@/types'
export const paletteIds: Palette[] = ['red', 'blue']
export const paletteMeta: Record<Palette, { label: Record<Language, string>; hint: Record<Language, string> }> = {
red: { label: { en: 'Red', vi: 'Đỏ' }, hint: { en: 'warm neutral', vi: 'trung tính ấm' } },
blue: { label: { en: 'Blue', vi: 'Xanh dương' }, hint: { en: 'cool neutral', vi: 'trung tính lạnh' } },
}
+25 -2
View File
@@ -1,4 +1,5 @@
import { createContext, useContext, useEffect, useState, useCallback } from 'react'
import type { Palette } from '@/types'
type Theme = 'dark' | 'light'
@@ -7,11 +8,14 @@ interface ThemeContextValue {
resolvedTheme: 'dark' | 'light'
setTheme: (theme: Theme) => void
cycleTheme: () => void
palette: Palette
setPalette: (palette: Palette) => void
}
const ThemeContext = createContext<ThemeContextValue | undefined>(undefined)
const STORAGE_KEY = 'portfolio-theme'
const PALETTE_STORAGE_KEY = 'portfolio-palette'
function getInitialTheme(): 'dark' | 'light' {
try {
@@ -26,25 +30,40 @@ function getInitialTheme(): 'dark' | 'light' {
return prefersLight ? 'light' : 'dark'
}
function getInitialPalette(): Palette {
try {
const stored = localStorage.getItem(PALETTE_STORAGE_KEY)
if (stored === 'red' || stored === 'blue') {
return stored
}
} catch {
// localStorage unavailable
}
return 'red'
}
function resolveTheme(theme: Theme): 'dark' | 'light' {
return theme
}
export function ThemeProvider({ children }: { children: React.ReactNode }) {
const [theme, setThemeState] = useState<Theme>(getInitialTheme)
const [palette, setPaletteState] = useState<Palette>(getInitialPalette)
const resolvedTheme = resolveTheme(theme)
// Set data-theme during render so it's available before paint
document.documentElement.setAttribute('data-theme', resolvedTheme)
document.documentElement.setAttribute('data-palette', palette)
useEffect(() => {
try {
localStorage.setItem(STORAGE_KEY, theme)
localStorage.setItem(PALETTE_STORAGE_KEY, palette)
} catch {
// localStorage unavailable
}
}, [theme])
}, [theme, palette])
const setTheme = useCallback((newTheme: Theme) => {
@@ -55,8 +74,12 @@ export function ThemeProvider({ children }: { children: React.ReactNode }) {
setThemeState((prev) => (prev === 'dark' ? 'light' : 'dark'))
}, [])
const setPalette = useCallback((next: Palette) => {
setPaletteState(next)
}, [])
return (
<ThemeContext.Provider value={{ theme, resolvedTheme, setTheme, cycleTheme }}>
<ThemeContext.Provider value={{ theme, resolvedTheme, setTheme, cycleTheme, palette, setPalette }}>
{children}
</ThemeContext.Provider>
)
+2
View File
@@ -6,6 +6,8 @@ export const strings = {
projects: {en: 'Projects', vi: 'Dự án'},
philosophy: {en: 'Philosophy', vi: 'Triết lý'},
contact: {en: 'Contact', vi: 'Liên hệ'},
palette: {en: 'Theme color', vi: 'Màu giao diện'},
paletteAria: {en: 'Switch theme color', vi: 'Đổi màu giao diện'},
},
footer: {
copyright: {en: 'All rights reserved', vi: 'Bảo lưu mọi quyền'},
+71 -6
View File
@@ -10,9 +10,9 @@
--accent-hover: #fca5a5;
--accent-active: #f06060;
--text-on-accent: #151914;
--accent-soft: rgba(239, 68, 68, 0.12);
--accent-soft: rgba(248, 113, 113, 0.12);
--accent-tint: #2f1e1a;
--accent-glow: rgba(239, 68, 68, 0.3);
--accent-glow: rgba(248, 113, 113, 0.3);
--border: #2a3028;
--border-strong: #3a403a;
--shadow: rgba(0, 0, 0, 0.45);
@@ -68,9 +68,9 @@
--accent-hover: #a8382f;
--accent-active: #9b3229;
--text-on-accent: #ffffff;
--accent-soft: rgba(239, 68, 68, 0.1);
--accent-soft: rgba(176, 65, 56, 0.1);
--accent-tint: #f5e3e1;
--accent-glow: rgba(239, 68, 68, 0.22);
--accent-glow: rgba(176, 65, 56, 0.22);
--border: #e3e1dc;
--border-strong: #d9d6d0;
--shadow: rgba(21, 25, 20, 0.1);
@@ -84,6 +84,71 @@
color-scheme: light;
}
/* Blue palette. Both selectors carry two attributes (0,3,0), so they beat
:root (0,1,0) and :root[data-theme='light'] (0,2,0) regardless of block order. */
:root[data-theme='dark'][data-palette='blue'] {
--bg-primary: #0f1117;
--bg-surface: #161922;
--bg-elevated: #1c2030;
--text-primary: #e8eaed;
--text-secondary: #9aa0a6;
--text-muted: #828d9e;
--accent: #60a5fa;
--accent-hover: #93c5fd;
--accent-active: #3b82f6;
--text-on-accent: #0f1117;
--accent-soft: rgba(96, 165, 250, 0.12);
--accent-tint: #182338;
--accent-glow: rgba(96, 165, 250, 0.3);
--border: #2a3040;
--border-strong: #39415a;
--shadow: rgba(0, 0, 0, 0.45);
--inset-highlight: rgba(255, 255, 255, 0.05);
--code-bg: #1a1e2e;
--success: #34d399;
--success-glow: rgba(52, 211, 153, 0.35);
--error: #f87171;
--warning: #fbbf24;
--grid-color: rgba(232, 234, 237, 0.06);
color-scheme: dark;
}
:root[data-theme='light'][data-palette='blue'] {
--bg-primary: #f4f6f9;
--bg-surface: #fbfcfe;
--bg-elevated: #ffffff;
--text-primary: #111827;
--text-secondary: #4b5563;
--text-muted: #5c6b82;
--accent: #2563eb;
--accent-hover: #1d4ed8;
--accent-active: #1e40af;
--text-on-accent: #ffffff;
--accent-soft: rgba(37, 99, 235, 0.1);
--accent-tint: #eef3fc;
--accent-glow: rgba(37, 99, 235, 0.22);
--border: #dfe3ea;
--border-strong: #d3d8e1;
--shadow: rgba(17, 24, 39, 0.1);
--inset-highlight: rgba(255, 255, 255, 0.9);
--code-bg: #eceff5;
--success: #15803d;
--success-glow: rgba(21, 128, 61, 0.35);
--error: #991b1b;
--warning: #b45309;
--grid-color: rgba(17, 24, 39, 0.07);
color-scheme: light;
}
/* Picker previews: fixed identity colors so a row previews its own palette,
not the one currently active. Never referenced outside .nav-palette-preview. */
:root {
--preview-red-accent: #b04138;
--preview-red-bg: #f6f5f2;
--preview-blue-accent: #2563eb;
--preview-blue-bg: #f4f6f9;
}
/* Pre-hydration / no-JS: honour an OS light preference */
@media (prefers-color-scheme: light) {
:root:not([data-theme='dark']) {
@@ -97,9 +162,9 @@
--accent-hover: #a8382f;
--accent-active: #9b3229;
--text-on-accent: #ffffff;
--accent-soft: rgba(239, 68, 68, 0.1);
--accent-soft: rgba(176, 65, 56, 0.1);
--accent-tint: #f5e3e1;
--accent-glow: rgba(239, 68, 68, 0.22);
--accent-glow: rgba(176, 65, 56, 0.22);
--border: #e3e1dc;
--border-strong: #d9d6d0;
--shadow: rgba(21, 25, 20, 0.1);
+2
View File
@@ -1,5 +1,7 @@
export type Language = 'en' | 'vi';
export type Palette = 'red' | 'blue';
export type ProjectCategory =
| "software-engineering"
| "logistics"