222-responsive-icon-nav-css icon indicating copy to clipboard operation
222-responsive-icon-nav-css copied to clipboard

Themeing Option Fix

Open alteredorange opened this issue 5 years ago • 1 comments

Awesome menu! Everything worked great except for themes, because local storage is never set, so it's always empty. If you check for local storage first, then set it to a default, it works like champ:

const themeMap = {
    dark: "light",
    light: "solar",
    solar: "dark"
};

if (localStorage.theme) {
    const theme = localStorage.getItem("theme");
    const bodyClass = document.body.classList;
    theme && bodyClass.add(theme);
    function toggleTheme() {
        const current = localStorage.getItem("theme");
        const next = themeMap[current];

        bodyClass.replace(current, next);
        localStorage.setItem("theme", next);
    }
} else {
    localStorage.setItem("theme", "light");
}

document.getElementById("themeButton").onclick = toggleTheme;

Thanks for the great videos!

alteredorange avatar Mar 03 '20 21:03 alteredorange

There is nothing wrong with the theme.js file. To use it, all you need to do is after the

tag, create another

tag, and link the theme.js file there.

mohammadr09 avatar Jun 28 '22 13:06 mohammadr09