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 mode/dark mode auto switching. #2644

Open
ioquatix opened this issue Jan 17, 2022 · 5 comments
Open

Light mode/dark mode auto switching. #2644

ioquatix opened this issue Jan 17, 2022 · 5 comments
Labels
Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect

Comments

@ioquatix
Copy link

ioquatix commented Jan 17, 2022

It's possible (supported in Chrome but not Safari) to use prefers-color-scheme in SVG documents.

With careful styling, this can be used to achieve diagram styling which responds to user preference.

As a fallback, we can detect changes to prefers-color-scheme and refresh SVGs.

This would make the out-of-the-box light/dark styling much more seamless. As of now, the experience is less than ideal and the presentation of diagrams in a website which uses prefers-color-scheme is poor/unusable.

Would this be of interest?

@ioquatix ioquatix added Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect labels Jan 17, 2022
@sidharthv96
Copy link
Member

In mermaid.live, we are currently switching the theme in config based on the color scheme.

@ioquatix
Copy link
Author

ioquatix commented Feb 4, 2022

Yes, it's a reasonable approach for browsers which don't support prefers-color-scheme in SVG documents. Eventually however I believe it will be supported in all browsers.

Can you explain how someone can integrate mermaid.live to achieve this? Thanks!

@hemeroc
Copy link

hemeroc commented Jun 30, 2023

@sidharthv96 I would also be interested in how mermaid.live does it

@myl7
Copy link

myl7 commented Jun 7, 2024

IMO it is kinda more complicated.

First, websites have various methods to switch light/dark mode. In addition to media query prefers-color-scheme, class dark is also widely used to toggle it, e.g., in Next.js, so that users can override the browser default.
It is hard for Mermaid to choose one (or maybe we need lots of buttons for every methods).

Second, embedding styles for auto light/dark mode switching in SVG documents doesnot always work.
E.g., after image optimization of Next.js / Astro, SVGs are loaded via <img src="..."> and thus cannot switch light/dark mode with styles. Only loading SVGs by embedding them as HTML elements can make the styles work.

Anyway, if you use class dark for dark mode and embedded SVGs as HTML elements, you can replace the content of the second <style> element in SVG documents as https://gist.github.com/myl7/c93d000876ec76da8d23b0ccc9cbcb6e to implement auto light/dark mode switching. Ones using media query prefers-color-scheme can modify the CSS correspondingly.

@welpo
Copy link

welpo commented Aug 27, 2024

As a simple solution to support both prefers-color-scheme and a manual toggle, I used the invert filter targeting mermaid diagrams (demo):

[data-theme='dark'] .mermaid {
    filter: invert(0.88);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme='light']) .mermaid {
        filter: invert(0.88);
    }
}

This CSS does two things:

  1. It applies an invert filter to diagrams when a data-theme='dark' attribute is set on an ancestor element (my theme switcher updates the html tag).
  2. It also applies the invert filter when the user's system is set to dark mode (prefers-color-scheme: dark), unless a light theme is explicitly set.

Note: I'm using invert(0.88) instead of invert(1) because a full invert looked too harsh to my eyes —different values will work better on different websites.

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect
Projects
None yet
Development

No branches or pull requests

5 participants