Created
October 4, 2023 12:30
-
-
Save urchymanny/da0e412cd1633c917914ad485bceb8d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// check if the system is currently set to dark, this would return "true" or "false" | |
const defaultDark = window.matchMedia("(prefers-color-scheme: dark)").matches; | |
// assuming we are using a state manager as in React.js | |
// this set the value of "theme" in local storage to "dark" or "light" | |
const [theme, setTheme] = useLocalStorage( | |
"theme", | |
defaultDark ? "dark" : "light" | |
); | |
// This function is used to handle a button toggle to swap themes | |
const switchTheme = () => { | |
setTheme(theme === "light" ? "dark" : "light"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment