Right-clicking a line in the dialogue editor put the markup menu off to one side of the panel and stretched the panel as it overflowed, which is not what a context menu should do. The menu was rendered inline with position: fixed. The edit dialog's content carries a transform for its open animation, and a transformed ancestor becomes the containing block for fixed positioning, so the pointer's viewport coordinates landed at those coordinates inside the dialog. It is portalled to the body now, which detaches it from that ancestor, and still positioned at the pointer. Three things had to follow from the portal: - The admin stacks its dialog at 1300 and its own overlays up to 9999, so the menu is placed at 10000 rather than underneath the panel. - Payload styles every button with a light surface, which painted pale blocks over the dark menu; item buttons are explicitly transparent now. - A Radix modal sets pointer-events: none on the body while open, and a body portal inherits it, leaving the menu visible but unclickable. It carries pointer-events-auto. Placement is a pure clampMenuPosition: the point is pulled inside the viewport so a right-click near an edge cannot open a menu the author cannot reach. The menu also closes on resize and on any scroll, so it never detaches from the box. Verified in the running editor: the menu's rect starts exactly at the click point (750, 308), its parent is the body, pointer-events is auto, the dialog reports no overflow, and clicking the first item wrapped the caret with **. 4 unit tests cover the clamp; 227 npc-dialogue tests pass.
39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
MENU_HEIGHT,
|
|
MENU_MARGIN,
|
|
MENU_WIDTH,
|
|
clampMenuPosition,
|
|
} from "@/lib/npc-dialogue/markupMenu";
|
|
|
|
const viewport = { width: 1500, height: 1000 };
|
|
|
|
describe("markup menu placement", () => {
|
|
it("sits exactly at the pointer away from the edges", () => {
|
|
expect(clampMenuPosition(750, 308, viewport)).toEqual({ left: 750, top: 308 });
|
|
});
|
|
|
|
it("pulls back from the right and bottom edges so every item stays reachable", () => {
|
|
const point = clampMenuPosition(1499, 999, viewport);
|
|
expect(point.left).toBe(viewport.width - MENU_WIDTH - MENU_MARGIN);
|
|
expect(point.top).toBe(viewport.height - MENU_HEIGHT - MENU_MARGIN);
|
|
expect(point.left + MENU_WIDTH).toBeLessThanOrEqual(viewport.width);
|
|
expect(point.top + MENU_HEIGHT).toBeLessThanOrEqual(viewport.height);
|
|
});
|
|
|
|
it("keeps the margin when the viewport is smaller than the menu", () => {
|
|
const small = { width: 120, height: 100 };
|
|
expect(clampMenuPosition(119, 99, small)).toEqual({
|
|
left: MENU_MARGIN,
|
|
top: MENU_MARGIN,
|
|
});
|
|
});
|
|
|
|
it("never returns a negative offset for a pointer outside the window", () => {
|
|
const point = clampMenuPosition(-40, -40, viewport);
|
|
expect(point.left).toBeGreaterThanOrEqual(MENU_MARGIN);
|
|
expect(point.top).toBeGreaterThanOrEqual(MENU_MARGIN);
|
|
});
|
|
});
|