Skip to content

Commit

Permalink
refactor(v2): add missing types to theme-classic useTheme (#4467)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored Mar 19, 2021
1 parent 9e881b4 commit 61548d3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/docusaurus-theme-classic/src/theme/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 61548d3

Please sign in to comment.