Skip to content
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

feat(runtime): make shadow root adopt scoped component styles #6028

Merged
Merged
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
41 changes: 27 additions & 14 deletions src/runtime/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,34 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
: styleContainerNode.querySelector('style');
(styleContainerNode as HTMLElement).insertBefore(styleElm, referenceNode);
} else if ('host' in styleContainerNode) {
/**
* If a scoped component is used within a shadow root, we want to insert the styles
* at the beginning of the shadow root node.
*
* However if there is already a style node in the ShadowRoot, we just append
* the styles to the existing node.
*
* Note: order of how styles are applied is important. The new style node
* should be inserted before the existing style node.
*/
const existingStyleContainer = styleContainerNode.querySelector('style');
if (existingStyleContainer) {
existingStyleContainer.innerHTML = style + existingStyleContainer.innerHTML;
if (supportsConstructableStylesheets) {
/**
* If a scoped component is used within a shadow root then turn the styles into a
* constructable stylesheet and add it to the shadow root's adopted stylesheets.
*
* Note: order of how styles are adopted is important. The new stylesheet should be
* adopted before the existing styles.
*/
const stylesheet = new CSSStyleSheet();
stylesheet.replaceSync(style);
styleContainerNode.adoptedStyleSheets = [stylesheet, ...styleContainerNode.adoptedStyleSheets];
} else {
(styleContainerNode as HTMLElement).prepend(styleElm);
/**
* If a scoped component is used within a shadow root and constructable stylesheets are
* not supported, we want to insert the styles at the beginning of the shadow root node.
*
* However, if there is already a style node in the shadow root, we just append
* the styles to the existing node.
*
* Note: order of how styles are applied is important. The new style node
* should be inserted before the existing style node.
*/
const existingStyleContainer = styleContainerNode.querySelector('style');
if (existingStyleContainer) {
existingStyleContainer.innerHTML = style + existingStyleContainer.innerHTML;
} else {
(styleContainerNode as HTMLElement).prepend(styleElm);
}
}
} else {
styleContainerNode.append(styleElm);
Expand Down