fix(v2): set theme color mode for SSR (#4383)

This commit is contained in:
Alexey Pyltsyn 2021-03-10 22:10:07 +03:00 committed by GitHub
parent 02cd5d343b
commit 6afa83c419
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,9 +21,9 @@ const coerceToTheme = (theme) => {
return theme === themes.dark ? themes.dark : themes.light;
};
const getInitialTheme = () => {
const getInitialTheme = (defaultMode) => {
if (!ExecutionEnvironment.canUseDOM) {
return themes.light; // SSR: we don't care
return coerceToTheme(defaultMode);
}
return coerceToTheme(document.documentElement.getAttribute('data-theme'));
};
@ -38,9 +38,9 @@ const storeTheme = (newTheme) => {
const useTheme = (): useThemeReturns => {
const {
colorMode: {disableSwitch, respectPrefersColorScheme},
colorMode: {defaultMode, disableSwitch, respectPrefersColorScheme},
} = useThemeConfig();
const [theme, setTheme] = useState(getInitialTheme);
const [theme, setTheme] = useState(getInitialTheme(defaultMode));
const setLightTheme = useCallback(() => {
setTheme(themes.light);