feat(design-system): ThemeProvider + useTheme — toggle dark/light (Sprint 0.5 étape 2)
This commit is contained in:
parent
e0a4653653
commit
a1d98bd255
3 changed files with 64 additions and 5 deletions
26
src/shared/lib/theme.ts
Normal file
26
src/shared/lib/theme.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { createContext } from 'react'
|
||||
|
||||
export type Theme = 'light' | 'dark'
|
||||
|
||||
export interface ThemeContextValue {
|
||||
theme: Theme
|
||||
setTheme: (t: Theme) => void
|
||||
}
|
||||
|
||||
export const ThemeContext = createContext<ThemeContextValue | null>(null)
|
||||
|
||||
const STORAGE_KEY = 'expria-theme'
|
||||
|
||||
export function getInitialTheme(): Theme {
|
||||
const stored = localStorage.getItem(STORAGE_KEY)
|
||||
if (stored === 'light' || stored === 'dark') return stored
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
||||
}
|
||||
|
||||
export function applyTheme(theme: Theme): void {
|
||||
document.documentElement.classList.toggle('dark', theme === 'dark')
|
||||
}
|
||||
|
||||
export function persistTheme(theme: Theme): void {
|
||||
localStorage.setItem(STORAGE_KEY, theme)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue