fix(theme-classic): do not switch color modes when printing (#6490)

* fix(theme-classic): coerce to light theme when printing

* revert this...

* fix
This commit is contained in:
Joshua Chen 2022-01-28 20:19:23 +08:00 committed by GitHub
parent ade486d079
commit c99026c524
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -82,16 +82,28 @@ function useColorModeContextValue(): ColorModeContextValue {
}
}, [disableSwitch, setTheme]);
// PCS is coerced to light mode when printing, which causes the color mode to
// be reset to dark when exiting print mode, disregarding user settings. When
// the listener fires only because of a print/screen switch, we don't change
// color mode. See https://github.com/facebook/docusaurus/pull/6490
const previousMediaIsPrint = React.useRef(false);
useEffect(() => {
if (disableSwitch && !respectPrefersColorScheme) {
return;
return undefined;
}
window
.matchMedia('(prefers-color-scheme: dark)')
.addListener(({matches}) => {
setTheme(matches ? themes.dark : themes.light);
});
const mql = window.matchMedia('(prefers-color-scheme: dark)');
const onChange = ({matches}: MediaQueryListEvent) => {
if (window.matchMedia('print').matches || previousMediaIsPrint.current) {
previousMediaIsPrint.current = window.matchMedia('print').matches;
return;
}
setTheme(matches ? themes.dark : themes.light);
};
mql.addListener(onChange);
return () => {
mql.removeListener(onChange);
};
}, [disableSwitch, respectPrefersColorScheme]);
return {