Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions articles/styling/advanced/dynamic-stylesheets.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,58 @@
registration.remove();
----

Using the dynamic loading and the registration it is possible to have swappable style functionality.

Check failure on line 37 in articles/styling/advanced/dynamic-stylesheets.adoc

View workflow job for this annotation

GitHub Actions / lint

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'swappable'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'swappable'?", "location": {"path": "articles/styling/advanced/dynamic-stylesheets.adoc", "range": {"start": {"line": 37, "column": 71}}}, "severity": "ERROR"}

This can be used, for example, in a [classname]`StyleChanger` component to update the current style based on the user’s selection.

Sample code defines the styles `default`, `color vision deficiency` and `compact`, for which the actual style files would be located in `src/main/resources/META-INF/resources/`

[source,java]
----
public class ThemeChanger extends Select<String> {
public ThemeChanger() {
setLabel("Select Theme");
setItems("default-style.css", "cvd-style.css", "compact-style.css");
setValue("default-style.css");

addValueChangeListener(event -> switchTheme(
event.getSource().getUI().orElse(UI.getCurrent()),
event.getValue()));
}

@Override
protected void onAttach(AttachEvent attachEvent) {
UI ui = attachEvent.getUI();
Registration registration = ComponentUtil.getData(ui,
Registration.class);
// set default-style if no registration found
if (registration != null) {
switchTheme(ui, "default-style.css");
}
}

/**
* Switch the current theme style sheet for a new one.
*
* @param ui the ui in use, not {@code null}
* @param styleSheet new style to replace current with
*/
protected void switchTheme(UI ui, String styleSheet) {
// Get previous style registration
Registration registration = ComponentUtil.getData(ui,
Registration.class);
// If previous available remove style sheet
if (registration != null) {
registration.remove();
}

// Add the new style sheet
registration = ui.getPage().addStyleSheet(styleSheet);

// Store registration to remove style on later change
ComponentUtil.setData(ui, Registration.class, registration);
}
}
----

[discussion-id]`6c72d9f9-16d5-4ab5-add8-3c481c3103f8`