Skip to content

Commit b3ecb34

Browse files
committed
refactor: use event instead of mutation observer
1 parent 99f4474 commit b3ecb34

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
@@ -293,6 +293,7 @@ export const DashboardItemMixin = (superClass) =>
293293
super();
294294
this.__keyboardController = new KeyboardController(this);
295295
this.__focusTrapController = new FocusTrapController(this);
296+
this.__boundRootHeadingLevelChangedListener = this.__updateRootHeadingLevel.bind(this);
296297
}
297298

298299
/** @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
@@ -123,6 +123,7 @@ export const DashboardLayoutMixin = (superClass) =>
123123
value: 2,
124124
sync: true,
125125
reflectToAttribute: true,
126+
observer: '__rootHeadingLevelChanged',
126127
},
127128
};
128129
}
@@ -156,4 +157,11 @@ export const DashboardLayoutMixin = (superClass) =>
156157
// ...and set it as the new value
157158
this.$.grid.style.setProperty('--_col-count', columnCount);
158159
}
160+
161+
/** @private */
162+
__rootHeadingLevelChanged(rootHeadingLevel) {
163+
this.dispatchEvent(
164+
new CustomEvent('dashboard-root-heading-level-changed', { detail: { value: rootHeadingLevel } }),
165+
);
166+
}
159167
};

0 commit comments

Comments
 (0)