Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect the toggle for light/dark mode #57

Open
pihomeserver opened this issue Mar 25, 2023 · 0 comments
Open

Detect the toggle for light/dark mode #57

pihomeserver opened this issue Mar 25, 2023 · 0 comments

Comments

@pihomeserver
Copy link

One suggestion to be adapted : i'm using the theme for my website but i need to adapt my logo when the user switch from light to dark and dark to light so i have updated the theme index.js file :

  function changeMode(isDarkMode) {
    if(isDarkMode) {
      bank.setItem(storageKey, light)
      elemAttribute(doc, data, light);
    } else {
      bank.setItem(storageKey, dark);
      elemAttribute(doc, data, dark);
    }
    window.dispatchEvent(new Event("storage"));
  }

The event is not triggered when staying on the same page
Then in the header partial :

...
        <script>
          const storageKey = 'colorMode';
          const bank = window.localStorage;
          window.onstorage = () => {
            var logoElt = document.getElementById("logo");
            if (logoElt && bank.getItem(storageKey) === "dim") {
              logoElt.setAttribute("style", "filter: invert();");
            } else {
              logoElt.setAttribute("style", "filter: none;");
            }
          };
        </script>
...

where logo is the img element of my logo

Not sure it's the best approach but could help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant