mirror of
https://github.com/dong-nz/cv.git
synced 2026-11-22 16:10:40 +07:00
feat: personal engineering portfolio with routing, theme toggle, and responsive design
- 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
This commit is contained in:
1 parent
0273f555de
commit
7e42bd8017
38 files changed
+2921
-358
No files matched your search
@@ -0,0 +1,78 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user