Skip to content

Commit afc7306

Browse files
committed
feat: add support for dynamic theme in iframes
1 parent 8ffdd6d commit afc7306

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,19 @@ public void apply(HasElement component) {
224224
assertFeatureInitialized();
225225

226226
VaadinSession.getCurrent().setAttribute(DynamicTheme.class, this);
227+
228+
String document;
229+
if (component.getElement().getTag().equalsIgnoreCase("iframe")) {
230+
document = "this.contentWindow.document";
231+
} else {
232+
document = "document";
233+
}
234+
227235
component.getElement().executeJs("""
236+
const _document = %s;
228237
const applyTheme = () => {
229238
["lumo/lumo.css", "aura/aura.css"].forEach(href=> {
230-
let link = document.querySelector(`link[href='${href}']`);
239+
let link = _document.querySelector(`link[href='${href}']`);
231240
if (!link) return;
232241
if (href === $0) {
233242
if (link.rel === 'preload') link.rel = 'stylesheet';
@@ -238,12 +247,12 @@ public void apply(HasElement component) {
238247
});
239248
};
240249
241-
if (document.startViewTransition) {
242-
document.startViewTransition(applyTheme);
250+
if (_document.startViewTransition) {
251+
_document.startViewTransition(applyTheme);
243252
} else {
244253
applyTheme();
245254
}
246-
""", href);
255+
""".formatted(document), href);
247256
}
248257

249258
}

0 commit comments

Comments
 (0)