Skip to content

Commit 5c10ebf

Browse files
fix(runtime): properly assign style declarations (#5838)
* fix(runtime): properly assign style declarations * prettier
1 parent 7ffb25d commit 5c10ebf

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/runtime/initialize-component.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,38 @@ export const initializeComponent = async (
113113
}
114114

115115
if (BUILD.style && Cstr && Cstr.style) {
116-
// this component has styles but we haven't registered them yet
117-
let style = Cstr.style;
118-
119-
if (BUILD.mode && typeof style !== 'string') {
116+
/**
117+
* this component has styles but we haven't registered them yet
118+
*/
119+
let style: string | undefined;
120+
121+
if (typeof Cstr.style === 'string') {
122+
/**
123+
* in case the component has a `styleUrl` defined, e.g.
124+
* ```ts
125+
* @Component({
126+
* tag: 'my-component',
127+
* styleUrl: 'my-component.css'
128+
* })
129+
* ```
130+
*/
131+
style = Cstr.style;
132+
} else if (BUILD.mode && typeof Cstr.style !== 'string') {
133+
/**
134+
* in case the component has a `styleUrl` object defined, e.g.
135+
* ```ts
136+
* @Component({
137+
* tag: 'my-component',
138+
* styleUrl: {
139+
* ios: 'my-component.ios.css',
140+
* md: 'my-component.md.css'
141+
* }
142+
* })
143+
* ```
144+
*/
120145
hostRef.$modeName$ = computeMode(elm) as string | undefined;
121146
if (hostRef.$modeName$) {
122-
style = style[hostRef.$modeName$];
147+
style = Cstr.style[hostRef.$modeName$];
123148
}
124149

125150
if (BUILD.hydrateServerSide && hostRef.$modeName$) {

src/runtime/styles.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ export const registerStyle = (scopeId: string, cssText: string, allowCS: boolean
4747
* @param mode an optional current mode
4848
* @returns the scope ID for the component of interest
4949
*/
50-
export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMeta, mode?: string) => {
50+
export const addStyle = (
51+
styleContainerNode: Element | Document | ShadowRoot,
52+
cmpMeta: d.ComponentRuntimeMeta,
53+
mode?: string,
54+
) => {
55+
const styleContainerDocument = styleContainerNode as Document;
56+
const styleContainerShadowRoot = styleContainerNode as ShadowRoot;
5157
const scopeId = getScopeId(cmpMeta, mode);
5258
const style = styles.get(scopeId);
5359

@@ -60,7 +66,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
6066

6167
if (style) {
6268
if (typeof style === 'string') {
63-
styleContainerNode = styleContainerNode.head || styleContainerNode;
69+
styleContainerNode = styleContainerDocument.head || (styleContainerNode as HTMLElement);
6470
let appliedStyles = rootAppliedStyles.get(styleContainerNode);
6571
let styleElm;
6672
if (!appliedStyles) {
@@ -69,7 +75,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
6975
if (!appliedStyles.has(scopeId)) {
7076
if (
7177
BUILD.hydrateClientSide &&
72-
styleContainerNode.host &&
78+
styleContainerShadowRoot.host &&
7379
(styleElm = styleContainerNode.querySelector(`[${HYDRATED_STYLE_ID}="${scopeId}"]`))
7480
) {
7581
// This is only happening on native shadow-dom, do not needs CSS var shim
@@ -100,8 +106,8 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
100106
appliedStyles.add(scopeId);
101107
}
102108
}
103-
} else if (BUILD.constructableCSS && !styleContainerNode.adoptedStyleSheets.includes(style)) {
104-
styleContainerNode.adoptedStyleSheets = [...styleContainerNode.adoptedStyleSheets, style];
109+
} else if (BUILD.constructableCSS && !styleContainerDocument.adoptedStyleSheets.includes(style)) {
110+
styleContainerDocument.adoptedStyleSheets = [...styleContainerDocument.adoptedStyleSheets, style];
105111
}
106112
}
107113
return scopeId;
@@ -119,7 +125,7 @@ export const attachStyles = (hostRef: d.HostRef) => {
119125
const flags = cmpMeta.$flags$;
120126
const endAttachStyles = createTime('attachStyles', cmpMeta.$tagName$);
121127
const scopeId = addStyle(
122-
BUILD.shadowDom && supportsShadow && elm.shadowRoot ? elm.shadowRoot : elm.getRootNode(),
128+
BUILD.shadowDom && supportsShadow && elm.shadowRoot ? elm.shadowRoot : (elm.getRootNode() as ShadowRoot),
123129
cmpMeta,
124130
hostRef.$modeName$,
125131
);

0 commit comments

Comments
 (0)