@@ -1,11 +1,15 @@
1+import { useLayoutEffect, useRef, useState } from "react";
2+import { createPortal } from "react-dom";
13 import { Pencil, Trash2, X } from "lucide-react";
24 import { Button, IconButton } from "@/ui";
35 import type { Message, Reaction } from "../lib/types";
46 import { reactions } from "./MessageList";
57
6-// MessageActions is the contextual bar for the selected message. It replaces
7-// per-message hover toolbars: tap the ⋯ on any message and its actions appear
8-// here, reachable on touch screens and by keyboard alike.
8+// MessageActions is the contextual popover for the selected message: tap the ⋯
9+// on any message (or the row) and the actions float beside it, reachable on
10+// touch screens and by keyboard alike. It is portalled and positioned
11+// absolutely, so opening it never reflows the message list; its right edge is
12+// anchored to the message's ⋯ button.
913 export default function MessageActions({
1014 message,
1115 own,
@@ -21,38 +25,72 @@ export default function MessageActions({
2125 onDelete: () => void;
2226 onClose: () => void;
2327 }) {
24- return (
25- <div className="shrink-0 px-2 pb-1 md:px-3">
26- <div className="flex items-center gap-1 rounded-lg border border-line bg-raised p-1.5 shadow-lg">
27- {reactions.map(({ key, label, Icon }) => (
28- <IconButton
29- key={key}
30- label={`react ${label}`}
31- size="sm"
32- className="size-9"
33- onClick={() => onReact(key)}
34- >
35- <Icon />
36- </IconButton>
37- ))}
38- <span className="mx-1 h-6 w-px bg-line" />
39- <span className="min-w-0 flex-1 truncate px-1 text-xs text-faint">
40- {message.username}: {message.body}
41- </span>
42- {own && (
43- <>
44- <Button size="sm" variant="ghost" onClick={onEdit}>
45- <Pencil /> edit
46- </Button>
47- <Button size="sm" variant="danger" onClick={onDelete}>
48- <Trash2 /> delete
49- </Button>
50- </>
51- )}
52- <IconButton label="close message actions" size="sm" onClick={onClose}>
53- <X />
28+ const panel = useRef<HTMLDivElement>(null);
29+ const [pos, setPos] = useState<{ top: number; left: number } | null>(null);
30+
31+ // Track the ⋯ button through list scrolling, resizes and relayouts.
32+ useLayoutEffect(() => {
33+ const anchor = document.querySelector<HTMLElement>(`[data-actions-for="${message.id}"]`);
34+ if (!anchor) return;
35+ const place = () => {
36+ const r = anchor.getBoundingClientRect();
37+ const p = panel.current!.getBoundingClientRect();
38+ let top = r.bottom + 4;
39+ if (top + p.height > innerHeight - 8) top = Math.max(8, r.top - p.height - 4);
40+ const left = Math.max(8, Math.min(r.right - p.width, innerWidth - p.width - 8));
41+ setPos((prev) => (prev?.top === top && prev.left === left ? prev : { top, left }));
42+ };
43+ place();
44+ addEventListener("scroll", place, true);
45+ addEventListener("resize", place);
46+ const ro = new ResizeObserver(place);
47+ ro.observe(panel.current!);
48+ return () => {
49+ removeEventListener("scroll", place, true);
50+ removeEventListener("resize", place);
51+ ro.disconnect();
52+ };
53+ }, [message.id, own]);
54+
55+ return createPortal(
56+ <div
57+ ref={panel}
58+ role="toolbar"
59+ aria-label="message actions"
60+ style={{
61+ top: pos?.top ?? 0,
62+ left: pos?.left ?? 0,
63+ visibility: pos ? "visible" : "hidden",
64+ }}
65+ className="fixed z-40 flex w-max max-w-[calc(100vw-1rem)] flex-wrap items-center gap-1 rounded-lg border border-line bg-raised p-1.5 shadow-lg"
66+ >
67+ {reactions.map(({ key, label, Icon }) => (
68+ <IconButton
69+ key={key}
70+ label={`react ${label}`}
71+ size="sm"
72+ className="size-9"
73+ onClick={() => onReact(key)}
74+ >
75+ <Icon />
5476 </IconButton>
55- </div>
56- </div>
77+ ))}
78+ {own && (
79+ <>
80+ <span className="mx-1 h-6 w-px bg-line" />
81+ <Button size="sm" variant="ghost" onClick={onEdit}>
82+ <Pencil /> edit
83+ </Button>
84+ <Button size="sm" variant="danger" onClick={onDelete}>
85+ <Trash2 /> delete
86+ </Button>
87+ </>
88+ )}
89+ <span className="mx-1 h-6 w-px bg-line" />
90+ <IconButton label="close message actions" size="sm" onClick={onClose}>
91+ <X />
92+ </IconButton>
93+ </div>,
94+ document.body,
5795 );
5896 }