Skip to content

Commit a3e82fb

Browse files
committed
refactor: use event instead of mutation observer
1 parent bb0dba0 commit a3e82fb

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

packages/dashboard/src/vaadin-dashboard-item-mixin.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export const DashboardItemMixin = (superClass) =>
288288
super();
289289
this.__keyboardController = new KeyboardController(this);
290290
this.__focusTrapController = new FocusTrapController(this);
291+
this.__boundRootHeadingLevelChangedListener = this.__updateRootHeadingLevel.bind(this);
291292
}
292293

293294
/** @protected */
@@ -401,19 +402,22 @@ export const DashboardItemMixin = (superClass) =>
401402
this.__removeHeadingLevelObserver();
402403
const parentLayout = getParentLayout(this);
403404
if (parentLayout) {
404-
this.__headingLevelObserver = new MutationObserver(() => this.__updateRootHeadingLevel());
405-
this.__headingLevelObserver.observe(parentLayout, {
406-
attributes: true,
407-
attributeFilter: ['root-heading-level'],
408-
});
405+
this.__rootHeadingLevelListenerTarget = parentLayout;
406+
parentLayout.addEventListener(
407+
'dashboard-root-heading-level-changed',
408+
this.__boundRootHeadingLevelChangedListener,
409+
);
409410
}
410411
}
411412

412413
/** @private */
413414
__removeHeadingLevelObserver() {
414-
if (this.__headingLevelObserver) {
415-
this.__headingLevelObserver.disconnect();
416-
this.__headingLevelObserver = null;
415+
if (this.__rootHeadingLevelListenerTarget) {
416+
this.__rootHeadingLevelListenerTarget.removeEventListener(
417+
'dashboard-root-heading-level-changed',
418+
this.__boundRootHeadingLevelChangedListener,
419+
);
420+
this.__rootHeadingLevelListenerTarget = null;
417421
}
418422
}
419423

packages/dashboard/src/vaadin-dashboard-layout-mixin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const DashboardLayoutMixin = (superClass) =>
4545
value: 2,
4646
sync: true,
4747
reflectToAttribute: true,
48+
observer: '__rootHeadingLevelChanged',
4849
},
4950
};
5051
}
@@ -78,4 +79,11 @@ export const DashboardLayoutMixin = (superClass) =>
7879
// ...and set it as the new value
7980
this.$.grid.style.setProperty('--_col-count', columnCount);
8081
}
82+
83+
/** @private */
84+
__rootHeadingLevelChanged(rootHeadingLevel) {
85+
this.dispatchEvent(
86+
new CustomEvent('dashboard-root-heading-level-changed', { detail: { value: rootHeadingLevel } }),
87+
);
88+
}
8189
};

0 commit comments

Comments
 (0)