Skip to content

Commit

Permalink
Merge pull request #18 from blasferna/modal-over-bs-modal
Browse files Browse the repository at this point in the history
Resolves unexpected closure of nested modals when the parent is a Bootstrap modal
  • Loading branch information
blasferna authored Mar 22, 2024
2 parents e93fc66 + 0226ef0 commit a7db759
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions js/src/modal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { uuidv4, templatePacks } from "./utils";
import $ from "jquery";
import { templatePacks, uuidv4 } from "./utils";

const modalDefault = {
placement: "center",
Expand All @@ -18,6 +19,7 @@ class Modal {
...options,
};
this._isHidden = true;
this._bsParent = this._parentEl.closest(".modal");
this._init();
this._addEventListeners();
}
Expand Down Expand Up @@ -129,7 +131,8 @@ class Modal {
_addEventListeners() {
let that = this;
this._targetEl.addEventListener("keyup", function (e) {
if (e.key === "Escape") {
if (e.which === 27) {
e.preventDefault();
that.hide();
}
});
Expand Down Expand Up @@ -169,6 +172,10 @@ class Modal {
}
this._targetEl.offsetWidth;
this._targetEl.classList.add(...this._getClasses("opacity100"));

if (this._bsParent) {
$(this._bsParent).off("keydown.dismiss.bs.modal");
}
}
hide() {
this._targetEl.classList.add(...this._getClasses("hidden"));
Expand All @@ -183,6 +190,16 @@ class Modal {

// callback function
this._options.onHide(this);

if (this._bsParent) {
this._bsParent.focus();
$(this._bsParent).on("keydown.dismiss.bs.modal", (e) => {
if (e.which === 27) {
e.preventDefault();
$(this._bsParent).modal("hide");
}
});
}
}
}

Expand Down

0 comments on commit a7db759

Please sign in to comment.