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

light/dark color scheme toggle #7378

Open
moellep opened this issue Nov 25, 2024 · 1 comment
Open

light/dark color scheme toggle #7378

moellep opened this issue Nov 25, 2024 · 1 comment

Comments

@moellep
Copy link
Member

moellep commented Nov 25, 2024

Apps which support the dark color scheme should have a toggle where the system default can be overridden. The dark model config should be changed to set the color-scheme value like the example below:

<html lang="en" data-color-scheme="dark">
<head>
  <style type="text/css">
[data-color-scheme="dark"] {
  color-scheme: dark;
}
[data-color-scheme="light"] {
  color-scheme: light;
}
  </style>
</head>
<body>
  <div>
    <button id="light">light</button>
    <button id="dark">dark</button>
    <button id="system">system</button>
    <div style="margin-top: 3ex">
      <input type="datetime-local" required />
      <br><br>
      <input type="text" placeholder="dummy input" />
    </div>
  </div>
  <script>
    (() => {
        let scheme = "system";
        function systemColorScheme() {
            if (window.matchMedia) {
                return window.matchMedia('(prefers-color-scheme: dark)').matches
                     ? "dark" : "light";
            }
            return "light";
        }
        function setColorScheme(newScheme) {
            scheme = newScheme;
            document.documentElement.setAttribute(
                "data-color-scheme",
                scheme === "system" ? systemColorScheme() : scheme,
            );
        }
        if (window.matchMedia) {
            // watch for global changes to color scheme
            window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
                console.log('CHANGED, current pref: ', scheme);
                if (scheme === "system") {
                    setColorScheme("system");
                }
            });
        }
        setColorScheme(scheme);
        document.querySelector("#light").addEventListener("click", () => {
            setColorScheme("light");
        });
        document.querySelector("#dark").addEventListener("click", () => {
            setColorScheme("dark");
        });
        document.querySelector("#system").addEventListener("click", () => {
            setColorScheme("system");
        });
    })();
  </script>
</body>
</html>
@robnagler
Copy link
Member

Looks good to me. id's are in a global flat space. These should be prefixed.

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

2 participants