Skip to content

Commit

Permalink
fix(readoptStyles): only attempt to readopt constructed stylesheets i…
Browse files Browse the repository at this point in the history
…f the element instance has an `ownerDocument.defaultView` value set
  • Loading branch information
DRiFTy17 committed Oct 22, 2024
1 parent 2bf1c87 commit c126ee7
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/custom-elements/component-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,21 @@ export function setShadowStyles<T extends HTMLElement>(componentInstance: T, sty
}

/**
* Reapplies styles to the shadow root of the provided element instance. This function was
* Re-applies styles to the shadow root of the provided element instance. This function is
* intended to be called after an element has been adopted by a new document to reconstruct the
* adopted stylesheet instances within the context of the new document.
* adopted stylesheet instances within the context (view) of the new document.
*
* @param componentInstance The component instance to reapply styles to.
*/
export function readoptStyles<T extends HTMLElement>(componentInstance: T): void {
if (!supportsConstructableStyleSheets ||
!componentInstance.shadowRoot ||
!componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY]) {
!componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY] ||
!componentInstance.ownerDocument.defaultView) {
return;
}
const cssText = componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY];
const context = componentInstance.ownerDocument.defaultView ?? window;
const sheet = new context.CSSStyleSheet();
sheet.replaceSync(cssText);
const sheet = new componentInstance.ownerDocument.defaultView.CSSStyleSheet();
sheet.replaceSync(componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY]);
componentInstance.shadowRoot.adoptedStyleSheets = [sheet];
}

Expand Down

0 comments on commit c126ee7

Please sign in to comment.