Skip to content

Commit b5b552e

Browse files
Timo Meinenjavier-godoy
andcommitted
fix: ensure dense theme CSS is included in production bundle
Add setDenseTheme(Grid, boolean) and isDenseTheme(Grid) methods that create a bytecode reference to GridHelper, ensuring the Vaadin production bundle scanner discovers its @CssImport annotations. The DENSE_THEME constant is a compile-time constant (static final String), so javac inlines it at the call site. When a consumer only uses the constant (e.g. grid.addThemeName(GridHelper.DENSE_THEME)), no bytecode reference to GridHelper is emitted and the scanner never reaches it. DENSE_THEME is deprecated in favor of the new methods. Closes #171 Co-authored-by: Javier Godoy <11554739+javier-godoy@users.noreply.github.com>
1 parent fae6e95 commit b5b552e

4 files changed

Lines changed: 43 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ The class `GridHelper` provides several static methods that receive a `Grid` or
8484

8585
```
8686
grid.setSelectionMode(SelectionMode.MULTI);
87-
grid.addThemeName(GridHelper.DENSE_THEME);
87+
GridHelper.setDenseTheme(grid, true);
8888
GridHelper.setSelectOnClick(grid, true);
8989
GridHelper.setArrowSelectionEnabled(grid, true);
9090
GridHelper.setSelectionColumnHidden(grid, true);

src/main/java/com/flowingcode/vaadin/addons/gridhelpers/GridHelper.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,45 @@ public final class GridHelper<T> implements Serializable {
7878

7979
/** Compact row styling for Vaadin Grid */
8080
// https://cookbook.vaadin.com/grid-dense-theme
81-
public static final String DENSE_THEME = "fcGh-dense";
81+
private static final String DENSE_THEME_NAME = "fcGh-dense";
82+
83+
/**
84+
* Compact row styling for Vaadin Grid.
85+
*
86+
* @deprecated Use {@link #setDenseTheme(Grid, boolean)} instead. Direct use of this constant
87+
* bypasses the bytecode reference to {@code GridHelper}, which prevents the Vaadin production
88+
* bundle scanner from discovering the required {@code @CssImport} annotations.
89+
*/
90+
@Deprecated(since = "2.1.0", forRemoval = true)
91+
public static final String DENSE_THEME = DENSE_THEME_NAME;
92+
93+
/**
94+
* Adds or removes compact row styling on the given grid.
95+
*
96+
* <p>Prefer this method over {@code grid.addThemeName(GridHelper.DENSE_THEME)} because it
97+
* creates a bytecode reference to {@code GridHelper}, ensuring that the Vaadin production
98+
* bundle scanner discovers the required {@code @CssImport} annotations.
99+
*
100+
* @param grid the grid to style
101+
* @param dense {@code true} to enable dense theme, {@code false} to remove it
102+
*/
103+
public static void setDenseTheme(Grid<?> grid, boolean dense) {
104+
if (dense) {
105+
grid.addThemeName(DENSE_THEME_NAME);
106+
} else {
107+
grid.removeThemeName(DENSE_THEME_NAME);
108+
}
109+
}
110+
111+
/**
112+
* Returns whether the dense theme is currently applied to the given grid.
113+
*
114+
* @param grid the grid to check
115+
* @return {@code true} if the dense theme is applied
116+
*/
117+
public static boolean isDenseTheme(Grid<?> grid) {
118+
return grid.hasThemeName(DENSE_THEME_NAME);
119+
}
82120

83121
@Getter(value = AccessLevel.PACKAGE)
84122
private final Grid<T> grid;

src/test/java/com/flowingcode/vaadin/addons/gridhelpers/AllFeaturesDemo.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,11 @@ private boolean hasSelectionFilter(Grid<Person> grid) {
263263
}
264264

265265
private void setDenseTheme(Grid<Person> grid, boolean value) {
266-
if (value) {
267-
grid.addThemeName(GridHelper.DENSE_THEME);
268-
} else {
269-
grid.removeThemeName(GridHelper.DENSE_THEME);
270-
}
266+
GridHelper.setDenseTheme(grid, value);
271267
}
272268

273269
private boolean hasDenseTheme(Grid<Person> grid) {
274-
return grid.hasThemeName(GridHelper.DENSE_THEME);
270+
return GridHelper.isDenseTheme(grid);
275271
}
276272

277273
private void setHeaderHidden(Grid<Person> grid, boolean value) {

src/test/java/com/flowingcode/vaadin/addons/gridhelpers/DenseThemeDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public DenseThemeDemo() {
4444
grid.addColumn(Person::getLastName).setHeader("Last name");
4545
grid.addColumn(Person::getCountry).setHeader("Country");
4646

47-
grid.addThemeName(GridHelper.DENSE_THEME);
47+
GridHelper.setDenseTheme(grid, true);
4848

4949
grid.setHeightFull();
5050
add(grid);

0 commit comments

Comments
 (0)