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

Dark theme from CSS media query #447

Open
cloakedninjas opened this issue Jun 7, 2024 · 4 comments
Open

Dark theme from CSS media query #447

cloakedninjas opened this issue Jun 7, 2024 · 4 comments

Comments

@cloakedninjas
Copy link

@josdejong What are your thoughts on providing the dark theme via @media (prefers-color-scheme: dark) instead of having to add the jse-theme-dark class?

@josdejong
Copy link
Owner

josdejong commented Jun 7, 2024

If there is an easier or more flexible way to work with a custom theme I'm in for it.

What I like about the current solution is that you can programmatically control what theme is used by adding/remove a class name. So you can toggle not only dark theme, but any custom theme.

Is there a way to allow for both? I.e. be able to load the dark theme CSS and have control over whether to use it or not both via JS (toggling a classs) and via CSS (like using @media)?

@cloakedninjas
Copy link
Author

cloakedninjas commented Jun 7, 2024

So given the scenario that the user has their device set to dark theme, but visits a website that embeds jsoneditor and toggle it to light theme - then we would need a second CSS class .jse-theme-light and it would need to be formed something like this:

@media (prefers-color-scheme: dark) {
 .jse-theme-light {
    --jse-theme: light;

    /* over all fonts, sizes, and colors */
    --jse-theme-color: #FFF;
   
    ... rest of theme variables
  }
}

The app I currently work on does this, we just let the OS / browser guide us to which theme to render, but we provide our users a way to render the app in a specific theme from their preference page.

If you wanted, this feature could be provided in a separate CSS file that developers opt-in to e.g.

@import 'node_modules/vanilla-jsoneditor/themes/jse-theme-auto.css';

... But given the widespread adoption of light / dark themes in at the OS level and browser level... I'm not sure you'd want to go against the grain.

@josdejong
Copy link
Owner

I'm totally for using @media (prefers-color-scheme: dark). What I mostly see in practice is giving people three options: light, dark, or system default. How can we cater for that?

I was reading up on the following two articles that describe how to achieve this:

They both come up with about the same solution to allow toggling system/light/dark, see this example "Overriding OS Settings". However the solution involves repeating all theme variables twice. I'm not sure yet how we could prevent that and provide a `jse-theme-dark.css that supports both being loaded based on either the system preference or a choice. Any ideas?

@josdejong
Copy link
Owner

Any idea on how to go about this?

Maybe we should deliver two different files, one for each purpose, and have an internal built tool that can generate one from the other.

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

No branches or pull requests

2 participants