Is it good to change the tagname of a DOM – AGamePlayer Aug 21 '21 at 01:31

  • 1
    One idea is to use a class on the `body` that indicates dark mode and can be added/removed/toggled with JavaScript. Other declarations can depend on that class, something like `body.dark .another-element { ... }`. I think it might help to see more specifics about your code, including a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) if possible. – showdev Aug 21 '21 at 01:35
  • Thanks, I'll check it out and see whether I can adapt my code to that style. – AGamePlayer Aug 21 '21 at 01:40
  • I would suggest using two different css files, its easier to manage and have more control over your future changes. Think of it as template, as there may even exist images, html etc – Alen.Toma Aug 21 '21 at 02:50
  • 1 Answers1

    1

    No this is definitively not the proper way to do this.

    The HTMLStyleElement (<style>) has a .disabled property for this task:

    const button = document.querySelector("button");
    const style = document.querySelector("style");
    
    button.onclick = (evt) => style.disabled = !style.disabled;
    button { color: red; }
    <button>click me to toggle color</button>

    And even <link> elements have that property.

    const button = document.querySelector("button");
    
    // creates a <link> element pointing to a stylesheet
    // containing the single rule "button { color: red; }"
    const link = document.createElement("link");
    link.rel = "stylesheet";
    link.href = URL.createObjectURL(new Blob([`button { color: red; }`], { type: "text/css" }));
    link.onload = (evt) => URL.revokeObjectURL(link.href);
    document.head.append(link);
    
    // works with <link> too
    button.onclick = (evt) => link.disabled = !link.disabled;
    <button>click me to toggle color</button>

    An other way would be to add a class on a common ancestor, and check for the presence or absence of such class in every rules, but this will force you to repeat a lot of rules. Might still be useful nevertheless.

    const select = document.querySelector("select");
    select.onchange = (evt) => {
      document.body.classList.remove("shade-auto", "light", "dark");
      document.body.classList.add(select.value);
    };
    document.body.classList.add(select.value);
    body.light {
      background: white;
      color: black;
    }
    body.light select {
      background: #EEE;
      color: #222;
    }
    body.dark {
      background: #222;
      color: #eee;
    }
    body.dark select {
      color: #eee;
      background: #333;
    }
    
    @media (prefers-color-scheme: light) {
      body.shade-auto {
        background: white;
        color: black;
      }
    }
    @media (prefers-color-scheme: dark) {
      body.shade-auto {
        background: #222;
        color: #eee;
      }
      body.shade-auto select {
        color: #eee;
        background: #333;
      }
    }
    <select>
      <option value="shade-auto">Auto (respect OS)</option>
      <option value="light">Force Light mode</option>
      <option value="dark">Force Dark mode</option>
    </select>
    Foo bar
    Kaiido
    • 123,334
    • 13
    • 219
    • 285