-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add support for dynamic theme switching #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
src/main/java/com/flowingcode/vaadin/addons/demo/DynamicTheme.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| package com.flowingcode.vaadin.addons.demo; | ||
|
|
||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.component.HasElement; | ||
| import com.vaadin.flow.component.page.Inline.Position; | ||
| import com.vaadin.flow.server.AppShellSettings; | ||
| import com.vaadin.flow.server.VaadinSession; | ||
| import com.vaadin.flow.server.Version; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| /** | ||
| * Enumeration representing supported themes for dynamic switching. | ||
| * <p> | ||
| * This enum facilitates switching between themes (e.g., Lumo, Aura) at runtime. | ||
| * </p> | ||
| */ | ||
| @RequiredArgsConstructor | ||
| public enum DynamicTheme { | ||
|
|
||
| /** | ||
| * The standard Lumo theme. | ||
| */ | ||
| LUMO("lumo/lumo.css", "hsl(214, 35%, 21%)"), | ||
|
|
||
| /** | ||
| * The standard Aura theme. | ||
| */ | ||
| AURA("aura/aura.css", "oklch(0.2 0.01 260)"), | ||
|
|
||
| /** | ||
| * A base theme without specific styling. | ||
| */ | ||
| BASE(null, "#000"); | ||
|
|
||
| @Getter | ||
| private final String href; | ||
|
|
||
| @Getter | ||
| private final String bgColor; | ||
|
|
||
|
|
||
| private static void assertFeatureSupported() { | ||
| if (!isFeatureSupported()) { | ||
| throw new UnsupportedOperationException("Dynamic theme switching requires Vaadin 25+"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the dynamic theme feature is supported. The feature is supported in Vaadin 25. | ||
| * | ||
| * @return {@code true} if the feature is supported and initialized; {@code false} otherwise. | ||
| */ | ||
| public static boolean isFeatureSupported() { | ||
| return Version.getMajorVersion() >= 25; | ||
| } | ||
|
|
||
| private static void assertFeatureInitialized() { | ||
| assertFeatureSupported(); | ||
| if (!isFeatureInitialized()) { | ||
| throw new IllegalStateException("Dynamic theme switching has not been initialized"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the dynamic theme feature has been initialized for the current session. | ||
| * | ||
| * @return {@code true} if the feature is supported and initialized; {@code false} otherwise. | ||
| */ | ||
| public static boolean isFeatureInitialized() { | ||
| return isFeatureSupported() | ||
| && VaadinSession.getCurrent().getAttribute(DynamicTheme.class) != null; | ||
| } | ||
|
|
||
| /** | ||
| * Return the current dynamic theme. | ||
| * | ||
| * @throws UnsupportedOperationException if the runtime Vaadin version is older than 25. | ||
| * @return the current dynamic theme, or {@code null} if the feature has not been initialized. | ||
| */ | ||
| public static DynamicTheme getCurrent() { | ||
| assertFeatureSupported(); | ||
| return VaadinSession.getCurrent().getAttribute(DynamicTheme.class); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the theme settings. | ||
| * <p> | ||
| * This method performs a lazy initialization of the {@link DynamicTheme} within the | ||
| * current {@link VaadinSession}. If no theme is present, it registers this instance | ||
| * as the session default. Subsequently, it injects the corresponding CSS stylesheet | ||
| * link into the {@link AppShellSettings}. | ||
| * </p> | ||
| * | ||
| * @param settings the application shell settings to be modified | ||
| * @throws UnsupportedOperationException if the runtime Vaadin version is older than 25 | ||
| */ | ||
| public void initialize(AppShellSettings settings) { | ||
| assertFeatureSupported(); | ||
|
|
||
| DynamicTheme theme = getCurrent(); | ||
| if (theme == null) { | ||
| theme = this; | ||
| VaadinSession.getCurrent().setAttribute(DynamicTheme.class, theme); | ||
| } | ||
|
|
||
| switch (theme) { | ||
| case AURA: | ||
| settings.addLink(Position.APPEND, "stylesheet", "aura/aura.css"); | ||
| break; | ||
| case LUMO: | ||
| settings.addLink(Position.APPEND, "stylesheet", "lumo/lumo.css"); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Prepares the component for dynamic theme switching by preloading stylesheets. | ||
| * <p> | ||
| * Adds a client-side listener to the component that detects mouseover events. | ||
| * When triggered, it preloads the theme stylesheets (Lumo and Aura) to ensure | ||
| * they can be applied immediately when needed. | ||
| * </p> | ||
| * | ||
| * @param component the component to attach the listener to | ||
| * @throws IllegalStateException if the dynamic theme feature has not been initialized | ||
| */ | ||
| public static void prepare(Component component) { | ||
| assertFeatureInitialized(); | ||
|
|
||
| component.addAttachListener(ev -> doPrepare(component)); | ||
| if (component.isAttached()) { | ||
| doPrepare(component); | ||
| } | ||
| } | ||
|
|
||
| private static void doPrepare(Component component) { | ||
| component.getElement().executeJs(""" | ||
| this.addEventListener('mouseover', function() { | ||
| ["lumo/lumo.css", "aura/aura.css"].forEach(href=> { | ||
| let link = document.querySelector(`link[href="${href}"]`); | ||
| if (!link) { | ||
| link = document.createElement("link"); | ||
| link.href = href; | ||
| link.as = 'style'; | ||
| link.rel = 'preload'; | ||
| document.head.prepend(link); | ||
| } | ||
| }); | ||
| }, {once:true} ); | ||
| """); | ||
| } | ||
|
|
||
| /** | ||
| * Applies this theme to the view. | ||
| * | ||
| * @param component a component in the view | ||
| * @throws IllegalStateException if the dynamic theme feature has not been initialized | ||
| */ | ||
| public void apply(HasElement component) { | ||
| assertFeatureInitialized(); | ||
|
|
||
| VaadinSession.getCurrent().setAttribute(DynamicTheme.class, this); | ||
| component.getElement().executeJs(""" | ||
| const applyTheme = () => { | ||
| ["lumo/lumo.css", "aura/aura.css"].forEach(href=> { | ||
| let link = document.querySelector(`link[href='${href}']`); | ||
| if (!link) return; | ||
| if (href === $0) { | ||
| if (link.rel === 'preload') link.rel = 'stylesheet'; | ||
| if (link.disabled) link.disabled = false; | ||
| } else if (link.rel === 'stylesheet' && !link.disabled) { | ||
| link.disabled = true; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| if (document.startViewTransition) { | ||
| document.startViewTransition(applyTheme); | ||
| } else { | ||
| applyTheme(); | ||
| } | ||
| """, href); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/resources/META-INF/resources/frontend/styles/commons-demo/vaadin-select-overlay.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** Needed in order to prevent vaadin-select from remaining 'closing' when switching from Lumo to Aura/Base*/ | ||
| :host([theme~='demo-footer-theme-select']) vaadin-select-overlay { | ||
| animation-name: none; | ||
| } |
10 changes: 8 additions & 2 deletions
10
src/test/java/com/flowingcode/vaadin/addons/demo/AppShellConfiguratorImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| package com.flowingcode.vaadin.addons.demo; | ||
|
|
||
| import com.vaadin.flow.component.page.AppShellConfigurator; | ||
| import com.vaadin.flow.theme.Theme; | ||
| import com.vaadin.flow.server.AppShellSettings; | ||
|
|
||
| @Theme | ||
| public class AppShellConfiguratorImpl implements AppShellConfigurator { | ||
|
|
||
| @Override | ||
| public void configurePage(AppShellSettings settings) { | ||
| if (DynamicTheme.isFeatureSupported()) { | ||
| DynamicTheme.LUMO.initialize(settings); | ||
| } | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.