feat: add brand assets, design tokens and light/dark theme
- Use real logo/isotype SVGs across app and emails - Token-based color system aligned with landing (brand scale + semantic tokens) - ThemeProvider (light/dark/system) with header toggle and settings card - Embed logo in transactional emails; centralize email colors
This commit is contained in:
68
apps/web/src/context/ThemeProvider.tsx
Normal file
68
apps/web/src/context/ThemeProvider.tsx
Normal file
@@ -0,0 +1,68 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
|
||||
export type Theme = 'light' | 'dark' | 'system'
|
||||
|
||||
type ThemeContextValue = {
|
||||
theme: Theme
|
||||
isDark: boolean
|
||||
setTheme: (theme: Theme) => void
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'theme'
|
||||
|
||||
function readStoredTheme(): Theme {
|
||||
const value = localStorage.getItem(STORAGE_KEY)
|
||||
return value === 'light' || value === 'dark' || value === 'system' ? value : 'system'
|
||||
}
|
||||
|
||||
function systemPrefersDark() {
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
}
|
||||
|
||||
function resolveDark(theme: Theme) {
|
||||
return theme === 'dark' || (theme === 'system' && systemPrefersDark())
|
||||
}
|
||||
|
||||
const ThemeContext = createContext<ThemeContextValue | null>(null)
|
||||
|
||||
export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
const [theme, setThemeState] = useState<Theme>(readStoredTheme)
|
||||
const [isDark, setIsDark] = useState(() =>
|
||||
document.documentElement.classList.contains('dark'),
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const update = () => {
|
||||
document.documentElement.classList.toggle('dark', resolveDark(theme))
|
||||
setIsDark(document.documentElement.classList.contains('dark'))
|
||||
localStorage.setItem(STORAGE_KEY, theme)
|
||||
}
|
||||
update()
|
||||
|
||||
if (theme !== 'system') return
|
||||
const media = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
media.addEventListener('change', update)
|
||||
return () => media.removeEventListener('change', update)
|
||||
}, [theme])
|
||||
|
||||
const setTheme = useCallback((next: Theme) => setThemeState(next), [])
|
||||
const value = useMemo(() => ({ theme, isDark, setTheme }), [theme, isDark, setTheme])
|
||||
|
||||
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
const context = useContext(ThemeContext)
|
||||
if (!context) {
|
||||
throw new Error('useTheme debe usarse dentro de <ThemeProvider>')
|
||||
}
|
||||
return context
|
||||
}
|
||||
Reference in New Issue
Block a user