-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Add dark mode support to the HTML versions of the OpenAPI specs #4268
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @pavelkornev points out in #3866 (comment) this only works on my Mac if the system appearance is set to Dark. If it is set to Light, or set to Auto and is currently Light, then the switch doesn't work.
@ralfhandl Oh yes... I had encapsulated the code under the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On MacOS only works if system is in dark mode.
@ralfhandl That's weird.. I am not getting the same results as you. I am testing it on a MacOS and it perfectly works with both Chrome and Safari. Could it be a browser setting? I will further look into it. |
@ralfhandl I have managed to reproduce it with Firefox. The problem is that respec adds this link <link rel="stylesheet" media="(prefers-color-scheme: dark)" href="https://www.w3.org/StyleSheets/TR/2021/dark.css"> Once again, due to the Although, this seems to be fixing the issue it still loads unnecessarily the Note: I will fix the tests after we agree on the solution. |
Is that necessary? I'd prefer to keep our CSS as slim as possible. |
Why not, if that works better? |
@swaldron58 can you check if our theme fits this? |
@ralfhandl I think so. The implementation depends on how Respec handles dark mode, which is by adding the
@ralfhandl I have changed the UPDATE: /* Dark mode toggle */
const darkCss = document.querySelector('link[rel~="stylesheet"][href^="https://www.w3.org/StyleSheets/TR/2021/dark"]');
if (darkCss) {
const colorScheme = localStorage.getItem("tr-theme") || "auto";
const browserDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
const theme = colorScheme === "auto" ? (browserDarkMode ? "dark" : "light") : colorScheme;
darkCss.disabled = theme === "light";
darkCss.media = theme === "dark" ? "(prefers-color-scheme: dark)" : ""; Since the code runs only when the stylesheet exist, the dark mode toggle does not appear. To bypass this we would need to add our own javascript to replicate this behavior. |
Closes #3866
Except from adding the necessary CSS rules I have also moved it into its own file here: b3949bd
This declutters a little bit the
md2html.js
. Plus, we can also use the IDE for styling and formatting.The CSS is heavily inspired by the work of @pavelkornev, see #3866 (comment)
End results: https://jsfiddle.net/gu23x9kb/