Skip to content

Commit ca9ee25

Browse files
authored
fix: avoid triggering connected callback from disconnected callback (#4718)
When moving an element that extends from `DirMixin` in the DOM, the `DirMixin.disconnectedCallback` can trigger the element's `connectedCallback` before the `disconnectedCallback` has finished (currently reproducable in Chrome, FF). That can cause elements to re-run their initialization logic before their cleanup logic is finished. For example in case of the `AppLayout`, this results in the `connectedCallback` adding several event listeners again, after which the remaining logic in `disconnectedCallback` removes them again, leaving the element in a state where the listeners are not registered. See #4682 (comment). This change removes the `removeAttribute` call from `DirMixin.disconnectedCallback` that causes the premature reconnect, and instead introduces a flag that ensures that the element will subscribe to global dir changes again on reconnect. This also fixes a minor bug where the custom dir of an element was removed on disconnect (see test case). Fixes: #4682
1 parent 09af697 commit ca9ee25

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

‎packages/component-base/src/dir-mixin.js‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { DirHelper } from './dir-helper.js';
99
* Array of Vaadin custom element classes that have been subscribed to the dir changes.
1010
*/
1111
const directionSubscribers = [];
12+
1213
function directionUpdater() {
1314
const documentDir = getDocumentDir();
1415
directionSubscribers.forEach((element) => {
@@ -74,7 +75,7 @@ export const DirMixin = (superClass) =>
7475
connectedCallback() {
7576
super.connectedCallback();
7677

77-
if (!this.hasAttribute('dir')) {
78+
if (!this.hasAttribute('dir') || this.__restoreSubscription) {
7879
this.__subscribe();
7980
alignDirs(this, getDocumentDir(), null);
8081
}
@@ -100,15 +101,15 @@ export const DirMixin = (superClass) =>
100101
this.__subscribe();
101102
alignDirs(this, documentDir, newValue);
102103
} else if (newDiffValue) {
103-
this.__subscribe(false);
104+
this.__unsubscribe();
104105
}
105106
}
106107

107108
/** @protected */
108109
disconnectedCallback() {
109110
super.disconnectedCallback();
110-
this.__subscribe(false);
111-
this.removeAttribute('dir');
111+
this.__restoreSubscription = directionSubscribers.includes(this);
112+
this.__unsubscribe();
112113
}
113114

114115
/** @protected */
@@ -133,12 +134,15 @@ export const DirMixin = (superClass) =>
133134
}
134135

135136
/** @private */
136-
__subscribe(push = true) {
137-
if (push) {
138-
if (!directionSubscribers.includes(this)) {
139-
directionSubscribers.push(this);
140-
}
141-
} else if (directionSubscribers.includes(this)) {
137+
__subscribe() {
138+
if (!directionSubscribers.includes(this)) {
139+
directionSubscribers.push(this);
140+
}
141+
}
142+
143+
/** @private */
144+
__unsubscribe() {
145+
if (directionSubscribers.includes(this)) {
142146
directionSubscribers.splice(directionSubscribers.indexOf(this), 1);
143147
}
144148
}

‎packages/component-base/test/dir-mixin.test.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ const runTests = (baseClass) => {
221221
await Promise.resolve();
222222
expect(element.getAttribute('dir')).to.eql('ltr');
223223
});
224+
225+
it('should keep custom direction when disconnecting and reconnecting', async () => {
226+
element.setAttribute('dir', 'ltr');
227+
setDir('rtl');
228+
await Promise.resolve();
229+
// Disconnect + reconnect
230+
document.body.appendChild(element);
231+
expect(element.getAttribute('dir')).to.eql('ltr');
232+
});
224233
});
225234
};
226235

0 commit comments

Comments
 (0)