/* A script to toggle dark mode with a button, * storing the current state in browser local storage. * The button hasn't yet been added... */constloadTheme=()=>{functionsvg(url: string){letobj=document.createElement("img");obj.src=url;obj.style.height="30px";obj.style.width="30px";returnobj;}constTHEME={LIGHT: "light",DARK: "dark",};constICONS={light: svg("/components/ToggleDarkMode/assets/sun_icon.svg"),dark: svg("/components/ToggleDarkMode/assets/moon_icon.svg"),};constpossibleThemes=Object.values(THEME).map((theme)=>`${theme}-theme`);constbtn=document.querySelector(".toggle-dark-mode");constprefersDarkScheme=window.matchMedia("(prefers-color-scheme: dark)").matches;letcurrentTheme=localStorage.getItem("theme")??(prefersDarkScheme ? THEME.DARK : THEME.LIGHT);functionswitchToTheme(add){console.log("Switching to theme ",add);constclasses=document.body.classList;classes.remove(...possibleThemes);classes.add(`${add}-theme`);currentTheme=add;constThemeCSS={[THEME.LIGHT]: document.getElementById("light-theme-highlight"),[THEME.DARK]: document.getElementById("dark-theme-highlight"),};[THEME.LIGHT,THEME.DARK].forEach((theme)=>{console.log("Theme: ",theme);if(!ThemeCSS?.[theme]?.["disabled"]){return;}ThemeCSS[theme]!["disabled"]=theme!==add;});localStorage.setItem("theme",add);}functionswitchTheme(){if(!btn){return;}if(currentTheme==THEME.DARK){switchToTheme(THEME.LIGHT);btn.replaceChild(ICONS.light,ICONS.dark);}else{switchToTheme(THEME.DARK);btn.replaceChild(ICONS.dark,ICONS.light);}}if(!btn){return;}btn.addEventListener("click",switchTheme);switchToTheme(currentTheme);btn.appendChild(ICONS[currentTheme]);};document.addEventListener("DOMContentLoaded",loadTheme);