Skip to content

Commit

Permalink
fix: restore overlay opened state when moving within the DOM (#3648) (#…
Browse files Browse the repository at this point in the history
…3665)

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
  • Loading branch information
vaadin-bot and sissbruecker committed Apr 11, 2022
1 parent ba1b2b0 commit 9f7cfe8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/dialog/src/vaadin-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,20 @@ class Dialog extends ThemePropertyMixin(ElementMixin(DialogDraggableMixin(Dialog
this.$.overlay.setProperties({ owner: this, renderer });
}

/** @protected */
connectedCallback() {
super.connectedCallback();
// Restore opened state if overlay was opened when disconnecting
if (this.__restoreOpened) {
this.opened = true;
}
}

/** @protected */
disconnectedCallback() {
super.disconnectedCallback();
// Close overlay and memorize opened state
this.__restoreOpened = this.opened;
this.opened = false;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/dialog/test/dialog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ describe('vaadin-dialog', () => {
dialog.parentNode.removeChild(dialog);
expect(dialog.opened).to.be.false;
});

it('should not close the overlay when moved within the DOM', () => {
const newParent = document.createElement('div');
document.body.appendChild(newParent);

newParent.appendChild(dialog);
expect(dialog.opened).to.be.true;
});
});

describe('modeless', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/login/src/vaadin-login-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class LoginOverlay extends LoginMixin(ElementMixin(ThemableMixin(PolymerElement)

this.$.vaadinLoginOverlayWrapper.addEventListener('vaadin-overlay-outside-click', this._preventClosingLogin);
this.$.vaadinLoginOverlayWrapper.addEventListener('vaadin-overlay-escape-press', this._preventClosingLogin);

// Restore opened state if overlay was open when disconnecting
if (this.__restoreOpened) {
this.$.vaadinLoginOverlayWrapper.opened = true;
}
}

/** @protected */
Expand All @@ -135,6 +140,9 @@ class LoginOverlay extends LoginMixin(ElementMixin(ThemableMixin(PolymerElement)

this.$.vaadinLoginOverlayWrapper.removeEventListener('vaadin-overlay-outside-click', this._preventClosingLogin);
this.$.vaadinLoginOverlayWrapper.removeEventListener('vaadin-overlay-escape-press', this._preventClosingLogin);

// Close overlay and memorize opened state
this.__restoreOpened = this.$.vaadinLoginOverlayWrapper.opened;
this.$.vaadinLoginOverlayWrapper.opened = false;
}

Expand Down
12 changes: 10 additions & 2 deletions packages/login/test/login-overlay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ describe('opened overlay', () => {

it('should set opened using attribute', () => {
expect(overlay.opened).to.be.true;
expect(document.querySelector('vaadin-login-form-wrapper')).to.be.ok;
expect(document.querySelector('vaadin-login-form-wrapper')).to.exist;
});

it('should remove form wrapper when closed', () => {
overlay.opened = false;
expect(document.querySelector('vaadin-login-form-wrapper')).to.be.not.ok;
expect(document.querySelector('vaadin-login-form-wrapper')).not.to.exist;
});

it('should not remove form wrapper when moved within DOM', () => {
const newParent = document.createElement('div');
document.body.appendChild(newParent);
newParent.appendChild(overlay);

expect(document.querySelector('vaadin-login-form-wrapper')).to.exist;
});

it('should propagate theme to a wrapper', () => {
Expand Down

0 comments on commit 9f7cfe8

Please sign in to comment.