Skip to content

Commit

Permalink
fix(modal): buggy click handlers to close modal on outside click (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut authored Oct 31, 2023
1 parent 8babfab commit 8949948
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions blocks/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ export class Modal extends Block {
};

/** @param {Event} e */
_handleDialogPointerUp = (e) => {
if (e.target === this.ref.dialog) {
_handleDialogMouseDown = (e) => {
/** @private */
this._mouseDownTarget = e.target;
};

/** @param {Event} e */
_handleDialogMouseUp = (e) => {
if (e.target === this.ref.dialog && e.target === this._mouseDownTarget) {
this._closeDialog();
}
};
Expand All @@ -53,7 +59,8 @@ export class Modal extends Block {
super.initCallback();
if (typeof HTMLDialogElement === 'function') {
this.ref.dialog.addEventListener('close', this._handleDialogClose);
this.ref.dialog.addEventListener('pointerup', this._handleDialogPointerUp);
this.ref.dialog.addEventListener('mousedown', this._handleDialogMouseDown);
this.ref.dialog.addEventListener('mouseup', this._handleDialogMouseUp);
} else {
this.setAttribute('dialog-fallback', '');
let backdrop = document.createElement('div');
Expand Down Expand Up @@ -94,8 +101,10 @@ export class Modal extends Block {
destroyCallback() {
super.destroyCallback();
document.body.style.overflow = '';
this._mouseDownTarget = undefined;
this.ref.dialog.removeEventListener('close', this._handleDialogClose);
this.ref.dialog.removeEventListener('click', this._handleDialogPointerUp);
this.ref.dialog.removeEventListener('mousedown', this._handleDialogMouseDown);
this.ref.dialog.removeEventListener('mouseup', this._handleDialogMouseUp);
}
}

Expand Down

0 comments on commit 8949948

Please sign in to comment.