Replies: 1 comment
-
For a more up-to-date example as of this current dark mode implementation in the official docs, simply change everything inside ...
export function ThemeProvider({
...
if (theme === "system") {
// set the initial theme based on the system preference
const queryMedia = window.matchMedia("(prefers-color-scheme: dark)")
const systemTheme = queryMedia.matches ? "dark" : "light"
root.classList.add(systemTheme)
// listen for changes to the system theme
const listener = (e: MediaQueryListEvent) => {
root.classList.remove("light", "dark")
root.classList.add(e.matches ? "dark" : "light")
}
queryMedia.addEventListener("change", listener)
return () => queryMedia.removeEventListener("change", listener)
}
... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the current dark mode, if the system preference theme changes, the application will not reflect the change.
I recommend adding an event listener to observe theme changes.
It also can resolve the #1757
Vite
Add new
useSystemPreferenceDark
hookUpdate
ThemeProvider
, add a newresolvedTheme
value inuseTheme
If you like this proposal, I would be happy to provide examples for other frameworks and submit a pull request.
Beta Was this translation helpful? Give feedback.
All reactions