89 lines
6.7 KiB
TypeScript
89 lines
6.7 KiB
TypeScript
import { actionIcon, type ActionIcon } from "./actionIcons.ts";
|
|
|
|
export type ActionMenuItem = { label: string; icon?: ActionIcon; shortcut?: string; run?: () => void; children?: ActionMenuItem[]; disabled?: boolean; checked?: boolean; radio?: boolean; title?: string } | { separator: true };
|
|
|
|
export function actionMenu(x: number, y: number, items: ActionMenuItem[], onClose: () => void, label = "Table actions", parent: HTMLElement = document.body) {
|
|
const root = document.createElement("div"); root.className = "rs-menu table-cell-menu"; root.setAttribute("role", "menu"); root.setAttribute("aria-label", label);
|
|
const opened: { button: HTMLButtonElement; panel: HTMLElement }[] = [];
|
|
if (label.startsWith("Terminal")) root.style.width = "min(430px, calc(100vw - 16px))";
|
|
const submenus = new Map<HTMLButtonElement, HTMLElement>();
|
|
function focusRow(button?: HTMLButtonElement) {
|
|
if (!button) return;
|
|
button.focus({ preventScroll: true });
|
|
const panel = button.parentElement!;
|
|
const row = button.getBoundingClientRect(), bounds = panel.getBoundingClientRect();
|
|
if (row.top < bounds.top) panel.scrollTop -= bounds.top - row.top;
|
|
else if (row.bottom > bounds.bottom) panel.scrollTop += row.bottom - bounds.bottom;
|
|
}
|
|
function hideSubmenu(parent: HTMLElement = root) {
|
|
const keep = parent === root ? 0 : opened.findIndex(entry => entry.panel === parent) + 1;
|
|
for (const { button, panel } of opened.splice(keep)) { panel.hidden = true; button.setAttribute("aria-expanded", "false"); }
|
|
}
|
|
function positionSubmenu(button: HTMLButtonElement, panel: HTMLElement) {
|
|
const rect = button.getBoundingClientRect(), size = panel.getBoundingClientRect(), main = button.parentElement!.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.some(entry => entry.panel === panel)) { hideSubmenu(button.parentElement!); opened.push({ button, panel }); }
|
|
panel.hidden = false; button.setAttribute("aria-expanded", "true");
|
|
positionSubmenu(button, panel);
|
|
if (focus) focusRow(panel.querySelector<HTMLButtonElement>("button:not(:disabled)") ?? undefined);
|
|
}
|
|
function populate(panel: HTMLElement, entries: ActionMenuItem[]) {
|
|
const hasIcons = entries.some(item => !("separator" in item) && (!!item.icon || (item.checked !== undefined && !item.radio)));
|
|
panel.classList.toggle("rs-menu-text", !hasIcons);
|
|
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.checked !== undefined) { button.setAttribute("role", item.radio ? "menuitemradio" : "menuitemcheckbox"); button.setAttribute("aria-checked", String(item.checked)); }
|
|
if (item.title) button.title = item.title;
|
|
const label = document.createElement("span"); label.className = "action-label"; label.textContent = item.label;
|
|
const icon = item.checked && !item.radio ? actionIcon("task") : item.icon ? actionIcon(item.icon) : document.createElement("span");
|
|
icon.classList.add("action-icon"); icon.setAttribute("aria-hidden", "true");
|
|
button.append(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 = "rs-menu 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", event => {
|
|
event.stopPropagation();
|
|
if (item.run && !(event.target instanceof Node && arrow.contains(event.target))) item.run();
|
|
else reveal(button, submenu, true);
|
|
});
|
|
} else {
|
|
button.addEventListener("mousemove", () => hideSubmenu(panel));
|
|
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 (!opened.some(entry => entry.button === button) && button.parentElement) hideSubmenu(button.parentElement);
|
|
});
|
|
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(); focusRow(buttons[event.key === "Home" ? 0 : event.key === "End" ? buttons.length - 1 : (index + (event.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length]);
|
|
}
|
|
if (event.key === "ArrowRight" && submenus.has(focused)) { event.preventDefault(); reveal(focused, submenus.get(focused)!, true); }
|
|
const entry = opened.find(entry => entry.panel === panel);
|
|
if (event.key === "ArrowLeft" && entry) { event.preventDefault(); hideSubmenu(entry.button.parentElement!); focusRow(entry.button); }
|
|
});
|
|
root.addEventListener("scroll", () => { for (const { button, panel } of opened) positionSubmenu(button, panel); }, true);
|
|
parent.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[role="menuitemradio"][aria-checked="true"]:not(:disabled)')
|
|
?? root.querySelector<HTMLButtonElement>(":scope > button:not(:disabled)"))?.focus({ preventScroll: true });
|
|
return root;
|
|
}
|