From f7b8e5404a2e2612801e06118bd03b57eca542d2 Mon Sep 17 00:00:00 2001 From: Peter Pan Date: Thu, 10 Jun 2021 15:39:07 +0800 Subject: [PATCH] fix: theme broken in some cases (#985) --- frontend/packages/core/src/store/theme/reducers.ts | 5 ++--- frontend/packages/core/src/utils/theme.ts | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/frontend/packages/core/src/store/theme/reducers.ts b/frontend/packages/core/src/store/theme/reducers.ts index df83f6c7..ca9e0dd3 100644 --- a/frontend/packages/core/src/store/theme/reducers.ts +++ b/frontend/packages/core/src/store/theme/reducers.ts @@ -14,15 +14,14 @@ * limitations under the License. */ -import {THEME, autoTheme} from '~/utils/theme'; +import {THEME, autoTheme, parseTheme} from '~/utils/theme'; import type {ThemeActionTypes, ThemeState} from './types'; import {ActionTypes} from './types'; -import type {Theme} from '~/utils/theme'; const STORAGE_KEY = 'theme'; -const theme = THEME || (window.localStorage.getItem(STORAGE_KEY) as Theme | undefined) || 'auto'; +const theme = THEME || parseTheme(window.localStorage.getItem(STORAGE_KEY) || '') || 'auto'; const initState: ThemeState = { theme: theme === 'auto' ? autoTheme : theme, diff --git a/frontend/packages/core/src/utils/theme.ts b/frontend/packages/core/src/utils/theme.ts index 65895f9a..45e02a82 100644 --- a/frontend/packages/core/src/utils/theme.ts +++ b/frontend/packages/core/src/utils/theme.ts @@ -21,7 +21,9 @@ import kebabCase from 'lodash/kebabCase'; export type Theme = 'light' | 'dark'; -export const THEME: Theme | undefined = import.meta.env.SNOWPACK_PUBLIC_THEME; +export const parseTheme = (t: string): Theme | undefined => (['light', 'dark'].includes(t) ? (t as Theme) : undefined); + +export const THEME = parseTheme(import.meta.env.SNOWPACK_PUBLIC_THEME); export const matchMedia = window.matchMedia('(prefers-color-scheme: dark)'); -- GitLab