Skip to content
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
25 changes: 13 additions & 12 deletions packages/vaadin-themable-mixin/src/css-property-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
*/
export class CSSPropertyObserver {
#root;
#name;
#callback;
#properties = new Set();
#styleSheet;
#isConnected = false;

constructor(root, name, callback) {
constructor(root, callback) {
this.#root = root;
this.#name = name;
this.#callback = callback;
this.#styleSheet = new CSSStyleSheet();
}

#handleTransitionEvent(event) {
Expand All @@ -33,16 +32,12 @@ export class CSSPropertyObserver {
observe(property) {
this.connect();

this.#properties.add(property);
this.#rootHost.style.setProperty(`--${this.#name}-props`, [...this.#properties].join(', '));
}

connect() {
if (this.#isConnected) {
if (this.#properties.has(property)) {
return;
}

this.#styleSheet = new CSSStyleSheet();
this.#properties.add(property);

this.#styleSheet.replaceSync(`
:is(:root, :host)::before {
content: '' !important;
Expand All @@ -51,9 +46,16 @@ export class CSSPropertyObserver {
left: -9999px !important;
visibility: hidden !important;
transition: 1ms allow-discrete step-end !important;
transition-property: var(--${this.#name}-props) !important;
transition-property: ${[...this.#properties].join(', ')} !important;
}
`);
}

connect() {
if (this.#isConnected) {
return;
}

this.#root.adoptedStyleSheets.unshift(this.#styleSheet);

this.#rootHost.addEventListener('transitionstart', (event) => this.#handleTransitionEvent(event));
Expand All @@ -69,7 +71,6 @@ export class CSSPropertyObserver {

this.#rootHost.removeEventListener('transitionstart', this.#handleTransitionEvent);
this.#rootHost.removeEventListener('transitionend', this.#handleTransitionEvent);
this.#rootHost.style.removeProperty(`--${this.#name}-props`);

this.#isConnected = false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vaadin-themable-mixin/src/lumo-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class LumoInjector {

constructor(root = document) {
this.#root = root;
this.#cssPropertyObserver = new CSSPropertyObserver(this.#root, 'vaadin-lumo-injector', (propertyName) => {
this.#cssPropertyObserver = new CSSPropertyObserver(this.#root, (propertyName) => {
const tagName = propertyName.slice(2).replace('-lumo-inject', '');
this.#updateStyleSheet(tagName);
});
Expand Down
Loading