Skip to content

Commit ed4206d

Browse files
authored
fix: avoid opened changed events when moving dialog within DOM (#5995)
* fix: avoid opened changed events when moving dialog within DOM * explicitly close confirm-dialog in tests before opening new one
1 parent 9f05789 commit ed4206d

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎packages/confirm-dialog/test/confirm-dialog.test.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ describe('vaadin-confirm-dialog', () => {
549549
await oneEvent(overlay, 'vaadin-overlay-open');
550550
});
551551

552+
afterEach(() => {
553+
confirm.opened = false;
554+
});
555+
552556
it('should update width after opening the dialog', () => {
553557
confirm._contentWidth = '300px';
554558
expect(getComputedStyle(overlay.$.overlay).width).to.be.equal('300px');

‎packages/dialog/src/vaadin-dialog.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,15 @@ class Dialog extends OverlayClassMixin(
269269
/** @protected */
270270
disconnectedCallback() {
271271
super.disconnectedCallback();
272-
// Close overlay and memorize opened state
273-
this.__restoreOpened = this.opened;
274-
this.opened = false;
272+
// Automatically close the overlay when dialog is removed from DOM
273+
// Using a timeout to avoid toggling opened state, and dispatching change
274+
// events, when just moving the dialog in the DOM
275+
setTimeout(() => {
276+
if (!this.isConnected) {
277+
this.__restoreOpened = this.opened;
278+
this.opened = false;
279+
}
280+
});
275281
}
276282

277283
/** @private */

‎packages/dialog/test/dialog.test.js‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
22
import { aTimeout, esc, fixtureSync, oneEvent } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
34
import '../vaadin-dialog.js';
45
import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js';
56

@@ -113,19 +114,44 @@ describe('vaadin-dialog', () => {
113114
});
114115
});
115116

116-
describe('detaching', () => {
117-
it('should close the overlay when detached', () => {
118-
dialog.parentNode.removeChild(dialog);
117+
describe('removing and adding to the DOM', () => {
118+
it('should close the overlay when removed from DOM', async () => {
119+
dialog.remove();
120+
await aTimeout(0);
121+
122+
expect(dialog.opened).to.be.false;
123+
});
124+
125+
it('should restore opened state when added to the DOM', async () => {
126+
const parent = dialog.parentNode;
127+
dialog.remove();
128+
await aTimeout(0);
119129
expect(dialog.opened).to.be.false;
130+
131+
parent.appendChild(dialog);
132+
expect(dialog.opened).to.be.true;
120133
});
121134

122-
it('should not close the overlay when moved within the DOM', () => {
135+
it('should not close the overlay when moved within the DOM', async () => {
123136
const newParent = document.createElement('div');
124137
document.body.appendChild(newParent);
125-
126138
newParent.appendChild(dialog);
139+
await aTimeout(0);
140+
127141
expect(dialog.opened).to.be.true;
128142
});
143+
144+
it('should not dispatch opened changed events when moved within the DOM', async () => {
145+
const onOpenedChanged = sinon.spy();
146+
dialog.addEventListener('opened-changed', onOpenedChanged);
147+
148+
const newParent = document.createElement('div');
149+
document.body.appendChild(newParent);
150+
newParent.appendChild(dialog);
151+
await aTimeout(0);
152+
153+
expect(onOpenedChanged.called).to.be.false;
154+
});
129155
});
130156

131157
describe('modeless', () => {

0 commit comments

Comments
 (0)