mirror of
https://github.com/dong-nz/cv.git
synced 2026-11-23 00:20:40 +07:00
- React + TypeScript + Vite + React Router v6 - Dark/light/system theme toggle (fixed cycle from 'system' state) - Multi-page SPA: Home, About, Experience, Projects, Engineering, Contact - Responsive layout with mobile menu - Data-driven sections (profile, skills, experience, projects) - CSS Modules-compatible with CSS variables for theming - Accessibility: skip-link, ARIA labels, keyboard navigation
79 lines
2.0 KiB
TypeScript
79 lines
2.0 KiB
TypeScript
import { createContext, useContext, useEffect, useState, useCallback } from 'react'
|
|
|
|
type Theme = 'dark' | 'light' | 'system'
|
|
|
|
interface ThemeContextValue {
|
|
theme: Theme
|
|
resolvedTheme: 'dark' | 'light'
|
|
setTheme: (theme: Theme) => void
|
|
cycleTheme: () => void
|
|
}
|
|
|
|
const ThemeContext = createContext<ThemeContextValue | undefined>(undefined)
|
|
|
|
const STORAGE_KEY = 'portfolio-theme'
|
|
|
|
function getSystemTheme(): 'dark' | 'light' {
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
|
}
|
|
|
|
function getStoredTheme(): Theme {
|
|
try {
|
|
const stored = localStorage.getItem(STORAGE_KEY)
|
|
if (stored === 'dark' || stored === 'light' || stored === 'system') {
|
|
return stored
|
|
}
|
|
} catch {
|
|
// localStorage unavailable
|
|
}
|
|
return 'system'
|
|
}
|
|
|
|
function resolveTheme(theme: Theme): 'dark' | 'light' {
|
|
return theme === 'system' ? getSystemTheme() : theme
|
|
}
|
|
|
|
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
|
const [theme, setThemeState] = useState<Theme>(getStoredTheme)
|
|
|
|
const resolvedTheme = resolveTheme(theme)
|
|
|
|
useEffect(() => {
|
|
document.documentElement.setAttribute('data-theme', resolvedTheme)
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, theme)
|
|
} catch {
|
|
// localStorage unavailable
|
|
}
|
|
}, [resolvedTheme, theme])
|
|
|
|
const setTheme = useCallback((newTheme: Theme) => {
|
|
setThemeState(newTheme)
|
|
}, [])
|
|
|
|
const cycleTheme = useCallback(() => {
|
|
setThemeState((prev) => {
|
|
if (prev === 'system') {
|
|
return getSystemTheme() === 'dark' ? 'light' : 'dark'
|
|
}
|
|
if (prev === 'dark') return 'light'
|
|
if (prev === 'light') return 'system'
|
|
return 'dark'
|
|
})
|
|
}, [])
|
|
|
|
return (
|
|
<ThemeContext.Provider value={{ theme, resolvedTheme, setTheme, cycleTheme }}>
|
|
{children}
|
|
</ThemeContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useTheme() {
|
|
const context = useContext(ThemeContext)
|
|
if (!context) {
|
|
throw new Error('useTheme must be used within ThemeProvider')
|
|
}
|
|
return context
|
|
}
|