From d2307e7ff535aaa707428ee9f2bb261bcd145167 Mon Sep 17 00:00:00 2001 From: Robert Eggl Date: Tue, 7 Nov 2023 16:50:32 +0100 Subject: [PATCH] Adds dynamic status bar colors --- rogue-thi-app/data/themes.json | 2 +- rogue-thi-app/pages/_app.jsx | 42 +++++++++++++++++++++++++++++- rogue-thi-app/public/manifest.json | 1 - 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/rogue-thi-app/data/themes.json b/rogue-thi-app/data/themes.json index 6760374c..43b9a6b3 100644 --- a/rogue-thi-app/data/themes.json +++ b/rogue-thi-app/data/themes.json @@ -1,8 +1,8 @@ [ { "name": { "en": "Automatic", "de": "Automatisch" }, "style": "default", "mapTheme": "system" }, { "name": { "en": "Light", "de": "Hell" }, "style": "light", "mapTheme": "light"}, - { "name": { "en": "Blue", "de": "Blau" }, "style": "blue", "mapTheme": "light"}, { "name": { "en": "Dark", "de": "Dunkel" }, "style": "dark", "mapTheme": "dark" }, + { "name": { "en": "Blue", "de": "Blau" }, "style": "blue", "mapTheme": "light"}, { "name": { "en": "Barbie & Ken", "de": "Barbie & Ken" }, "style": "barbie", "mapTheme": "light" }, { "name": { "en": "Retro", "de": "Retro" }, "style": "retro", "mapTheme": "dark" }, { "name": { "en": "Windows 95", "de": "Windows 95" }, "style": "95", "mapTheme": "light" }, diff --git a/rogue-thi-app/pages/_app.jsx b/rogue-thi-app/pages/_app.jsx index 4d678aed..624a3edd 100644 --- a/rogue-thi-app/pages/_app.jsx +++ b/rogue-thi-app/pages/_app.jsx @@ -29,6 +29,7 @@ config.autoAddCss = false function MyApp ({ Component, pageProps }) { const router = useRouter() const [theme, setTheme] = useState('default') + const [themeColor, setThemeColor] = useState(undefined) const [showThemeModal, setShowThemeModal] = useState(false) const [showDashboardModal, setShowDashboardModal] = useState(false) const [showPersonalDataModal, setShowPersonalDataModal] = useState(false) @@ -43,6 +44,9 @@ function MyApp ({ Component, pageProps }) { resetOrder } = useDashboard() + const LIGHT = '#ffffff' + const DARK = '#202020' + useEffect(() => { // migrate legacy cookie theme setting to localStorage // added 2022-04-15, can be removed later @@ -65,6 +69,42 @@ function MyApp ({ Component, pageProps }) { } }, [theme, router.pathname]) + // Update the theme color and theme when the user changes dark/light mode + useEffect(() => { + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)') + const handleChange = () => { + if (theme === 'default') { + if (mediaQuery.matches) { + setThemeColor(DARK) + setTheme('default') + } else { + setThemeColor(LIGHT) + setTheme('default') + } + } + } + mediaQuery.addEventListener('change', handleChange) + return () => mediaQuery.removeEventListener('change', handleChange) + }) + + // Update the theme color when the user changes the theme + useEffect(() => { + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)') + const isDarkMode = mediaQuery.matches + const themeColors = { + light: LIGHT, + dark: DARK, + retro: '#121212', + barbie: '#d44e95', + hacker: '#0ae40a', + pride: '#8845ef', + blue: '#005ea1', + 95: '#008080' + } + const selectedThemeColor = themeColors[theme] || (isDarkMode ? DARK : LIGHT) + setThemeColor(selectedThemeColor) + }, [theme]) + return ( @@ -92,7 +132,7 @@ function MyApp ({ Component, pageProps }) { /> - + diff --git a/rogue-thi-app/public/manifest.json b/rogue-thi-app/public/manifest.json index 9056595a..aa12772a 100644 --- a/rogue-thi-app/public/manifest.json +++ b/rogue-thi-app/public/manifest.json @@ -1,7 +1,6 @@ { "name": "neuland.app", "short_name": "neuland.app", - "theme_color": "#8845ef", "background_color": "#ffffff", "display": "standalone", "orientation": "portrait",