Automatic Color Scheme

In our app we want to offer three options for color-scheme selection.
Light, Dark and Auto (which would apply the OS-Level Theme.

We use JS to do that, but we would really like to do this using Java Only. Is there something in the works in this direction or should we be happy with our js solution?

What is really important is, that you would also apply the theme change via the event listener. This lets us easily switch themes via the os settings, for faster development.

What you also should consider is applying the css color-scheme. this lets us easily define two colors for light and for dark-scheme.

it’s a newer css feature, but support should be pretty good by now.

JS:

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
	if (event.matches) {
		document.body.setAttribute("theme", "dark");
	} else {
		document.body.removeAttribute("theme");
	}
});
			
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
	document.body.setAttribute("theme", "dark");
} else {
	document.body.removeAttribute("theme");
}
1 Like

Take a look at Theming system renewal · Issue #7453 · vaadin/platform · GitHub - especially “Lumo refactored to support light/dark variants through color-scheme CSS property.”

Nice, thank you Christian, this looks very promising. Hopefully this will be available in the next major Vaadin version. :crossed_fingers: