I’m not a fan of dark themes because I always work in a bright environment.
I noticed that I can switch the theme of https://vaadin.com (well it doesn’t look pretty because it’s probably not optimized for it)
<html lang="en" theme="light" class="light">
It would be great to be able to switch the theme with a switch 
1 Like
maneesh2
(maneesh godbole)
2
I use this in my project
Feel free to customize as per your requirements
public class ThemeModeSelector extends Button {
private enum ThemeModeOption {
DARK, LIGHT, SYSTEM
}
public ThemeModeSelector() {
super(VaadinIcon.ADJUST.create());
this.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
this.setTooltipText("Select mode");
this.setAriaLabel(this.getTooltip().getText());
var selector = new ListBox<ThemeModeOption>();
selector.setItems(ThemeModeOption.DARK, ThemeModeOption.LIGHT, ThemeModeOption.SYSTEM); // Populate the selector
selector.setValue(ThemeModeOption.SYSTEM); // Default selection
// Handle user selection
selector.addValueChangeListener(event -> {
ThemeModeOption selected = event.getValue();
switch (selected) {
case DARK -> setTheme(true);
case LIGHT -> setTheme(false);
case SYSTEM -> applySystemTheme();
}
});
var popover = new Popover();
popover.setTarget(this);
popover.addThemeVariants(PopoverVariant.ARROW,PopoverVariant.LUMO_NO_PADDING);
popover.add(selector);
}
private void applySystemTheme() {
UI ui = UI.getCurrent();
// Query browser for the current OS theme
ui.getPage().executeJs(
"return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';"
).then(String.class, osTheme -> {
// Apply OS theme to UI
setTheme(Lumo.DARK.equals(osTheme));
});
}
private void setTheme(boolean dark) {
var js = "document.documentElement.setAttribute('theme', $0)";
getElement().executeJs(js, dark ? Lumo.DARK : Lumo.LIGHT);
}
}
Jouni1
(Jouni Koivuviita)
3
Simon was asking for a light-dark switch for the vaadin.com website 
And no, vaadin.com is not build with Vaadin/Flow. But thanks Maneesh for sharing that component in any case. Might come in handy for plenty of others.
1 Like