chore: update color scheme listener (#735)

This commit is contained in:
boojack 2022-12-11 23:18:25 +08:00 committed by GitHub
parent 37b7b983d2
commit 5e978e2cfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,12 +22,23 @@ function App() {
}, []);
useEffect(() => {
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
const darkMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const handleColorSchemeChange = (e: MediaQueryListEvent) => {
if (globalService.getState().appearance === "system") {
const mode = e.matches ? "dark" : "light";
setMode(mode);
}
});
};
try {
if (darkMediaQuery.addEventListener) {
darkMediaQuery.addEventListener("change", handleColorSchemeChange);
} else {
darkMediaQuery.addListener(handleColorSchemeChange);
}
} catch (error) {
console.error("failed to initial color scheme listener", error);
}
}, []);
// Inject additional style and script codes.