Skip to content

Commit

Permalink
fix(styles): ensure styles are applied before paint (#3452)
Browse files Browse the repository at this point in the history
this commit fixes a bug where styles from a stencil component would not
be applied prior to render. the issue involved stylesheet construction &
application not blocking render, causing the component to be created
without styles applied.

this commit moves from the `replace` function to the `replaceSync`
function to properly await the call

STENCIL-433: Stabilize Browserstack CI Tests
  • Loading branch information
rwaskiewicz authored Jul 5, 2022
1 parent a6e0141 commit c47cec6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/client/client-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export const supportsListenerOptions = /*@__PURE__*/ (() => {

export const promiseResolve = (v?: any) => Promise.resolve(v);

export const supportsConstructibleStylesheets = BUILD.constructableCSS
export const supportsConstructableStylesheets = BUILD.constructableCSS
? /*@__PURE__*/ (() => {
try {
new CSSStyleSheet();
return typeof new CSSStyleSheet().replace === 'function';
return typeof new CSSStyleSheet().replaceSync === 'function';
} catch (e) {}
return false;
})()
Expand Down
2 changes: 1 addition & 1 deletion src/hydrate/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const supportsShadow = false;

export const supportsListenerOptions = false;

export const supportsConstructibleStylesheets = false;
export const supportsConstructableStylesheets = false;

const hostRefs: WeakMap<d.RuntimeRef, d.HostRef> = new WeakMap();

Expand Down
10 changes: 7 additions & 3 deletions src/runtime/styles.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type * as d from '../declarations';
import { BUILD } from '@app-data';
import { CMP_FLAGS } from '@utils';
import { doc, plt, styles, supportsConstructibleStylesheets, supportsShadow } from '@platform';
import { doc, plt, styles, supportsConstructableStylesheets, supportsShadow } from '@platform';
import { HYDRATED_STYLE_ID, NODE_TYPE } from './runtime-constants';
import { createTime } from './profile';

const rootAppliedStyles: d.RootAppliedStyleMap = /*@__PURE__*/ new WeakMap();

export const registerStyle = (scopeId: string, cssText: string, allowCS: boolean) => {
let style = styles.get(scopeId);
if (supportsConstructibleStylesheets && allowCS) {
if (supportsConstructableStylesheets && allowCS) {
style = (style || new CSSStyleSheet()) as CSSStyleSheet;
style.replace(cssText);
if (typeof style === 'string') {
style = cssText;
} else {
style.replaceSync(cssText);
}
} else {
style = cssText;
}
Expand Down
2 changes: 1 addition & 1 deletion src/testing/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export {
resetPlatform,
startAutoApplyChanges,
stopAutoApplyChanges,
supportsConstructibleStylesheets,
supportsConstructableStylesheets,
supportsListenerOptions,
setSupportsShadowDom,
} from './testing-platform';
Expand Down
2 changes: 1 addition & 1 deletion src/testing/platform/testing-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const setPlatformHelpers = (helpers: {

export const cssVarShim: d.CssVarShim = false as any;
export const supportsListenerOptions = true;
export const supportsConstructibleStylesheets = false;
export const supportsConstructableStylesheets = false;
export const Context: any = {};

export const setSupportsShadowDom = (supports: boolean) => {
Expand Down

0 comments on commit c47cec6

Please sign in to comment.