dark-mode-toggle
Version:
Web Component that toggles dark mode 🌒
3 lines • 708 B
JavaScript
// @license © 2024 Google LLC. Licensed under the Apache License, Version 2.0.
// @license © 2024 Google LLC. Licensed under the Apache License, Version 2.0.
(()=>{const s="dark-mode-toggle-stylesheets";const t="dark-mode-toggle";const e="light";const l="dark";let o=document.getElementById(s).textContent;let a=null;try{a=localStorage.getItem(t)}catch(s){return}const c=/\(\s*prefers-color-scheme\s*:\s*light\s*\)/gi;const r=/\(\s*prefers-color-scheme\s*:\s*dark\s*\)/gi;const n=/\b(?:dark\s+light|light\s+dark)\b/;switch(a){case e:o=o.replace(c,"$&, all").replace(r,"$& and not all").replace(n,a);break;case l:o=o.replace(r,"$&, all").replace(c,"$& and not all").replace(n,a);break}document.write(o)})();