From fa4c46c17da846effec0abac112e33eb9060a7a8 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:20:50 -0300 Subject: [PATCH] feat: store theme in session --- pom.xml | 7 +++++++ .../vaadin/addons/demo/TabbedDemo.java | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pom.xml b/pom.xml index ca3dcf4..116229a 100644 --- a/pom.xml +++ b/pom.xml @@ -90,6 +90,13 @@ provided + + jakarta.servlet + jakarta.servlet-api + 6.0.0 + provided + + com.flowingcode.vaadin.addons enhanced-tabs-addon diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java b/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java index 5d7731c..32c3e9a 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java +++ b/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java @@ -39,6 +39,7 @@ import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.router.RouterLayout; +import com.vaadin.flow.server.VaadinSession; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -227,6 +228,8 @@ public void showRouterLayoutContent(HasElement content) { updateFooterButtonsVisibility(); getElement().insertChild(1, content.getElement()); + + applyTheme(getElement(), getThemeName()); } private Optional createSourceCodeTab(Class annotatedClass, DemoSource annotation) { @@ -338,8 +341,22 @@ public void setOrientation(Orientation orientation) { orientationCB.setValue(Orientation.HORIZONTAL.equals(orientation)); } + private static final String THEME_NAME = TabbedDemo.class.getName() + "#THEME_NAME"; + + public static String getThemeName() { + return (String) Optional.ofNullable(VaadinSession.getCurrent().getAttribute(THEME_NAME)) + .orElse(""); + } + + @Deprecated public static void applyTheme(Element element, boolean useDarkTheme) { String theme = useDarkTheme ? "dark" : ""; + applyTheme(element, theme); + } + + public static void applyTheme(Element element, String theme) { + VaadinSession.getCurrent().setAttribute(THEME_NAME, theme); + element.executeJs("document.documentElement.setAttribute('theme', $0);", theme); Component c = element.getComponent().get();