-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathdark-mode-switch.min.js
More file actions
6 lines (6 loc) · 912 Bytes
/
dark-mode-switch.min.js
File metadata and controls
6 lines (6 loc) · 912 Bytes
1
2
3
4
5
6
/*!
* 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");function initTheme(){var e=window.matchMedia?window.matchMedia("(prefers-color-scheme: dark)"):null,t=localStorage.getItem("darkSwitch"),d="light"!==t&&"dark"===t;null===t&&null!==e&&(d=e.matches),darkSwitch.checked=d,d?document.body.setAttribute("data-theme","dark"):document.body.removeAttribute("data-theme")}function resetTheme(){darkSwitch.checked?(document.body.setAttribute("data-theme","dark"),localStorage.setItem("darkSwitch","dark")):(document.body.removeAttribute("data-theme"),localStorage.setItem("darkSwitch","light"))}window.addEventListener("load",function(){darkSwitch&&(initTheme(),darkSwitch.addEventListener("change",function(){resetTheme()}))});