Mayil-AI-Sandbox / eslint-v911

Clone of eslint v9.1.1 for debugging
MIT License
0 stars 0 forks source link

Docs: Option to use system preference for light/dark theme #8

Open btmills opened 1 month ago

btmills commented 1 month ago

Docs page(s)

all of them

What documentation issue do you want to solve?

The ESLint docs currently default to the browser's prefers-color-scheme setting for light/dark mode by default. And they have a Light/Dark selector to let the user specify an override.

But, there's no way to remove the override: i.e. go back to the default behavior of using the browser/system preference. This feels like a bug / missing feature to me: what if the user wants go back to the system preference?

I've hit this where I've needed to change the page temporarily, but subsequently wanted to go back to synced with the browser/system preferences.

What do you think is the correct solution?

A few UI options come to mind, in order of what I think is most intuitive & usable first:

The current selector, for reference: Screenshot of the ESLint docs Light/Dark buttons, with light mode selected

Participation

Additional comments

Sibling issue to #18189.

mayil-ai[bot] commented 1 month ago

To resolve the issue with the ESLint documentation website's theme selector, follow these steps:

  1. Add a new button for the "System" theme option in the theme-switcher HTML markup:

    <button id="system-theme-toggle" data-theme="system">System</button>
  2. Modify the themes.js script to handle the "System" theme option:

    • Introduce an event listener for the "System" theme button.
    • Implement functionality to remove the data-theme attribute from the document.documentElement and clear the theme item from localStorage when the "System" theme is selected.
    • Adjust the enableToggle and disableToggle functions to manage the "System" theme state.

Here is the updated code for themes.js:

document.addEventListener('DOMContentLoaded', function() {
    // ... existing code ...

    var system_theme_toggle = document.getElementById('system-theme-toggle');

    system_theme_toggle.addEventListener("click", function() {
        document.documentElement.removeAttribute('data-theme');
        window.localStorage.removeItem("theme");

        disableToggle(light_theme_toggle);
        disableToggle(dark_theme_toggle);
        enableToggle(system_theme_toggle);
    }, false);

    // ... existing code ...

    var enableToggle = function(btn) {
        btn.setAttribute("aria-pressed", "true");
        if (btn === system_theme_toggle) {
            disableToggle(light_theme_toggle);
            disableToggle(dark_theme_toggle);
        }
    }

    var disableToggle = function(btn) {
        btn.setAttribute("aria-pressed", "false");
        if (btn !== system_theme_toggle && light_theme_toggle.getAttribute("aria-pressed") === "false" && dark_theme_toggle.getAttribute("aria-pressed") === "false") {
            enableToggle(system_theme_toggle);
        }
    }

    // ... existing code ...
}, false);

By implementing these changes, users will be able to select the "System" theme option, which will allow the website to automatically match the system's light or dark mode preference.

Code snippets to check