Skip to content

Commit 995837c

Browse files
committed
Improved constructable stylesheet detection & added cssStyleSheet function
1 parent 89de051 commit 995837c

File tree

7 files changed

+83
-20
lines changed

7 files changed

+83
-20
lines changed

dist/assembler.cjs.js

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assembler.es.js

Lines changed: 18 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assembler.js

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assembler.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@asmcss/assembler",
3-
"version": "0.6.1",
3+
"version": "0.7.0",
44
"main": "dist/assembler.cjs.js",
55
"module": "dist/assembler.es.js",
66
"browser": "dist/assembler.js",

src/index.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ declare global {
2727
interface Document {
2828
adoptedStyleSheets: CSSStyleSheet[];
2929
}
30+
interface Window {
31+
ShadyCSS?: any;
32+
}
3033
interface CSSStyleSheet {
3134
replace(css: string);
3235
replaceSync(css: string);
@@ -36,12 +39,18 @@ declare global {
3639
}
3740
}
3841

42+
3943
let styleHandler: StyleHandler = null;
4044
let supportsConstructable = true;
45+
let supportsAdoptingStyleSheets = true;
4146
let settings = null;
4247
const observedShadowRoots = new WeakMap<ShadowRoot, boolean>();
4348

4449
export function init(options?: {[key: string]: string}): boolean {
50+
supportsAdoptingStyleSheets = window.ShadowRoot &&
51+
(window.ShadyCSS === undefined || window.ShadyCSS.nativeShadow) &&
52+
'adoptedStyleSheets' in Document.prototype &&
53+
'replace' in CSSStyleSheet.prototype;
4554
settings = getUserSettings(options || document.currentScript.dataset);
4655

4756
if (!settings.enabled) {
@@ -51,7 +60,7 @@ export function init(options?: {[key: string]: string}): boolean {
5160
let tracker: Set<string>;
5261
let stylesheet: CSSStyleSheet;
5362

54-
if (settings.constructable && document.adoptedStyleSheets && Object.isFrozen(document.adoptedStyleSheets)) {
63+
if (supportsAdoptingStyleSheets && settings.constructable) {
5564
stylesheet = new CSSStyleSheet();
5665
if (settings.generate) {
5766
const generated = generateStyles(settings);
@@ -80,12 +89,12 @@ export function init(options?: {[key: string]: string}): boolean {
8089
return true;
8190
}
8291

83-
export function handleShadowRoot(shadowRoot: ShadowRoot): boolean {
92+
export function handleShadowRoot(shadowRoot: ShadowRoot, add: boolean = true): boolean {
8493
if (styleHandler === null) {
8594
init();
8695
}
8796

88-
if (!supportsConstructable || !shadowRoot.adoptedStyleSheets || !Object.isFrozen(shadowRoot.adoptedStyleSheets)) {
97+
if (!supportsAdoptingStyleSheets || !supportsConstructable) {
8998
return false;
9099
}
91100

@@ -95,13 +104,22 @@ export function handleShadowRoot(shadowRoot: ShadowRoot): boolean {
95104

96105
observedShadowRoots.set(shadowRoot, true);
97106

98-
shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, styleHandler.style];
107+
if (add) {
108+
shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, styleHandler.style];
109+
}
99110

100111
observeShadow(shadowRoot, styleHandler);
101112

102113
return true;
103114
}
104115

116+
export function cssStyleSheet(): CSSStyleSheet {
117+
if (styleHandler === null) {
118+
init();
119+
}
120+
return styleHandler.style;
121+
}
122+
105123
if (typeof window !== 'undefined') {
106124
init();
107125
}

types/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ declare global {
44
interface Document {
55
adoptedStyleSheets: CSSStyleSheet[];
66
}
7+
interface Window {
8+
ShadyCSS?: any;
9+
}
710
interface CSSStyleSheet {
811
replace(css: string): any;
912
replaceSync(css: string): any;
@@ -15,4 +18,5 @@ declare global {
1518
export declare function init(options?: {
1619
[key: string]: string;
1720
}): boolean;
18-
export declare function handleShadowRoot(shadowRoot: ShadowRoot): boolean;
21+
export declare function handleShadowRoot(shadowRoot: ShadowRoot, add?: boolean): boolean;
22+
export declare function cssStyleSheet(): CSSStyleSheet;

0 commit comments

Comments
 (0)