Skip to content

Instantly share code, notes, and snippets.

@urchymanny
Created October 4, 2023 12:30
Show Gist options
  • Save urchymanny/da0e412cd1633c917914ad485bceb8d2 to your computer and use it in GitHub Desktop.
Save urchymanny/da0e412cd1633c917914ad485bceb8d2 to your computer and use it in GitHub Desktop.
...
// 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