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

Is the way dark-light-theme selection is implemented part of the public API? #1764

Closed
RunDevelopment opened this issue Oct 25, 2021 · 3 comments
Labels
question Question about functionality

Comments

@RunDevelopment
Copy link
Contributor

Search terms

theme, API, dark, light

Question

Currently, selecting the dark, light, or OS theme is done by adding special classes (dark or light) to the <body> and by using @media queries if those special classes aren't present.

Is this way of doing things part of the public API? Or in other words, can I rely on this behavior?

Motivation

Some plugins need to know this.

Example: kamiazya/typedoc-plugin-mermaid#456. This plugin renders Mermaid graphs in comments. Mermaid always renders graphs into a standalone SVG with a specific Mermaid theme backed in. My idea is to render two versions of the same graph (one for dark mode and one for light mode) and use CSS to switch between the two (making it so that only one of them is visible at a time depending on dark mode). However, I need a CSS way for detecting light/dark mode to do that.

@RunDevelopment RunDevelopment added the question Question about functionality label Oct 25, 2021
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Oct 26, 2021

Yes - I consider this part of the public API, but it will change in 0.23, most likely to use the method in #1706.

@RunDevelopment
Copy link
Contributor Author

Thank you for the info.


Regarding #1706: This solution still seems suboptimal to me.

Let's say I simply want to set some CSS variables --foo and --bar for my plugin depending on the color scheme. I would have to do it like this:

@media (prefers-color-scheme: light) {
    :root {
        --foo: white;
        --bar: lightgreen;
    }
}
@media (prefers-color-scheme: dark) {
    :root {
        --foo: black;
        --bar: darkgreen;
    }
}

:root[data-theme="light"] {
    --foo: white;
    --bar: lightgreen;
}
:root[data-theme="dark"] {
    --foo: black;
    --bar: darkgreen;
}

I need 2 copies of all variable assignments per color scheme.

I wonder if you could get rid of the @media queries that cause this code duplication. Couldn't we assign data-theme before the <body> loads using a little script in <head> like this:

document.documentElement.dataset.theme = 
    window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Oct 27, 2021

That works - for people who have JavaScript turned on. It won't work for anyone with JS turned off.

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

No branches or pull requests

2 participants