Skip to content

Custom elements #5

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

Merged
merged 5 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ We have conducted intensive manual testing on the following operating systems an
- [x] Add support for scopes
- [x] Add support for mixins
- [ ] Add support for alternative attribute rules.
- [ ] Add support for custom elements
- [x] Add support for custom elements
- [ ] Add automated tests, so we can make sure we don’t mess up things in future releases
- [ ] Cleanup & prepare stable releases

Expand Down
46 changes: 42 additions & 4 deletions dist/assembler.cjs.js

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

47 changes: 42 additions & 5 deletions dist/assembler.es.js

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

46 changes: 42 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 src/StyleHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const VAR_REGEX = /@([a-zA-Z0-9\-_]+)/g;
const REPLACE_REGEX = /\$(selector|body|class|value|property|state|variants|var)/g;

export default class StyleHandler {
private style: CSSStyleSheet;
readonly style: CSSStyleSheet;
private readonly settings: UserSettings;
private tracker: Set<string>;
private mediaSettings: object;
Expand Down
43 changes: 38 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {generateStyles} from "./generators";
import {observeDocument} from "./observers";
import {observeDocument, observeShadow} from "./observers";
import {getUserSettings} from "./helpers";
import {generateRootVariables} from "./variables";
import StyleHandler from "./StyleHandler";
Expand All @@ -31,10 +31,18 @@ declare global {
replace(css: string);
replaceSync(css: string);
}
interface ShadowRoot {
adoptedStyleSheets?: CSSStyleSheet[];
}
}

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

export function init(options?: {[key: string]: string}): boolean {
const settings = getUserSettings(options || document.currentScript.dataset);
settings = getUserSettings(options || document.currentScript.dataset);

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

if (settings.constructable && document.adoptedStyleSheets) {
if (settings.constructable && document.adoptedStyleSheets && Object.isFrozen(document.adoptedStyleSheets)) {
stylesheet = new CSSStyleSheet();
if (settings.generate) {
const generated = generateStyles(settings);
Expand All @@ -53,8 +61,9 @@ export function init(options?: {[key: string]: string}): boolean {
tracker = new Set<string>();
stylesheet.replaceSync(generateRootVariables(settings));
}
document.adoptedStyleSheets = [stylesheet];
document.adoptedStyleSheets = [...document.adoptedStyleSheets, stylesheet];
} else {
supportsConstructable = false;
const style = document.createElement("style");
const generated = generateStyles(settings);
tracker = generated.tracker;
Expand All @@ -64,7 +73,31 @@ export function init(options?: {[key: string]: string}): boolean {
stylesheet = style.sheet;
}

observeDocument(document, new StyleHandler(settings, stylesheet, tracker));
styleHandler = new StyleHandler(settings, stylesheet, tracker);

observeDocument(document, styleHandler);

return true;
}

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

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

if (observedShadowRoots.has(shadowRoot)) {
return true;
}

observedShadowRoots.set(shadowRoot, true);

shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, styleHandler.style];

observeShadow(shadowRoot, styleHandler);

return true;
}
Expand Down
Loading