mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 02:24:09 +01:00
refactor(v2): detect nested menus through the escape stack
RMenu's click-outside check no longer keeps its own registry of child panels. Every open overlay already sits on the shared escape stack with a panel getter, in open order, so a new `isInsideEscapableAbove` helper answers "is this press inside something opened above me?" for nested menus, and for dialogs or drawers opened from a menu too. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S76JZm1jjh32KhBbPCBvGW
This commit is contained in:
@@ -34,7 +34,6 @@ import {
|
||||
import type { Placement } from "@floating-ui/vue";
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
nextTick,
|
||||
onBeforeUnmount,
|
||||
onMounted,
|
||||
@@ -50,11 +49,12 @@ import { opensInNewContext } from "@/v2/utils/mouseGestures";
|
||||
import RTextField from "../../forms/RTextField/RTextField.vue";
|
||||
import {
|
||||
type EscapableEntry,
|
||||
isInsideEscapableAbove,
|
||||
popEscapable,
|
||||
pushEscapable,
|
||||
} from "../../overlays/RDialog/escapeStack.js";
|
||||
import RIcon from "../../primitives/RIcon/RIcon.vue";
|
||||
import { RMenuCloseKey, RMenuNestingKey } from "./context";
|
||||
import { RMenuCloseKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
@@ -168,19 +168,6 @@ function toggle() {
|
||||
|
||||
provide(RMenuCloseKey, close);
|
||||
|
||||
// Nested menus teleport their panels outside this one; tracking them keeps a
|
||||
// click inside a child menu from closing this menu.
|
||||
const nestedPanels = new Set<() => HTMLElement | null>();
|
||||
const registerWithParent = inject(RMenuNestingKey, null);
|
||||
provide(RMenuNestingKey, (panel) => {
|
||||
nestedPanels.add(panel);
|
||||
const unregisterFromParent = registerWithParent?.(panel);
|
||||
return () => {
|
||||
nestedPanels.delete(panel);
|
||||
unregisterFromParent?.();
|
||||
};
|
||||
});
|
||||
|
||||
// ── Refs ────────────────────────────────────────────────────────
|
||||
// The activator slot renders inside a `display: contents` span; we
|
||||
// read its first child as the floating-ui reference (the actual
|
||||
@@ -321,9 +308,8 @@ function onDocPointerDown(evt: PointerEvent) {
|
||||
)
|
||||
return;
|
||||
if (panelRef.value?.contains(target)) return;
|
||||
for (const panel of nestedPanels) {
|
||||
if (panel()?.contains(target)) return;
|
||||
}
|
||||
// A nested menu's panel is teleported outside this one.
|
||||
if (isInsideEscapableAbove(escEntry, target)) return;
|
||||
close();
|
||||
}
|
||||
|
||||
@@ -352,14 +338,11 @@ watch(
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
const unregisterPanel = registerWithParent?.(() => panelRef.value);
|
||||
|
||||
onMounted(() => {
|
||||
reference.value = activatorWrapper.value?.firstElementChild ?? null;
|
||||
document.addEventListener("pointerdown", onDocPointerDown, true);
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
unregisterPanel?.();
|
||||
document.removeEventListener("pointerdown", onDocPointerDown, true);
|
||||
// Safety: if we unmount while open (route change while the menu is
|
||||
// visible) drop our entry so the stack doesn't dereference a dead
|
||||
|
||||
@@ -4,10 +4,3 @@
|
||||
import type { InjectionKey } from "vue";
|
||||
|
||||
export const RMenuCloseKey: InjectionKey<() => void> = Symbol("RMenuClose");
|
||||
|
||||
/** A nested menu registers its panel with every ancestor menu, so a click
|
||||
* inside it does not count as a click outside theirs. Returns an unregister. */
|
||||
export type RMenuNesting = (panel: () => HTMLElement | null) => () => void;
|
||||
|
||||
export const RMenuNestingKey: InjectionKey<RMenuNesting> =
|
||||
Symbol("RMenuNesting");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
type EscapableEntry,
|
||||
isInsideEscapableAbove,
|
||||
isUnderOpenEscapable,
|
||||
onEscapableOpen,
|
||||
popEscapable,
|
||||
@@ -85,3 +86,26 @@ describe("isUnderOpenEscapable", () => {
|
||||
expect(isUnderOpenEscapable(document.createElement("button"))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isInsideEscapableAbove", () => {
|
||||
it("finds a node in an overlay opened after the entry, not before it", () => {
|
||||
const outerPanel = document.createElement("div");
|
||||
const innerPanel = document.createElement("div");
|
||||
const inInner = innerPanel.appendChild(document.createElement("input"));
|
||||
const inOuter = outerPanel.appendChild(document.createElement("button"));
|
||||
const outer = entry(outerPanel);
|
||||
const inner = entry(innerPanel);
|
||||
push(outer);
|
||||
push(inner);
|
||||
|
||||
expect(isInsideEscapableAbove(outer, inInner)).toBe(true);
|
||||
expect(isInsideEscapableAbove(outer, inOuter)).toBe(false);
|
||||
expect(isInsideEscapableAbove(inner, inOuter)).toBe(false);
|
||||
});
|
||||
|
||||
it("is false for an entry that is not open", () => {
|
||||
const panel = document.createElement("div");
|
||||
push(entry(panel));
|
||||
expect(isInsideEscapableAbove(entry(), panel)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -82,6 +82,19 @@ export function isUnderOpenEscapable(el: Node | null): boolean {
|
||||
return !panel.contains(el);
|
||||
}
|
||||
|
||||
/** True when `node` sits in an overlay opened after `entry`, such as a menu
|
||||
* nested in it, so a press there does not count as outside `entry`. */
|
||||
export function isInsideEscapableAbove(
|
||||
entry: EscapableEntry,
|
||||
node: Node,
|
||||
): boolean {
|
||||
const idx = stack.indexOf(entry);
|
||||
if (idx === -1) return false;
|
||||
return stack
|
||||
.slice(idx + 1)
|
||||
.some((above) => above.panel?.()?.contains(node) ?? false);
|
||||
}
|
||||
|
||||
/** True when at least one non-persistent escapable overlay is open.
|
||||
* Persistent layers still count as "open" — they block back-style
|
||||
* dismiss the same way Esc is a no-op for them, so the user gets
|
||||
|
||||
Reference in New Issue
Block a user