Skip to content

Commit

Permalink
Improved constructable stylesheet detection & added cssStyleSheet fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
msarca committed Apr 8, 2022
1 parent 89de051 commit 995837c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 20 deletions.
22 changes: 18 additions & 4 deletions dist/assembler.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions dist/assembler.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions dist/assembler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/assembler.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asmcss/assembler",
"version": "0.6.1",
"version": "0.7.0",
"main": "dist/assembler.cjs.js",
"module": "dist/assembler.es.js",
"browser": "dist/assembler.js",
Expand Down
26 changes: 22 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ declare global {
interface Document {
adoptedStyleSheets: CSSStyleSheet[];
}
interface Window {
ShadyCSS?: any;
}
interface CSSStyleSheet {
replace(css: string);
replaceSync(css: string);
Expand All @@ -36,12 +39,18 @@ declare global {
}
}


let styleHandler: StyleHandler = null;
let supportsConstructable = true;
let supportsAdoptingStyleSheets = true;
let settings = null;
const observedShadowRoots = new WeakMap<ShadowRoot, boolean>();

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

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

if (settings.constructable && document.adoptedStyleSheets && Object.isFrozen(document.adoptedStyleSheets)) {
if (supportsAdoptingStyleSheets && settings.constructable) {
stylesheet = new CSSStyleSheet();
if (settings.generate) {
const generated = generateStyles(settings);
Expand Down Expand Up @@ -80,12 +89,12 @@ export function init(options?: {[key: string]: string}): boolean {
return true;
}

export function handleShadowRoot(shadowRoot: ShadowRoot): boolean {
export function handleShadowRoot(shadowRoot: ShadowRoot, add: boolean = true): boolean {
if (styleHandler === null) {
init();
}

if (!supportsConstructable || !shadowRoot.adoptedStyleSheets || !Object.isFrozen(shadowRoot.adoptedStyleSheets)) {
if (!supportsAdoptingStyleSheets || !supportsConstructable) {
return false;
}

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

observedShadowRoots.set(shadowRoot, true);

shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, styleHandler.style];
if (add) {
shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, styleHandler.style];
}

observeShadow(shadowRoot, styleHandler);

return true;
}

export function cssStyleSheet(): CSSStyleSheet {
if (styleHandler === null) {
init();
}
return styleHandler.style;
}

if (typeof window !== 'undefined') {
init();
}
Expand Down
6 changes: 5 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ declare global {
interface Document {
adoptedStyleSheets: CSSStyleSheet[];
}
interface Window {
ShadyCSS?: any;
}
interface CSSStyleSheet {
replace(css: string): any;
replaceSync(css: string): any;
Expand All @@ -15,4 +18,5 @@ declare global {
export declare function init(options?: {
[key: string]: string;
}): boolean;
export declare function handleShadowRoot(shadowRoot: ShadowRoot): boolean;
export declare function handleShadowRoot(shadowRoot: ShadowRoot, add?: boolean): boolean;
export declare function cssStyleSheet(): CSSStyleSheet;

0 comments on commit 995837c

Please sign in to comment.