From ba7c6d34e4737dc6c6590b814afe35dbcb15aa2d Mon Sep 17 00:00:00 2001 From: Armano Date: Fri, 19 Mar 2021 12:58:58 +0100 Subject: [PATCH] refactor(v2): add missing types to theme-classic useTheme --- .../src/theme/hooks/useTheme.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/hooks/useTheme.ts b/packages/docusaurus-theme-classic/src/theme/hooks/useTheme.ts index b2a24c356c39..05a8fcff1cdf 100644 --- a/packages/docusaurus-theme-classic/src/theme/hooks/useTheme.ts +++ b/packages/docusaurus-theme-classic/src/theme/hooks/useTheme.ts @@ -14,21 +14,23 @@ import {useThemeConfig} from '@docusaurus/theme-common'; const themes = { light: 'light', dark: 'dark', -}; +} as const; + +type Themes = typeof themes[keyof typeof themes]; // Ensure to always return a valid theme even if input is invalid -const coerceToTheme = (theme) => { +const coerceToTheme = (theme?: string | null): Themes => { return theme === themes.dark ? themes.dark : themes.light; }; -const getInitialTheme = (defaultMode) => { +const getInitialTheme = (defaultMode: Themes | undefined): Themes => { if (!ExecutionEnvironment.canUseDOM) { return coerceToTheme(defaultMode); } return coerceToTheme(document.documentElement.getAttribute('data-theme')); }; -const storeTheme = (newTheme) => { +const storeTheme = (newTheme: Themes) => { try { localStorage.setItem('theme', coerceToTheme(newTheme)); } catch (err) {