Skip to content

Commit 8a3f23f

Browse files
authored
feat: add ColorScheme.Value.SYSTEM for intuitive system preference support (#22924)
Add SYSTEM enum constant to ColorScheme.Value that provides a more intuitive name for developers wanting to follow system color scheme preferences. This addresses the confusion around LIGHT_DARK and DARK_LIGHT naming. SYSTEM maps to the CSS value "light dark" (same as LIGHT_DARK), supporting both light and dark modes while defaulting to light when system preference cannot be determined. Fixes #22891
1 parent c6ceba2 commit 8a3f23f

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

flow-server/src/main/java/com/vaadin/flow/component/page/ColorScheme.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Example usage:
3434
*
3535
* <pre>
36-
* &#64;ColorScheme(ColorScheme.Value.DARK)
36+
* &#64;ColorScheme(ColorScheme.Value.SYSTEM)
3737
* public class AppShell implements AppShellConfigurator {
3838
* }
3939
* </pre>
@@ -98,7 +98,20 @@ enum Value {
9898
* system preferences, or other meta tags like
9999
* {@code <meta name="color-scheme" content="dark">}.
100100
*/
101-
NORMAL("normal");
101+
NORMAL("normal"),
102+
103+
/**
104+
* Use the system's preferred color scheme. Supports both light and dark
105+
* modes, adapting to operating system or browser preferences. Defaults
106+
* to light mode when system preference cannot be determined.
107+
* <p>
108+
* This provides a more intuitive name than LIGHT_DARK for the common
109+
* use case of following system preferences. It is functionally
110+
* equivalent to LIGHT_DARK, both mapping to the CSS value "light dark".
111+
*
112+
* @see #LIGHT_DARK
113+
*/
114+
SYSTEM("light dark");
102115

103116
private final String value;
104117

flow-server/src/test/java/com/vaadin/flow/component/HasStyleTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ public void colorScheme_roundtripWorks() {
290290
HasStyleComponent component = new HasStyleComponent();
291291

292292
for (ColorScheme.Value value : ColorScheme.Value.values()) {
293-
if (value == ColorScheme.Value.NORMAL) {
294-
continue; // NORMAL clears the property
293+
if (value == ColorScheme.Value.NORMAL
294+
|| value == ColorScheme.Value.SYSTEM) {
295+
continue; // NORMAL clears the property, SYSTEM sets LIGHT_DARK
295296
}
296297
component.getStyle().setColorScheme(value);
297298
Assert.assertEquals("Roundtrip failed for " + value, value,

flow-server/src/test/java/com/vaadin/flow/component/page/ColorSchemeTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void getValue_returnsCorrectValue() {
2929
Assert.assertEquals("dark light",
3030
ColorScheme.Value.DARK_LIGHT.getValue());
3131
Assert.assertEquals("normal", ColorScheme.Value.NORMAL.getValue());
32+
Assert.assertEquals("light dark", ColorScheme.Value.SYSTEM.getValue());
3233
}
3334

3435
@Test
@@ -44,6 +45,8 @@ public void getThemeValue_multiValue_replacesSpaceWithHyphen() {
4445
ColorScheme.Value.LIGHT_DARK.getThemeValue());
4546
Assert.assertEquals("dark-light",
4647
ColorScheme.Value.DARK_LIGHT.getThemeValue());
48+
Assert.assertEquals("light-dark",
49+
ColorScheme.Value.SYSTEM.getThemeValue());
4750
}
4851

4952
@Test
@@ -75,4 +78,15 @@ public void fromString_unrecognizedValue_returnsNormal() {
7578
Assert.assertEquals(ColorScheme.Value.NORMAL,
7679
ColorScheme.Value.fromString("light-dark"));
7780
}
81+
82+
@Test
83+
public void fromString_lightDark_returnsLightDarkNotSystem() {
84+
// Ensure backward compatibility: parsing "light dark" returns
85+
// LIGHT_DARK
86+
Assert.assertEquals(ColorScheme.Value.LIGHT_DARK,
87+
ColorScheme.Value.fromString("light dark"));
88+
// SYSTEM and LIGHT_DARK should be functionally equivalent
89+
Assert.assertEquals(ColorScheme.Value.LIGHT_DARK.getValue(),
90+
ColorScheme.Value.SYSTEM.getValue());
91+
}
7892
}

0 commit comments

Comments
 (0)