Skip to content

Commit 1e0ad51

Browse files
javier-godoypaodb
authored andcommitted
feat: store theme in session
1 parent 1084a04 commit 1e0ad51

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@
9090
<scope>provided</scope>
9191
</dependency>
9292

93+
<dependency>
94+
<groupId>jakarta.servlet</groupId>
95+
<artifactId>jakarta.servlet-api</artifactId>
96+
<version>6.0.0</version>
97+
<scope>provided</scope>
98+
</dependency>
99+
93100
<dependency>
94101
<groupId>com.flowingcode.vaadin.addons</groupId>
95102
<artifactId>enhanced-tabs-addon</artifactId>

src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.vaadin.flow.router.PageTitle;
4040
import com.vaadin.flow.router.Route;
4141
import com.vaadin.flow.router.RouterLayout;
42+
import com.vaadin.flow.server.VaadinSession;
4243
import java.util.ArrayList;
4344
import java.util.List;
4445
import java.util.Objects;
@@ -227,6 +228,8 @@ public void showRouterLayoutContent(HasElement content) {
227228

228229
updateFooterButtonsVisibility();
229230
getElement().insertChild(1, content.getElement());
231+
232+
applyTheme(getElement(), getThemeName());
230233
}
231234

232235
private Optional<SourceCodeTab> createSourceCodeTab(Class<?> annotatedClass, DemoSource annotation) {
@@ -338,8 +341,22 @@ public void setOrientation(Orientation orientation) {
338341
orientationCB.setValue(Orientation.HORIZONTAL.equals(orientation));
339342
}
340343

344+
private static final String THEME_NAME = TabbedDemo.class.getName() + "#THEME_NAME";
345+
346+
public static String getThemeName() {
347+
return (String) Optional.ofNullable(VaadinSession.getCurrent().getAttribute(THEME_NAME))
348+
.orElse("");
349+
}
350+
351+
@Deprecated
341352
public static void applyTheme(Element element, boolean useDarkTheme) {
342353
String theme = useDarkTheme ? "dark" : "";
354+
applyTheme(element, theme);
355+
}
356+
357+
public static void applyTheme(Element element, String theme) {
358+
VaadinSession.getCurrent().setAttribute(THEME_NAME, theme);
359+
343360
element.executeJs("document.documentElement.setAttribute('theme', $0);", theme);
344361

345362
Component c = element.getComponent().get();

0 commit comments

Comments
 (0)