Skip to content

Instantly share code, notes, and snippets.

@joychetry
Created July 30, 2024 05:49
Show Gist options
  • Save joychetry/1a56f529ae09a73249d03517e9592399 to your computer and use it in GitHub Desktop.
Save joychetry/1a56f529ae09a73249d03517e9592399 to your computer and use it in GitHub Desktop.
Dark Mode Switch
/*!
* Dark Mode Switch v1.0.1 (https://github.com/coliff/dark-mode-switch)
* Copyright 2021 C.Oliff
* Licensed under MIT (https://github.com/coliff/dark-mode-switch/blob/main/LICENSE)
*/
var darkSwitch=document.getElementById("darkSwitch");window.addEventListener("load",(function(){if(darkSwitch){initTheme();darkSwitch.addEventListener("change",(function(){resetTheme()}))}}));function initTheme(){var darkThemeSelected=localStorage.getItem("darkSwitch")!==null&&localStorage.getItem("darkSwitch")==="dark";darkSwitch.checked=darkThemeSelected;darkThemeSelected?document.body.setAttribute("data-theme","dark"):document.body.removeAttribute("data-theme")}function resetTheme(){if(darkSwitch.checked){document.body.setAttribute("data-theme","dark");localStorage.setItem("darkSwitch","dark")}else{document.body.removeAttribute("data-theme");localStorage.removeItem("darkSwitch")}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment