Skip to content

Commit

Permalink
fix(menus): hide behavior for dialog children
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaraa committed Jun 13, 2024
1 parent b87419e commit c71783d
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions app/static/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,54 @@ function copyTextToClipboard(text) {
}

function menuer() {
const menus = [];
/**
* @type {HTMLDivElement}
*/
let lastEl = null;

/**
* @param {string} id
*/
const __registerPopover = (id) => {
menus.push(id);
document.body.addEventListener("click", __remove);
if (!id) {
return;
}
if (!!lastEl) {
lastEl.style.display = "none";
document.body.removeEventListener("click", __removePopover);
}
lastEl = document.getElementById(id);
if (!lastEl) {
return;
}
document.body.addEventListener("click", __removePopover);
};

/**
* @param {MouseEvent} e
*/
const __remove = (e) => {
for (let i = 0; i < menus.length; i++) {
const el = document.getElementById(menus[i]);
if (!el) {
continue;
}
const rect = el.getBoundingClientRect();
const parentRect = el.parentElement.getBoundingClientRect();
if (
e.clientX < rect.left ||
e.clientX > rect.right ||
e.clientY + parentRect.height + 5 < rect.top ||
e.clientY > rect.bottom
) {
menus.splice(i, 1);
el.style.display = "none";
const __removePopover = (e) => {
const rect = lastEl.getBoundingClientRect();
const parentRect = lastEl.parentElement.getBoundingClientRect();
let popupChild = null;
for (const c of lastEl.children.item(0)?.children) {
if (c.tagName === "DIALOG") {
popupChild = c;
}
}
if (document.activeElement === popupChild) {
return;
}
if (
e.clientX < rect.left ||
e.clientX > rect.right ||
e.clientY + parentRect.height + 5 < rect.top ||
e.clientY > rect.bottom
) {
lastEl.style.display = "none";
lastEl = null;
document.body.removeEventListener("click", __removePopover);
}
};

return [__registerPopover];
Expand Down

0 comments on commit c71783d

Please sign in to comment.