67 lines
5.1 KiB
TypeScript
67 lines
5.1 KiB
TypeScript
import { actionIcon, type ActionIcon } from "./actionIcons";
|
|
|
|
export type ActionMenuItem = { label: string; icon: ActionIcon; shortcut?: string; run?: () => void; children?: ActionMenuItem[]; disabled?: boolean; title?: string } | { separator: true };
|
|
|
|
export function actionMenu(x: number, y: number, items: ActionMenuItem[], onClose: () => void, label = "Table actions") {
|
|
const root = document.createElement("div"); root.className = "table-cell-menu"; root.setAttribute("role", "menu"); root.setAttribute("aria-label", label);
|
|
let opened: HTMLElement | undefined, owner: HTMLButtonElement | undefined;
|
|
if (label.startsWith("Terminal")) root.style.width = "min(430px, calc(100vw - 16px))";
|
|
const submenus = new Map<HTMLButtonElement, HTMLElement>();
|
|
function hideSubmenu() { if (opened) opened.hidden = true; owner?.setAttribute("aria-expanded", "false"); opened = undefined; owner = undefined; }
|
|
function positionSubmenu(button: HTMLButtonElement, panel: HTMLElement) {
|
|
const rect = button.getBoundingClientRect(), size = panel.getBoundingClientRect(), main = root.getBoundingClientRect();
|
|
panel.style.left = `${Math.max(8, main.right + size.width < innerWidth - 8 ? main.right - 2 : main.left - size.width + 2)}px`;
|
|
panel.style.top = `${Math.max(8, Math.min(rect.top - 5, innerHeight - size.height - 8))}px`;
|
|
}
|
|
function reveal(button: HTMLButtonElement, panel: HTMLElement, focus = false) {
|
|
if (opened !== panel) hideSubmenu();
|
|
opened = panel; owner = button; panel.hidden = false; button.setAttribute("aria-expanded", "true");
|
|
positionSubmenu(button, panel);
|
|
if (focus) panel.querySelector<HTMLButtonElement>("button:not(:disabled)")?.focus({ preventScroll: true });
|
|
}
|
|
function populate(panel: HTMLElement, entries: ActionMenuItem[]) {
|
|
for (const item of entries) {
|
|
if ("separator" in item) { const hr = document.createElement("hr"); hr.setAttribute("role", "separator"); panel.append(hr); continue; }
|
|
const button = document.createElement("button"); button.type = "button"; button.setAttribute("role", "menuitem"); button.disabled = !!item.disabled;
|
|
if (item.title) button.title = item.title;
|
|
const label = document.createElement("span"); label.className = "action-label"; label.textContent = item.label;
|
|
button.append(actionIcon(item.icon), label); panel.append(button);
|
|
if (item.shortcut) { const shortcut = document.createElement("span"); shortcut.textContent = item.shortcut; shortcut.style.cssText = "margin-left:auto;color:var(--rs-text-dim);font-size:12px;white-space:nowrap"; button.append(shortcut); }
|
|
if (item.children) {
|
|
button.setAttribute("aria-haspopup", "menu"); button.setAttribute("aria-expanded", "false");
|
|
const arrow = actionIcon("chevron"); arrow.classList.add("action-chevron"); button.append(arrow);
|
|
const submenu = document.createElement("div"); submenu.className = "table-cell-submenu"; submenu.setAttribute("role", "menu"); submenu.setAttribute("aria-label", item.label); submenu.hidden = true;
|
|
populate(submenu, item.children); root.append(submenu); submenus.set(button, submenu);
|
|
button.addEventListener("mousemove", () => { if (!button.disabled) reveal(button, submenu); });
|
|
button.addEventListener("click", () => reveal(button, submenu, true));
|
|
} else {
|
|
if (panel === root) button.addEventListener("mousemove", hideSubmenu);
|
|
button.addEventListener("click", event => { event.stopPropagation(); item.run?.(); });
|
|
}
|
|
}
|
|
}
|
|
populate(root, items);
|
|
root.addEventListener("mousedown", event => { event.preventDefault(); event.stopPropagation(); });
|
|
root.addEventListener("focusin", event => {
|
|
const button = event.target as HTMLElement;
|
|
if (button.parentElement === root && button !== owner) hideSubmenu();
|
|
});
|
|
root.addEventListener("keydown", event => {
|
|
event.stopPropagation();
|
|
if (event.key === "Escape" || event.key === "Tab") { event.preventDefault(); onClose(); return; }
|
|
const focused = document.activeElement as HTMLButtonElement, panel = focused.parentElement || root;
|
|
const buttons = [...panel.querySelectorAll<HTMLButtonElement>(":scope > button:not(:disabled)")], index = buttons.indexOf(focused);
|
|
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
|
|
event.preventDefault(); buttons[event.key === "Home" ? 0 : event.key === "End" ? buttons.length - 1 : (index + (event.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length]?.focus();
|
|
}
|
|
if (event.key === "ArrowRight" && submenus.has(focused)) { event.preventDefault(); reveal(focused, submenus.get(focused)!, true); }
|
|
if (event.key === "ArrowLeft" && opened?.contains(focused)) { event.preventDefault(); const previous = owner; hideSubmenu(); previous?.focus(); }
|
|
});
|
|
root.addEventListener("scroll", event => { if (event.target === root && opened && owner) positionSubmenu(owner, opened); });
|
|
document.body.append(root);
|
|
root.style.left = `${Math.max(8, Math.min(x, innerWidth - root.offsetWidth - 8))}px`;
|
|
root.style.top = `${Math.max(8, Math.min(y, innerHeight - root.offsetHeight - 8))}px`;
|
|
root.querySelector<HTMLButtonElement>(":scope > button:not(:disabled)")?.focus();
|
|
return root;
|
|
}
|