fix(npc-dialogue): open the markup menu at the pointer, above the panel
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.
This commit is contained in:
parent
ce534230a5
commit
297c39c3e2
3 changed files with 152 additions and 20 deletions
|
|
@ -1,8 +1,10 @@
|
|||
"use client";
|
||||
|
||||
import { memo, useEffect, useRef, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
|
||||
import { DialogueText } from "@/lib/npc-dialogue/DialogueText";
|
||||
import { clampMenuPosition } from "@/lib/npc-dialogue/markupMenu";
|
||||
|
||||
/** Friendly color names offered by the color picker (mirrors DialogueText). */
|
||||
const COLOR_CHOICES = [
|
||||
|
|
@ -29,6 +31,66 @@ const COLOR_CHOICES = [
|
|||
*/
|
||||
const PREVIEW_DEBOUNCE_MS = 220;
|
||||
|
||||
/**
|
||||
* The markup menu, portalled to the document body.
|
||||
*
|
||||
* The editor lives inside a Radix dialog, whose content carries a transform for
|
||||
* its open animation. A transformed ancestor becomes the containing block for
|
||||
* `position: fixed`, so a menu positioned at the cursor's viewport coordinates
|
||||
* landed at those coordinates *inside the dialog* instead: off to one side, and
|
||||
* widening the panel as it overflowed. Portalling detaches it from that ancestor.
|
||||
*/
|
||||
function MarkupMenu({
|
||||
x,
|
||||
y,
|
||||
items,
|
||||
onPick,
|
||||
}: {
|
||||
x: number;
|
||||
y: number;
|
||||
items: { label: string; run: () => void }[];
|
||||
onPick: () => void;
|
||||
}) {
|
||||
if (typeof document === "undefined") return null;
|
||||
// Pulled inside the viewport: a right-click near an edge would otherwise open
|
||||
// a menu the author cannot reach.
|
||||
const { left, top } = clampMenuPosition(x, y, {
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight,
|
||||
});
|
||||
return createPortal(
|
||||
<div
|
||||
role="menu"
|
||||
// Above every admin layer: the dialog sits at 1300 and Payload's own
|
||||
// overlays go as high as 9999, so a portalled menu below them would be
|
||||
// painted underneath the panel it belongs to.
|
||||
// `pointer-events-auto` is required: a Radix modal puts `pointer-events:
|
||||
// none` on the body while it is open, and a portal to the body inherits it,
|
||||
// which left the menu visible but unclickable.
|
||||
className="pointer-events-auto fixed z-[10000] flex min-w-40 flex-col rounded border border-zinc-700 bg-zinc-900 py-1 text-white shadow-xl"
|
||||
style={{ left, top }}
|
||||
onMouseDown={(event) => event.stopPropagation()}
|
||||
>
|
||||
{items.map((item) => (
|
||||
<button
|
||||
key={item.label}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
item.run();
|
||||
onPick();
|
||||
}}
|
||||
// `bg-transparent` is not redundant: the admin styles every button
|
||||
// with a light surface, which painted pale blocks over this dark menu.
|
||||
className="cursor-pointer border-0 bg-transparent px-3 py-1 text-left font-mono text-xs text-white/70 shadow-none hover:bg-zinc-800 hover:text-cyan-300"
|
||||
>
|
||||
{item.label}
|
||||
</button>
|
||||
))}
|
||||
</div>,
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
|
||||
/** The rendered preview, memoised so an unchanged line never re-parses. */
|
||||
const MarkupPreview = memo(function MarkupPreview({ text }: { text: string }) {
|
||||
return (
|
||||
|
|
@ -77,16 +139,21 @@ export function MarkupEditor({
|
|||
return () => clearTimeout(timer);
|
||||
}, [value, previewText]);
|
||||
|
||||
// Close the context menu on any outside click or Escape.
|
||||
// Close the menu on an outside click, Escape, or anything that would move the
|
||||
// box out from under it. Scroll is captured so the dialog's own scroller counts.
|
||||
useEffect(() => {
|
||||
if (!menu) return;
|
||||
const close = () => setMenu(null);
|
||||
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setMenu(null);
|
||||
window.addEventListener("mousedown", close);
|
||||
window.addEventListener("keydown", onKey);
|
||||
window.addEventListener("resize", close);
|
||||
window.addEventListener("scroll", close, true);
|
||||
return () => {
|
||||
window.removeEventListener("mousedown", close);
|
||||
window.removeEventListener("keydown", onKey);
|
||||
window.removeEventListener("resize", close);
|
||||
window.removeEventListener("scroll", close, true);
|
||||
};
|
||||
}, [menu]);
|
||||
|
||||
|
|
@ -206,25 +273,7 @@ export function MarkupEditor({
|
|||
</div>
|
||||
)}
|
||||
{menu && (
|
||||
<div
|
||||
className="fixed z-[120] flex min-w-40 flex-col rounded border border-zinc-700 bg-zinc-900 py-1 shadow-xl"
|
||||
style={{ left: menu.x, top: menu.y }}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
>
|
||||
{menuItems.map((item) => (
|
||||
<button
|
||||
key={item.label}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
item.run();
|
||||
setMenu(null);
|
||||
}}
|
||||
className="px-3 py-1 text-left font-mono text-xs text-white/70 hover:bg-zinc-800 hover:text-cyan-300"
|
||||
>
|
||||
{item.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<MarkupMenu x={menu.x} y={menu.y} items={menuItems} onPick={() => setMenu(null)} />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
44
src/lib/npc-dialogue/markupMenu.ts
Normal file
44
src/lib/npc-dialogue/markupMenu.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
* Placement for the dialogue editor's markup menu.
|
||||
*
|
||||
* The menu is portalled to the document body (the edit dialog's transform would
|
||||
* otherwise become the containing block for `position: fixed`), and it is placed
|
||||
* at the pointer. That means a right-click near an edge would open it partly off
|
||||
* screen, where the author cannot reach the last items, so the point is pulled
|
||||
* back inside the viewport first.
|
||||
*
|
||||
* Pure and size-aware on purpose: the menu's own size is fixed by its contents,
|
||||
* so there is nothing to measure after mounting.
|
||||
*/
|
||||
|
||||
/** Roughly the menu's size: its widest label plus padding, and six rows. */
|
||||
export const MENU_WIDTH = 176;
|
||||
export const MENU_HEIGHT = 188;
|
||||
/** Smallest gap kept between the menu and an edge. */
|
||||
export const MENU_MARGIN = 8;
|
||||
|
||||
export interface MenuPoint {
|
||||
left: number;
|
||||
top: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The point to draw the menu at, given the pointer position and the viewport.
|
||||
* Never negative: on a viewport smaller than the menu, the margin wins over the
|
||||
* right and bottom clamps.
|
||||
*/
|
||||
export function clampMenuPosition(
|
||||
x: number,
|
||||
y: number,
|
||||
viewport: { width: number; height: number },
|
||||
): MenuPoint {
|
||||
const left = Math.max(
|
||||
MENU_MARGIN,
|
||||
Math.min(x, viewport.width - MENU_WIDTH - MENU_MARGIN),
|
||||
);
|
||||
const top = Math.max(
|
||||
MENU_MARGIN,
|
||||
Math.min(y, viewport.height - MENU_HEIGHT - MENU_MARGIN),
|
||||
);
|
||||
return { left, top };
|
||||
}
|
||||
39
tests/int/npc-dialogue-markup-menu.int.spec.ts
Normal file
39
tests/int/npc-dialogue-markup-menu.int.spec.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// @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);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue