Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a228ebb
feat: add API for switching between light and dark theme variants
Artur- Nov 12, 2025
56cd091
Use short name
Artur- Nov 18, 2025
6e6531b
fix: handle null theme variant in Page.getThemeVariant()
Artur- Nov 18, 2025
9c85778
fix: use native color-scheme property instead of --aura-color-scheme
Artur- Nov 18, 2025
baa12b2
Merge remote-tracking branch 'origin/main' into flow-change-dark
Artur- Nov 18, 2025
2b62645
fix: treat "normal" color-scheme as no variant set
Artur- Nov 18, 2025
fbbb751
test: add integration tests for theme variant API
Artur- Nov 18, 2025
e2a7214
fix: revert waitUntil changes in ThemeVariantIT
Artur- Nov 19, 2025
20e7116
fix: update initialThemeVariant test to handle browser default
Artur- Nov 19, 2025
fa332c8
Merge remote-tracking branch 'origin/main' into flow-change-dark
Artur- Nov 19, 2025
a5b6788
use setThemeVariant
Artur- Nov 19, 2025
9d18083
Move conversion
Artur- Nov 19, 2025
4c20b8d
format
Artur- Nov 19, 2025
be2ea9a
Really use "" for themeVariant
Artur- Nov 19, 2025
2c1e328
Merge remote-tracking branch 'origin/main' into flow-change-dark
Artur- Nov 20, 2025
49d4ef8
feat: add ColorScheme API with enum-based type-safe implementation
Artur- Nov 20, 2025
3af289d
refactor: rename ThemeVariant test files to ColorScheme
Artur- Nov 20, 2025
99669c2
fix: update CSS to use light-dark() function for color scheme
Artur- Nov 20, 2025
60bca58
fix: use theme attribute for color scheme with CSS color-scheme property
Artur- Nov 20, 2025
210eb83
Update comment
Artur- Nov 20, 2025
3c0f595
Merge remote-tracking branch 'origin/main' into flow-change-dark
Artur- Nov 20, 2025
6f18a10
format
Artur- Nov 20, 2025
fb20f9a
fix: set theme attribute in IndexHtmlRequestHandler and update tests
Artur- Nov 20, 2025
8b5bf5a
Merge branch 'main' into flow-change-dark
Artur- Nov 21, 2025
6d3277b
docs: clarify NORMAL color scheme behavior
Artur- Nov 21, 2025
13f6589
fix: set color-scheme property in addition to theme attribute
Artur- Nov 21, 2025
b8a2162
fix: use hyphenated theme attribute for multi-value color schemes
Artur- Nov 21, 2025
7ce5642
Merge branch 'main' into flow-change-dark
Artur- Nov 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions flow-client/src/main/frontend/Flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,20 @@ export class Flow {
params['v-np'] = ($wnd as any).navigator.platform;
}

/* Color scheme from CSS color-scheme property */
const colorScheme = getComputedStyle(document.documentElement).colorScheme.trim();
// "normal" is the default value and means no color scheme is set
params['v-cs'] = colorScheme && colorScheme !== 'normal' ? colorScheme : '';
/* Theme name - detect which theme is in use */
const computedStyle = getComputedStyle(document.documentElement);
let themeName = '';
if (computedStyle.getPropertyValue('--vaadin-lumo-theme').trim()) {
themeName = 'lumo';
} else if (computedStyle.getPropertyValue('--vaadin-aura-theme').trim()) {
themeName = 'aura';
}
params['v-tn'] = themeName;

