Skip to content

Proposal: option to drop data-theme in favor of prefers-color-scheme #9579

Closed as not planned

Description

@SethFalco

Have you read the Contributing Guidelines on issues?

Description

It would be valuable for Docusaurus to provide an option that disables data-theme entirely, and drops all attributes and JavaScript for it, in favor of solely using the prefers-color-scheme property.

Has this been requested on Canny?

N/A

Motivation

In the following scenario, it's best to drop data-theme altogether:

  • the website uses prefers-color-scheme
  • does not provide an on-site toggle
  • light mode and dark mode are the only color schemes available

Some developers, such as myself, don't provide an on-site toggle for changing theme. Instead, we guide users to their browser or operating system settings if it's supported.

  • This also improves theming when JavaScript is disabled, whereas currently Docusaurus and similar tool chains are locked into light mode when JavaScript is disabled.
  • Avoids any messiness or disputes regarding if the ePrivacy Directive applies to this use of localStorage to store theming preferences.

This option would introduce a tradeoff that developers would have to consider:

  • current behavior, clients with JavaScript disabled can't access dark mode
  • proposed option, clients on old software can't access dark mode

API design

I think the best way to do this is to perform the action implicitly based on the current config, no API change is necessary.

In the color mode config, if disableSwitch: true and respectPrefersColorScheme: true are configured, then implicitly perform this behavior, where all styling is applied through prefers-color-scheme, not [data-theme=dark].

export default {
  themeConfig: {
    colorMode: {
      disableSwitch: true,
      respectPrefersColorScheme: true,
    }
  }
};

Have you tried building it?

I have reviewed the Docusaurus repo. Skipping the script that adds the theming script at the top of the <body> is easy.

The part I'm not sure of is how to approach selectors for [data-theme='dark'] in the code. I'm thinking this would be easiest as a post step with PostCSS or webpack to convert them to @media (prefers-color-scheme: dark), but I have not done this yet.

Self-service

  • I'd be willing to contribute this feature to Docusaurus myself.

Activity

  1. added
    feature
    status: needs triage
    on Nov 24, 2023
  2. removed
    status: needs triage
    on Nov 24, 2023
  3. changed the title [-]feat: option to drop data-theme in favor of prefers-color-scheme[/-] [+]Proposal: option to drop data-theme in favor of prefers-color-scheme[/+] on Nov 24, 2023
  4. slorber commented on Nov 24, 2023

    @slorber
    Collaborator

    That seems a bit complicated for limited usefulness.

    Who disables JS these days? I mean it can fail to load ok (but in our case it's inlined) but if you disable it on purpose you can expect 99.9% of the internet to not work as good as if it were enabled 😅

    Who has a concrete use-case for this, apart theorically? Do you browse internet using a very old browser (which we don't support) or with JS disabled?

  5. SethFalco commented on Nov 24, 2023

    @SethFalco
    ContributorAuthor

    Who disables JS these days?

    Not many people, admittedly. However, as a participant in privacy spaces, I have encountered users who browse with JavaScript disabled by default (i.e. with uBlock Origin rather than the browser setting) and only enable it for sites they trust.

    Who has a concrete use-case for this

    The only group I can think of, and a reference to the above, are individuals who are protective around privacy/tracking.

    Do you browse internet using a very old browser (which we don't support) or with JS disabled?

    I, personally, do not disable JavaScript myself.

    I have considered it, but as you've said, some sites don't work well, and many don't work whatsoever. However, this can also explain why I'd value building better JavaScript-less experiences in web apps, since I'd like to see sites run better without JavaScript.


    In general, I agree it's niche and might be more work than it's worth. If you aren't into it, I'm happy to look externally to get this done.

    I thought it would be great to have this baked into Docusaurus because it already has a (mostly) good JavaScript-less experience, so I assumed it was one of the goals. It's actually one of the reasons I tried it out.

    If JavaScript-less browsing hasn't been a focus, I'm actually surprised and impressed with how well it works. ^-^'

  6. slorber commented on Nov 25, 2023

    @slorber
    Collaborator

    Thanks for the explanations.

    Yes working without JS has been a focus for most features (notably navigation), and we kept the hardest cases for later.

    I guess we can close as duplicate of the existing "no js support" issue: #3030
    Similarly I'd like the theme toggle to work with JS disabled (probably using a checkbox + new :has() selector could work)

  7. added
    closed: duplicate
    on Nov 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed: duplicatefeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions