Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 20 additions & 11 deletions src/StyledElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ class StyledElement extends BaseElement {
return cssStyleSheet;
});
}

if (supportsAdoptingStyleSheets() && this._options.shadowRender) {
// adopting does only make sense in shadow dom. Fall back to append for light elements
this.adoptStyleSheets();

if (this._options.adoptGlobalStyles !== false) {
globalStylesStore.subscribe(() => {
this.adoptStyleSheets();
});
}
}
}

/**
Expand All @@ -73,6 +62,26 @@ class StyledElement extends BaseElement {
super.update(options);
}

/**
* Start the adoption of external stylesheets
* adopt global styles from document
* subscribe to globalStylesStore for updates
* @returns { void }
*/

initStyleAdoption() {
if (this._options.shadowRender && supportsAdoptingStyleSheets()) {
// adopting does only make sense in shadow dom. Fall back to append for light elements
this.adoptStyleSheets();

if (this._options.adoptGlobalStyles !== false) {
globalStylesStore.subscribe(() => {
this.adoptStyleSheets();
});
}
}
}

/**
* Adopt stylesheets
*/
Expand Down
10 changes: 8 additions & 2 deletions src/TemplateElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ class TemplateElement extends StyledElement {

/**
* Overrides the native `connectedCallback` of the HTMLElement to set up and initialize our element.
* This will attach a shadow DOM if the element is supposed to render in shadow DOM.
*/
connectedCallback() {
if (this._options.shadowRender && !this.shadowRoot) this.attachShadow({ mode: 'open' });
super.connectedCallback();
}

Expand All @@ -54,8 +52,16 @@ class TemplateElement extends StyledElement {

/**
* Render the template to the root
* This will attach a shadow DOM if the element is supposed to render in shadow DOM.
*/
renderTemplate() {
const firstRender = this._options.shadowRender && !this.shadowRoot;
if (firstRender) {
this.attachShadow({ mode: 'open' });
// init external stylesheets
this.initStyleAdoption();
}

const template = this._template || this.template();
render(template, this.getRoot());
}
Expand Down