/* Stringify each value (they are parsed on the server side) */
const stringParams: Record<string, string> = {};
Object.keys(params).forEach((key) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ public ExtendedClientDetails getExtendedClientDetails() {
// Create placeholder with default values
extendedClientDetails = new ExtendedClientDetails(ui, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null, null, null);
null, null, null, null, null, null);
}
return extendedClientDetails;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2000-2025 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.page;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Defines the color scheme for the application using the CSS color-scheme
* property.
* <p>
* This annotation should be placed on a class that implements
* {@link com.vaadin.flow.component.page.AppShellConfigurator} to set the
* initial color scheme for the entire application.
* <p>
* Example usage:
*
* <pre>
* &#64;ColorScheme(ColorScheme.Value.DARK)
* public class AppShell implements AppShellConfigurator {
* }
* </pre>
* <p>
* The color scheme can also be changed programmatically at runtime using
* {@link Page#setColorScheme(ColorScheme.Value)}.
*
* @see Page#setColorScheme(ColorScheme.Value)
* @see Page#getColorScheme()
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@Documented
public @interface ColorScheme {

/**
* The initial color scheme for the application.
*
* @return the color scheme value
*/
Value value() default Value.NORMAL;

/**
* Enumeration of supported color scheme values.
* <p>
* These values correspond to the CSS color-scheme property values and
* control how the browser renders UI elements and how the application
* responds to system color scheme preferences.
*/
enum Value {
/**
* Light color scheme only. The application will use a light theme
* regardless of system preferences.
*/
LIGHT("light"),

/**
* Dark color scheme only. The application will use a dark theme
* regardless of system preferences.
*/
DARK("dark"),

/**
* Supports both light and dark color schemes, with a preference for
* light. The application can adapt to system preferences but defaults
* to light mode.
*/
LIGHT_DARK("light dark"),

/**
* Supports both light and dark color schemes, with a preference for
* dark. The application can adapt to system preferences but defaults to
* dark mode.
*/
DARK_LIGHT("dark light"),

/**
* Normal/default color scheme. Uses the browser's default behavior
* without any specific color scheme preference.
*/
NORMAL("normal");

private final String value;

Value(String value) {
this.value = value;
}

/**
* Gets the CSS color-scheme property value.
*
* @return the CSS value string
*/
public String getValue() {
return value;
}

/**
* Converts a string to a ColorScheme.Value enum.
*
* @param value
* the CSS color-scheme value string
* @return the corresponding enum value, or NORMAL if not recognized
*/
public static Value fromString(String value) {
if (value == null || value.isEmpty()) {
return NORMAL;
}
for (Value v : values()) {
if (v.value.equals(value)) {
return v;
}
}
return NORMAL;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class ExtendedClientDetails implements Serializable {
private double devicePixelRatio = -1.0D;
private String windowName;
private String navigatorPlatform;
private ColorScheme.Value colorScheme = ColorScheme.Value.NORMAL;
private String themeName;

/**
* For internal use only. Updates all properties in the class according to
Expand Down Expand Up @@ -100,14 +102,18 @@ public class ExtendedClientDetails implements Serializable {
* a unique browser window name which persists on reload
* @param navigatorPlatform
* navigation platform received from the browser
* @param colorScheme
* the current color scheme
* @param themeName
* the theme name (e.g., "lumo", "aura")
*/
public ExtendedClientDetails(UI ui, String screenWidth, String screenHeight,
String windowInnerWidth, String windowInnerHeight,
String bodyClientWidth, String bodyClientHeight, String tzOffset,
String rawTzOffset, String dstShift, String dstInEffect,
String tzId, String curDate, String touchDevice,
String devicePixelRatio, String windowName,
String navigatorPlatform) {
String navigatorPlatform, String colorScheme, String themeName) {
this.ui = ui;
if (screenWidth != null) {
try {
Expand Down Expand Up @@ -184,6 +190,8 @@ public ExtendedClientDetails(UI ui, String screenWidth, String screenHeight,

this.windowName = windowName;
this.navigatorPlatform = navigatorPlatform;
setColorScheme(ColorScheme.Value.fromString(colorScheme));
this.themeName = themeName;
}

/**
Expand Down Expand Up @@ -397,6 +405,36 @@ public boolean isIOS() {
&& navigatorPlatform.startsWith("iPod"));
}

/**
* Gets the color scheme.
*
* @return the color scheme, never {@code null}
*/
public ColorScheme.Value getColorScheme() {
return colorScheme;
}

/**
* Gets the theme name.
*
* @return the theme name (e.g., "lumo", "aura"), or empty string if not
* detected
*/
public String getThemeName() {
return themeName;
}

/**
* Updates the color scheme. For internal use only.
*
* @param colorScheme
* the new color scheme
*/
void setColorScheme(ColorScheme.Value colorScheme) {
this.colorScheme = colorScheme == null ? ColorScheme.Value.NORMAL
: colorScheme;
}

/**
* Creates an ExtendedClientDetails instance from browser details JSON
* object. This is intended for internal use when browser details are
Expand Down Expand Up @@ -446,7 +484,9 @@ public static ExtendedClientDetails fromJson(UI ui, JsonNode json) {
getStringElseNull.apply("v-td"),
getStringElseNull.apply("v-pr"),
getStringElseNull.apply("v-wn"),
getStringElseNull.apply("v-np"));
getStringElseNull.apply("v-np"),
getStringElseNull.apply("v-cs"),
getStringElseNull.apply("v-tn"));
}

/**
Expand Down
41 changes: 41 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/component/page/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ public void setTitle(String title) {
ui.getInternals().setTitle(title);
}

/**
* Sets the color scheme for the page.
* <p>
* The color scheme is applied via a theme attribute on the html element,
* allowing CSS to use that attribute to target different color schemes. The
* theme attribute also ensures that browsers apply a color-scheme property
* accordingly.
*
* @param colorScheme
* the color scheme to set (e.g., ColorScheme.Value.DARK,
* ColorScheme.Value.LIGHT), or {@code null} to reset to NORMAL
*/
public void setColorScheme(ColorScheme.Value colorScheme) {
if (colorScheme == null || colorScheme == ColorScheme.Value.NORMAL) {
executeJs("""
document.documentElement.removeAttribute('theme');
document.documentElement.style.colorScheme = '';
""");
getExtendedClientDetails().setColorScheme(ColorScheme.Value.NORMAL);
} else {
executeJs("""
document.documentElement.setAttribute('theme', $0);
document.documentElement.style.colorScheme = '';
""", colorScheme.getValue());
getExtendedClientDetails().setColorScheme(colorScheme);
}
}

/**
* Gets the color scheme for the page.
* <p>
* Note that this method returns the server-side cached value and will not
* detect color scheme changes made directly via JavaScript or browser
* developer tools.
*
* @return the color scheme value, never {@code null}
*/
public ColorScheme.Value getColorScheme() {
return getExtendedClientDetails().getColorScheme();
}

/**
* Adds the given style sheet to the page and ensures that it is loaded
* successfully.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public boolean synchronizedHandleRequest(VaadinSession session,
}

addDevBundleTheme(indexDocument, context);
applyThemeVariant(indexDocument, context);
applyColorScheme(indexDocument, context);

if (config.isDevToolsEnabled()) {
addDevTools(indexDocument, config, session, request);
Expand Down Expand Up @@ -253,8 +253,25 @@ private static void addDevBundleTheme(Document document,
}
}

private void applyThemeVariant(Document indexDocument,
private void applyColorScheme(Document indexDocument,
VaadinContext context) {
// Check for @ColorScheme annotation first
AppShellRegistry registry = AppShellRegistry.getInstance(context);
Class<?> shell = registry.getShell();
if (shell != null) {
com.vaadin.flow.component.page.ColorScheme colorSchemeAnnotation = shell
.getAnnotation(
com.vaadin.flow.component.page.ColorScheme.class);
if (colorSchemeAnnotation != null) {
String colorScheme = colorSchemeAnnotation.value().getValue();
if (!colorScheme.isEmpty() && !colorScheme.equals("normal")) {
indexDocument.head().parent().attr("theme", colorScheme);
}
}
}

// Also apply from deprecated @Theme variant attribute for backwards
// compatibility
ThemeUtils.getThemeAnnotation(context).ifPresent(theme -> {
String variant = theme.variant();
if (!variant.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.vaadin.flow.component.dependency.StyleSheet;
import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.flow.component.page.BodySize;
import com.vaadin.flow.component.page.ColorScheme;
import com.vaadin.flow.component.page.Inline;
import com.vaadin.flow.component.page.Meta;
import com.vaadin.flow.component.page.Push;
Expand All @@ -61,8 +62,9 @@
*/
@HandlesTypes({ AppShellConfigurator.class, Meta.class, Meta.Container.class,
PWA.class, Inline.class, Inline.Container.class, Viewport.class,
BodySize.class, PageTitle.class, Push.class, Theme.class, NoTheme.class,
StyleSheet.class, StyleSheet.Container.class })
BodySize.class, PageTitle.class, Push.class, ColorScheme.class,
Theme.class, NoTheme.class, StyleSheet.class,
StyleSheet.Container.class })
// @WebListener is needed so that servlet containers know that they have to run
// it
@WebListener
Expand Down
6 changes: 6 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/theme/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,15 @@

/**
* The theme variant, if any.
* <p>
* <b>Deprecated:</b> Use {@link com.vaadin.flow.component.page.ColorScheme}
* annotation instead to set the color scheme for the application.
*
* @return the theme variant
* @deprecated Use {@link com.vaadin.flow.component.page.ColorScheme}
* annotation instead
*/
@Deprecated(since = "25.0", forRemoval = true)
String variant() default "";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public void initializeWithClientValues_gettersReturnExpectedValues() {
Assert.assertEquals(2.0D, details.getDevicePixelRatio(), 0.0);
Assert.assertEquals("ROOT-1234567-0.1234567", details.getWindowName());
Assert.assertFalse(details.isIPad());
Assert.assertEquals(ColorScheme.Value.LIGHT, details.getColorScheme());
Assert.assertEquals("aura", details.getThemeName());

// Don't test getCurrentDate() and time delta due to the dependency on
// server-side time
Expand Down Expand Up @@ -161,14 +163,16 @@ private class ExtendBuilder {
private String devicePixelRatio = "2.0";
private String windowName = "ROOT-1234567-0.1234567";
private String navigatorPlatform = "Linux i686";
private String colorScheme = "light";
private String themeName = "aura";

public ExtendedClientDetails buildDetails() {
return new ExtendedClientDetails(null, screenWidth, screenHeight,
windowInnerWidth, windowInnerHeight, bodyClientWidth,
bodyClientHeight, timezoneOffset, rawTimezoneOffset,
dstSavings, dstInEffect, timeZoneId, clientServerTimeDelta,
touchDevice, devicePixelRatio, windowName,
navigatorPlatform);
navigatorPlatform, colorScheme, themeName);
}

public ExtendBuilder setScreenWidth(String screenWidth) {
Expand Down
Loading
Loading