Update the browser, editor, settings, and installer
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
// Stroke icons share the same 24px grid and weight as IconButton.
|
||||
export const actionIcons = {
|
||||
archive: '<path d="M4 8v12h16V8M3 3h18v5H3zM9 12h6"/>',
|
||||
save: '<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><path d="M17 21v-8H7v8M7 3v5h8"/>',
|
||||
refresh: '<path d="M20 7v5h-5M4 17v-5h5M6 7a7 7 0 0 1 12-1l2 3M4 15l2 3a7 7 0 0 0 12-1"/>',
|
||||
'arrow-left': '<path d="M20 12H4m6-6-6 6 6 6"/>',
|
||||
'arrow-right': '<path d="M4 12h16m-6-6 6 6-6 6"/>',
|
||||
terminal: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="m6 8 4 4-4 4m6 0h6"/>',
|
||||
undo: '<path d="M3 10h11a7 7 0 0 1 0 14" transform="translate(0 -4)"/><path d="m7 2-4 4 4 4"/>',
|
||||
redo: '<path d="M21 6H10a7 7 0 0 0 0 14"/><path d="m17 2 4 4-4 4"/>',
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { actionIcon, type ActionIcon } from "./actionIcons";
|
||||
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 };
|
||||
|
||||
@@ -23,13 +23,15 @@ export function actionMenu(x: number, y: number, items: ActionMenuItem[], onClos
|
||||
if (focus) panel.querySelector<HTMLButtonElement>("button:not(:disabled)")?.focus({ preventScroll: true });
|
||||
}
|
||||
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 ? actionIcon("task") : item.icon ? actionIcon(item.icon) : document.createElement("span");
|
||||
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); }
|
||||
@@ -39,7 +41,11 @@ export function actionMenu(x: number, y: number, items: ActionMenuItem[], onClos
|
||||
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", () => reveal(button, submenu, true));
|
||||
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?.(); });
|
||||
@@ -68,6 +74,7 @@ export function actionMenu(x: number, y: number, items: ActionMenuItem[], onClos
|
||||
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:not(:disabled)")?.focus();
|
||||
(root.querySelector<HTMLButtonElement>(':scope > button[role="menuitemradio"][aria-checked="true"]:not(:disabled)')
|
||||
?? root.querySelector<HTMLButtonElement>(":scope > button:not(:disabled)"))?.focus();
|
||||
return root;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { actionMenu, type ActionMenuItem } from "./actionMenu.ts";
|
||||
|
||||
export type MenuOptions = { x: number; y: number; items: ActionMenuItem[]; label?: string; parent?: HTMLElement };
|
||||
|
||||
export function showActionMenu(options: MenuOptions, signal?: AbortSignal): { close(): void } {
|
||||
const origin = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
// Focus can scroll a trigger before its queued scroll event reaches this menu.
|
||||
const scrollPositions = new Map<EventTarget, [number, number]>();
|
||||
for (let node: HTMLElement | null = origin; node; node = node.parentElement) {
|
||||
scrollPositions.set(node, [node.scrollLeft, node.scrollTop]);
|
||||
}
|
||||
scrollPositions.set(document, [window.scrollX, window.scrollY]);
|
||||
let root: HTMLElement | undefined;
|
||||
const close = (restore = false) => {
|
||||
root?.remove();
|
||||
window.removeEventListener("pointerdown", outside, true);
|
||||
window.removeEventListener("resize", dismiss);
|
||||
window.removeEventListener("blur", dismiss);
|
||||
document.removeEventListener("scroll", scroll, true);
|
||||
signal?.removeEventListener("abort", dismiss);
|
||||
if (restore && origin?.isConnected) origin.focus({ preventScroll: true });
|
||||
};
|
||||
const dismiss = () => close();
|
||||
const outside = (event: PointerEvent) => { if (!root?.contains(event.target as Node)) close(); };
|
||||
const scroll = (event: Event) => {
|
||||
if (root?.contains(event.target as Node)) return;
|
||||
const before = event.target && scrollPositions.get(event.target);
|
||||
const current = event.target === document ? [window.scrollX, window.scrollY]
|
||||
: event.target instanceof HTMLElement ? [event.target.scrollLeft, event.target.scrollTop] : undefined;
|
||||
if (!before || !current || before[0] !== current[0] || before[1] !== current[1]) close();
|
||||
};
|
||||
const wrap = (items: ActionMenuItem[]): ActionMenuItem[] => items.map(item => "separator" in item ? item : {
|
||||
...item,
|
||||
...(item.children ? { children: wrap(item.children) } : {}),
|
||||
...(!item.children || item.run ? { run: () => { close(true); if (!signal?.aborted) item.run?.(); } } : {}),
|
||||
});
|
||||
root = actionMenu(options.x, options.y, wrap(options.items), () => close(true), options.label ?? "Actions", options.parent);
|
||||
window.addEventListener("pointerdown", outside, true);
|
||||
window.addEventListener("resize", dismiss);
|
||||
window.addEventListener("blur", dismiss);
|
||||
document.addEventListener("scroll", scroll, true);
|
||||
signal?.addEventListener("abort", dismiss, { once: true });
|
||||
if (signal?.aborted) close();
|
||||
return { close: () => close() };
|
||||
}
|
||||
+6
-3
@@ -6,7 +6,7 @@
|
||||
max-height: calc(100vh - 16px);
|
||||
overflow-y: auto;
|
||||
padding: var(--rs-menu-padding);
|
||||
border: 1px solid var(--rs-border);
|
||||
border: 1px solid var(--rs-menu-border);
|
||||
border-radius: var(--rs-control-radius);
|
||||
background: var(--rs-bg-elevated);
|
||||
color: var(--rs-text);
|
||||
@@ -33,10 +33,13 @@
|
||||
}
|
||||
|
||||
.rs-menu > button:disabled { opacity: .4; cursor: default; }
|
||||
.rs-menu > button[role="menuitemradio"][aria-checked="true"] { background: var(--rs-bg-active); }
|
||||
.rs-menu.rs-menu-text > button > .action-icon { display: none; }
|
||||
.rs-menu > button:not(:disabled):is(:hover, :focus-visible, [aria-expanded="true"]) {
|
||||
background: var(--rs-bg-active);
|
||||
outline: none;
|
||||
}
|
||||
.rs-menu > hr { margin: 4px 0; border: 0; border-top: 1px solid var(--rs-border); }
|
||||
.rs-menu > button > .action-icon { width: var(--rs-menu-icon-size); height: var(--rs-menu-icon-size); flex-basis: var(--rs-menu-icon-size); }
|
||||
.rs-menu > button > .action-chevron { width: 13px; height: 13px; flex-basis: 13px; }
|
||||
.rs-menu > button > .action-icon { width: var(--rs-menu-icon-size); height: var(--rs-menu-icon-size); flex: 0 0 var(--rs-menu-icon-size); }
|
||||
.rs-menu > button > .action-label { flex: 1; min-width: 0; overflow-wrap: anywhere; }
|
||||
.rs-menu > button > .action-chevron { width: 13px; height: 13px; flex: 0 0 13px; }
|
||||
|
||||
Reference in New Issue
Block a user