Skip to content

Commit

Permalink
fix(modal): buggy click handlers to close modal on outside click
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Oct 31, 2023
1 parent 141f1ed commit b808643
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 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 @@ -95,7 +102,8 @@ export class Modal extends Block {
super.destroyCallback();
document.body.style.overflow = '';
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 b808643

Please sign in to comment.