mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-02 08:19:07 +02:00
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:
parent
ade486d079
commit
c99026c524
1 changed files with 19 additions and 7 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue