Created
July 30, 2024 05:49
-
-
Save joychetry/1a56f529ae09a73249d03517e9592399 to your computer and use it in GitHub Desktop.
Dark Mode Switch
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
/*! | |
* 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