Skip to content

Commit 4749d5f

Browse files
DiegoCardosoZheSun88sissbruecker
authoredFeb 25, 2025
feat: add API to set spacing in HL/VL with custom values (#7129)
* feat: add API to set spacing in HL/VL with custom values This changes adds: - `setSpacing(String)` - method to set spacing passing a CSS lenght value (e.g. `10px`, `1em`) - `setSpacing(float, Unit)` - method to set spacing passing a float value and a `Unit` - `getSpacing()` - method to return the value set to the `gap` CSS property of the layout It also changes `isSpacing()` to return whether the spacing has been set by the theme API or by setting the `gap` property. * chore: apply format * fix: use gap instead of private CSS variable * refactor: remove code not needed * docs: enhance getSpacing documentation * test: add test to check setSpacing(false) --------- Co-authored-by: Zhe Sun <[email protected]> Co-authored-by: Sascha Ißbrücker <[email protected]>
1 parent b828fcb commit 4749d5f

File tree

2 files changed

+98
-2
lines changed
  • vaadin-ordered-layout-flow-parent/vaadin-ordered-layout-flow/src

2 files changed

+98
-2
lines changed
 

‎vaadin-ordered-layout-flow-parent/vaadin-ordered-layout-flow/src/main/java/com/vaadin/flow/component/orderedlayout/ThemableLayout.java

+50-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Set;
1919

2020
import com.vaadin.flow.component.HasElement;
21+
import com.vaadin.flow.component.Unit;
2122
import com.vaadin.flow.dom.Style;
2223
import com.vaadin.flow.dom.ThemeList;
2324

@@ -93,16 +94,63 @@ default boolean isPadding() {
9394
* it if {@code false}
9495
*/
9596
default void setSpacing(boolean spacing) {
97+
if (!spacing) {
98+
getElement().getStyle().remove("gap");
99+
}
96100
getThemeList().set("spacing", spacing);
97101
}
98102

99103
/**
100-
* Shows if {@code spacing} theme setting is applied to the component.
104+
* Shows if {@code spacing} setting is applied to the component, either by
105+
* setting the {@code spacing} theme or by setting the {@code gap} style.
101106
*
102107
* @return {@code true} if theme setting is applied, {@code false} otherwise
103108
*/
104109
default boolean isSpacing() {
105-
return getThemeList().contains("spacing");
110+
return getThemeList().contains("spacing")
111+
|| getElement().getStyle().has("gap");
112+
}
113+
114+
/**
115+
* Sets the spacing between the components inside the layout.
116+
*
117+
* @param spacing
118+
* the spacing between the components. The value must be a valid
119+
* CSS length, i.e. must provide a unit (e.g. "1px", "1rem",
120+
* "1%") for values other than 0.
121+
*/
122+
default void setSpacing(String spacing) {
123+
getElement().getStyle().set("gap", spacing);
124+
}
125+
126+
/**
127+
* Sets the spacing between the components inside the layout.
128+
*
129+
* @param spacing
130+
* the spacing between the components
131+
* @param unit
132+
* the unit of the spacing value
133+
*/
134+
default void setSpacing(float spacing, Unit unit) {
135+
if (spacing < 0) {
136+
setSpacing(false);
137+
}
138+
setSpacing(spacing + unit.toString());
139+
}
140+
141+
/**
142+
* Gets the spacing between the components inside the layout.
143+
*
144+
* <p>
145+
* The value returned is the value set by {@link #setSpacing(String)} or
146+
* {@link #setSpacing(float, Unit)}. If the spacing was set using
147+
* {@link #setSpacing(boolean)}, this method will return {@code null}. On
148+
* this case, use {@link #isSpacing()} instead.
149+
*
150+
* @return the spacing between the components
151+
*/
152+
default String getSpacing() {
153+
return getElement().getStyle().get("gap");
106154
}
107155

108156
/**

‎vaadin-ordered-layout-flow-parent/vaadin-ordered-layout-flow/src/test/java/com/vaadin/flow/component/orderedlayout/tests/ThemableLayoutTest.java

+48
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package com.vaadin.flow.component.orderedlayout.tests;
1717

18+
import static org.junit.Assert.assertEquals;
1819
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertNull;
1921
import static org.junit.Assert.assertTrue;
2022

2123
import java.util.function.Consumer;
@@ -24,6 +26,7 @@
2426
import org.junit.Before;
2527
import org.junit.Test;
2628

29+
import com.vaadin.flow.component.Unit;
2730
import com.vaadin.flow.component.orderedlayout.ThemableLayout;
2831
import com.vaadin.flow.dom.Element;
2932
import com.vaadin.flow.dom.ElementFactory;
@@ -69,6 +72,51 @@ public void checkWrap() {
6972
checkThemeToggling("wrap", layout::isWrap, layout::setWrap);
7073
}
7174

75+
@Test
76+
public void checkSpacingStringSetter() {
77+
layout.setSpacing("20px");
78+
assertTrue("Expected spacing to be applied after setting it",
79+
layout.isSpacing());
80+
assertEquals("Expected spacing to be '20px'", "20px",
81+
layout.getSpacing());
82+
}
83+
84+
@Test
85+
public void checkSpacingUnitSetter() {
86+
layout.setSpacing(2, Unit.REM);
87+
assertTrue("Expected spacing to be applied after setting it",
88+
layout.isSpacing());
89+
assertEquals("Expected spacing to be '2.0rem'", "2.0rem",
90+
layout.getSpacing());
91+
}
92+
93+
@Test
94+
public void checkIsSpacing() {
95+
layout.setSpacing("20px");
96+
assertTrue("Expected spacing to be applied after setting it",
97+
layout.isSpacing());
98+
layout.setSpacing(false);
99+
assertFalse("Expected no spacing applied after removing it",
100+
layout.isSpacing());
101+
layout.setSpacing(true);
102+
assertTrue("Expected spacing to be applied after setting it",
103+
layout.isSpacing());
104+
layout.setSpacing(false);
105+
assertFalse("Expected no spacing applied after removing it",
106+
layout.isSpacing());
107+
}
108+
109+
@Test
110+
public void removeSpacing_gapIsRemoved() {
111+
layout.setSpacing("20px");
112+
layout.setSpacing(false);
113+
assertFalse("Expected spacing to be removed after setting it to false",
114+
layout.isSpacing());
115+
assertNull("Expected spacing to be null", layout.getSpacing());
116+
assertNull("Expected gap to be null",
117+
layout.getElement().getStyle().get("gap"));
118+
}
119+
72120
private void checkThemeToggling(String themeName,
73121
Supplier<Boolean> themeGetter, Consumer<Boolean> themeSetter) {
74122
assertFalse(String.format(

0 commit comments

Comments
 (0)
Please sign in to comment.