document.body.addEventListener("htmx:load", function (evt) {
  if (evt.originalTarget.id == "htmx-dialog") {
    let modalBody = document.querySelector("#htmx-dialog .modal-body");
    
    // close on container or close button click
    document.addEventListener("click", function (e) {
      if (e.target.id == 'htmx-dialog-container' || e.target.classList.contains('btn-close')) {
        modalBody.innerHTML = "";      
      }
    });

    //close on escape key
    document.onkeydown = (e) => {
      e = e || window.event;
      if (e.keyCode == 27) {
        modalBody.innerHTML = "";
      }
    };

  }
});