settings: opcion de tema "Sistema" que sigue el prefers-color-scheme del OS
Ademas de Oscuro/Claro fijos, ahora se puede elegir que el tema siga la preferencia del sistema operativo/navegador, con actualizacion en vivo si esta cambia mientras la app esta abierta (sin necesitar recargar).
This commit is contained in:
1 parent
50194442b6
commit
7261403d2d
3 files changed
+23
-3
No files matched your search
+16
-1
@@ -28,9 +28,14 @@ import Admin from './views/Admin.jsx'
|
||||
|
||||
bindUI(useUI) // lets the shared controls open sheets without importing the store at module scope
|
||||
|
||||
// theme === 'system' follows the OS/browser preference instead of a fixed choice.
|
||||
const resolveTheme = theme => theme === 'light' || theme === 'dark'
|
||||
? theme
|
||||
: (window.matchMedia?.('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
|
||||
|
||||
function applyPrefs(theme, accent) {
|
||||
const de = document.documentElement
|
||||
de.dataset.theme = theme === 'light' ? 'light' : 'dark'
|
||||
de.dataset.theme = resolveTheme(theme)
|
||||
de.dataset.accent = ACCENTS[accent] ? accent : 'lime'
|
||||
const meta = document.querySelector('meta[name="theme-color"]')
|
||||
if (meta) meta.content = de.dataset.theme === 'light' ? '#f2f2f7' : '#000000'
|
||||
@@ -44,6 +49,16 @@ function Shell() {
|
||||
const langV = useLang() // re-renders the whole shell when the language (pack) changes
|
||||
useEffect(() => { setNav(navigate) }, [navigate])
|
||||
useEffect(() => { applyPrefs(S.theme, S.accent) }, [S.theme, S.accent])
|
||||
// 'system' needs to react live if the OS theme flips while the app is open, not just on
|
||||
// the next mount — a fixed 'dark'/'light' choice never re-fires this since matchMedia
|
||||
// isn't consulted for those.
|
||||
useEffect(() => {
|
||||
if (S.theme !== 'system' || !window.matchMedia) return
|
||||
const mql = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
const onChange = () => applyPrefs(S.theme, S.accent)
|
||||
mql.addEventListener('change', onChange)
|
||||
return () => mql.removeEventListener('change', onChange)
|
||||
}, [S.theme, S.accent])
|
||||
useEffect(() => { setLang(S.lang || 'en') }, [S.lang])
|
||||
useEffect(() => { document.documentElement.lang = S.lang || 'en' }, [langV, S.lang])
|
||||
// every tab/route change starts at the top of the page
|
||||
|
||||
Reference in new issue
Block a user