Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(chat): open the message actions menu above the composer dock

koeria9c9678710d4e
diff --git a/src/components/MessageActions.test.ts b/src/components/MessageActions.test.ts
index 660009ad80..01620b73ab 100644
--- a/src/components/MessageActions.test.ts
+++ b/src/components/MessageActions.test.ts
@@ -1,7 +1,7 @@
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
-import { MessageActions, type MessageMenuItem } from "./MessageActions";
+import { MessageActions, menuOpensAbove, type MessageMenuItem } from "./MessageActions";
type Props = Parameters<typeof MessageActions>[0];
@@ -60,3 +60,17 @@ describe("MessageActions", () => {
expect(render({ side: "bot" })).not.toContain("flex-row-reverse");
});
});
+
+describe("menuOpensAbove", () => {
+ it("opens downward when the menu fits above the floor", () => {
+ expect(menuOpensAbove(500, 472, 200, 792)).toBe(false);
+ });
+
+ it("flips up when the menu would cross the composer dock and there is room above", () => {
+ expect(menuOpensAbove(620, 592, 200, 632)).toBe(true);
+ });
+
+ it("stays downward when it would cross the floor but there is no room above", () => {
+ expect(menuOpensAbove(200, 172, 200, 250)).toBe(false);
+ });
+});
diff --git a/src/components/MessageActions.toggle.test.ts b/src/components/MessageActions.toggle.test.ts
index 03a1b892b7..b93e868d83 100644
--- a/src/components/MessageActions.toggle.test.ts
+++ b/src/components/MessageActions.toggle.test.ts
@@ -3,7 +3,7 @@ import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { setLocale } from "@/lib/i18n";
-import { MESSAGE_ROW, MessageActions } from "./MessageActions";
+import { MESSAGE_ROW, MessageActions, menuViewFloor } from "./MessageActions";
let host: HTMLDivElement;
let root: Root;
@@ -15,6 +15,9 @@ const shown = () => tray().dataset.shown === "true";
const more = () => tray().querySelector<HTMLButtonElement>('button[aria-haspopup="menu"]')!;
const items = () => [...host.querySelectorAll<HTMLButtonElement>("[role=menuitem]")];
+const box = (top: number, height: number): DOMRect =>
+ ({ top, bottom: top + height, height, left: 8, right: 48, width: 40, x: 8, y: top, toJSON() { return {}; } }) as DOMRect;
+
const pointer = (type: string, target: Element, pointerType: string) =>
act(async () => {
target.dispatchEvent(new PointerEvent(type, { bubbles: type !== "pointerenter" && type !== "pointerleave", pointerType }));
@@ -163,4 +166,71 @@ describe("message actions row", () => {
await pointer("pointerleave", row(), "mouse");
expect(shown()).toBe(true);
});
+
+ it("opens the menu above a composer dock the downward sheet would cover", async () => {
+ const frame = document.createElement("div");
+ frame.setAttribute("data-glass-frame", "");
+ const dock = document.createElement("div");
+ dock.className = "chat-column-dock";
+ host.replaceWith(frame);
+ frame.append(host, dock);
+ const restoreHeight = Object.getOwnPropertyDescriptor(window, "innerHeight");
+ Object.defineProperty(window, "innerHeight", { configurable: true, value: 800 });
+ const rects = vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) {
+ if (this === dock) return box(640, 160);
+ if (this.getAttribute("role") === "menu") return box(632, 240);
+ if (this.getAttribute("aria-haspopup") === "menu") return box(600, 28);
+ return box(0, 0);
+ });
+ try {
+ await render();
+ await act(async () => more().click());
+ const sheet = host.querySelector("[role=menu]")!;
+ expect(sheet.className).toContain("bottom-full");
+ expect(sheet.className).not.toContain("top-full");
+ } finally {
+ rects.mockRestore();
+ if (restoreHeight) Object.defineProperty(window, "innerHeight", restoreHeight);
+ else delete (window as { innerHeight?: number }).innerHeight;
+ frame.replaceWith(host);
+ }
+ });
+});
+
+describe("menuViewFloor", () => {
+ it("falls back to the window when there is no composer dock", () => {
+ const node = document.createElement("div");
+ expect(menuViewFloor(node, 800)).toBe(792);
+ expect(menuViewFloor(null, 800)).toBe(792);
+ });
+
+ it("uses the closest chat column dock, not a dock in another frame", () => {
+ const frame = document.createElement("div");
+ frame.setAttribute("data-glass-frame", "");
+ const from = frame.appendChild(document.createElement("div"));
+ const dock = frame.appendChild(document.createElement("div"));
+ dock.className = "chat-column-dock";
+ const other = document.createElement("div");
+ other.setAttribute("data-glass-frame", "");
+ const otherDock = other.appendChild(document.createElement("div"));
+ otherDock.className = "chat-column-dock";
+ document.body.append(other, frame);
+ vi.spyOn(dock, "getBoundingClientRect").mockReturnValue(box(640, 160));
+ vi.spyOn(otherDock, "getBoundingClientRect").mockReturnValue(box(200, 80));
+ expect(menuViewFloor(from, 800)).toBe(632);
+ frame.remove();
+ other.remove();
+ });
+
+ it("ignores a dock with no height", () => {
+ const frame = document.createElement("div");
+ frame.setAttribute("data-glass-frame", "");
+ const from = frame.appendChild(document.createElement("div"));
+ const dock = frame.appendChild(document.createElement("div"));
+ dock.className = "chat-column-dock";
+ document.body.append(frame);
+ vi.spyOn(dock, "getBoundingClientRect").mockReturnValue(box(0, 0));
+ expect(menuViewFloor(from, 800)).toBe(792);
+ frame.remove();
+ });
});
diff --git a/src/components/MessageActions.tsx b/src/components/MessageActions.tsx
index f98baeb7ad..40e6ba2038 100644
--- a/src/components/MessageActions.tsx
+++ b/src/components/MessageActions.tsx
@@ -34,6 +34,19 @@ export const MESSAGE_ROW = "data-message-row";
const INTERACTIVE = "a, button, input, textarea, select, summary, label, [role=button], [contenteditable=true]";
const OWN_MENU_MEDIA = "img, video, audio";
const LONG_PRESS_MS = 450;
+const MENU_VIEW_MARGIN = 8;
+
+/** Pixel floor a downward menu must stay above: the closest composer dock, else the window. */
+export function menuViewFloor(from: Element | null, viewportHeight?: number): number {
+ const view = viewportHeight ?? (typeof window !== "undefined" ? window.innerHeight : 0);
+ const box = from?.closest("[data-glass-frame]")?.querySelector(".chat-column-dock")?.getBoundingClientRect();
+ const floor = box && box.height > 0 ? Math.min(box.top, view) : view;
+ return floor - MENU_VIEW_MARGIN;
+}
+
+export function menuOpensAbove(downFrom: number, upFrom: number, height: number, floor: number, ceiling = MENU_VIEW_MARGIN): boolean {
+ return downFrom + height > floor && upFrom - height > ceiling;
+}
function focusVisible(target: EventTarget | null): boolean {
try {
@@ -183,14 +196,15 @@ export function MessageActions({
useLayoutEffect(() => {
if (!menuOpen) return;
const menuBox = menuRef.current?.getBoundingClientRect();
+ const floor = menuViewFloor(rootRef.current, window.innerHeight);
if (pointAt && menuBox) {
- setMenuAbove(pointAt.clientY + menuBox.height > window.innerHeight - 8 && pointAt.clientY - menuBox.height > 8);
+ setMenuAbove(menuOpensAbove(pointAt.clientY, pointAt.clientY, menuBox.height, floor));
setMenuFlipped(pointAt.clientX + menuBox.width > window.innerWidth - 8 && pointAt.clientX - menuBox.width >= 8);
menuRef.current?.querySelector<HTMLButtonElement>("[role=menuitem]:not(:disabled)")?.focus();
return;
}
const more = moreRef.current?.getBoundingClientRect();
- if (menuBox && more) setMenuAbove(more.bottom + 4 + menuBox.height > window.innerHeight - 8 && more.top - 4 - menuBox.height > 8);
+ if (menuBox && more) setMenuAbove(menuOpensAbove(more.bottom + 4, more.top - 4, menuBox.height, floor));
const rowBox = rootRef.current?.getBoundingClientRect();
if (menuBox && rowBox) {
const rtl = getComputedStyle(rootRef.current!).direction === "rtl";