Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fixed themes issue

Blue Mouse6882458a0332
diff --git a/src/redux/constants/index.js b/src/redux/constants/index.js
index 91c239278..8f8a16292 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -57,6 +57,8 @@ export const HAVE_I_BEEN_PWNED_URL = 'https://haveibeenpwned.com/passwords'
export const LINKEDIN_URL = 'https://www.linkedin.com/company/swetrix/'
export const GITHUB_URL = 'https://github.com/Swetrix'
+export const SUPPORTED_THEMES = ['light', 'dark']
+
export const CONTACT_EMAIL = '[email protected]'
export const SECURITY_EMAIL = '[email protected]'
diff --git a/src/redux/reducers/ui/theme.js b/src/redux/reducers/ui/theme.js
index 6b6ed6d1d..16e25c66d 100644
--- a/src/redux/reducers/ui/theme.js
+++ b/src/redux/reducers/ui/theme.js
@@ -1,33 +1,40 @@
+import _includes from 'lodash/includes'
import { types } from 'redux/actions/ui/types'
-import { LS_THEME_SETTING } from 'redux/constants'
+import { LS_THEME_SETTING, SUPPORTED_THEMES } from 'redux/constants'
+
+const setThemeToDOM = (theme) => {
+ const root = window.document.documentElement
+ const isDark = theme === 'dark'
+
+ root.classList.remove(isDark ? 'light' : 'dark')
+ root.classList.add(theme)
+}
+
+const setTheme = (theme) => {
+ setThemeToDOM(theme)
+ localStorage.setItem(LS_THEME_SETTING, theme)
+ return theme
+}
const getInitialTheme = () => {
if (typeof window !== 'undefined' && window.localStorage) {
- const storedPrefs = window.localStorage.getItem(LS_THEME_SETTING)
- if (typeof storedPrefs === 'string') {
- return storedPrefs
+ const lsTheme = window.localStorage.getItem(LS_THEME_SETTING)
+ if (_includes(SUPPORTED_THEMES, lsTheme)) {
+ setThemeToDOM(lsTheme)
+ return lsTheme
}
const userMedia = window.matchMedia('(prefers-color-scheme: dark)')
if (userMedia.matches) {
+ setThemeToDOM('dark')
return 'dark'
}
}
+ setThemeToDOM('light')
return 'light' // light theme as the default
}
-const setTheme = (rawTheme) => {
- const root = window.document.documentElement
- const isDark = rawTheme === 'dark'
-
- root.classList.remove(isDark ? 'light' : 'dark')
- root.classList.add(rawTheme)
-
- localStorage.setItem(LS_THEME_SETTING, rawTheme)
- return rawTheme
-}
-
const getInitialState = () => {
return {
theme: getInitialTheme(),