From 7261403d2d0a989a2adaf99c8758067385c451b4 Mon Sep 17 00:00:00 2001 From: Josevi Date: Mon, 24 Aug 2026 21:04:42 +0200 Subject: [PATCH] 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). --- frontend/src/App.jsx | 17 ++++++++++++++++- frontend/src/locales/es.js | 1 + frontend/src/views/Settings.jsx | 8 ++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index b413db0..d310957 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -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 diff --git a/frontend/src/locales/es.js b/frontend/src/locales/es.js index 9e1039e..6050aa3 100644 --- a/frontend/src/locales/es.js +++ b/frontend/src/locales/es.js @@ -274,6 +274,7 @@ export default { 'Theme': 'Tema', 'Dark': 'Oscuro', 'Light': 'Claro', + 'System': 'Sistema', 'Accent color': 'Color de acento', 'General': 'General', 'During a workout': 'Durante el entrenamiento', diff --git a/frontend/src/views/Settings.jsx b/frontend/src/views/Settings.jsx index 8630e8b..c56debe 100644 --- a/frontend/src/views/Settings.jsx +++ b/frontend/src/views/Settings.jsx @@ -153,8 +153,12 @@ export default function Settings() { update(s => { s.theme = v })} />