Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(conversation): keep the header "β‹―" menu on screen and close it on Escape (#18028)

Engel Nyst00819fca50ac
diff --git a/__tests__/components/features/conversation/conversation-name-context-menu.test.tsx b/__tests__/components/features/conversation/conversation-name-context-menu.test.tsx
index 6687d11578..5833041119 100644
--- a/__tests__/components/features/conversation/conversation-name-context-menu.test.tsx
+++ b/__tests__/components/features/conversation/conversation-name-context-menu.test.tsx
@@ -1,13 +1,18 @@
import React from "react";
import { screen } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "test-utils";
import { ConversationNameContextMenu } from "#/components/features/conversation/conversation-name-context-menu";
+const { useBreakpointMock } = vi.hoisted(() => ({
+ useBreakpointMock: vi.fn(() => false),
+}));
+
vi.mock("#/hooks/use-breakpoint", () => ({
- useBreakpoint: () => false,
+ useBreakpoint: () => useBreakpointMock(),
}));
vi.mock("#/contexts/active-backend-context", () => ({
@@ -56,7 +61,14 @@ function createAnchor(rect: Partial<DOMRect> = {}) {
// output of the portal positioning math (anchor rect β†’ menu coordinates).
// They are functional logic checks, not visual styling assertions.
describe("ConversationNameContextMenu portal rendering", () => {
+ const originalInnerWidth = window.innerWidth;
+
afterEach(() => {
+ Object.defineProperty(window, "innerWidth", {
+ configurable: true,
+ value: originalInnerWidth,
+ });
+ useBreakpointMock.mockReturnValue(false);
document
.querySelectorAll('[data-test-anchor="true"]')
.forEach((anchor) => anchor.remove());
@@ -112,6 +124,53 @@ describe("ConversationNameContextMenu portal rendering", () => {
expect(wrapper.style.bottom).toBe("688px");
});
+ it("keeps the menu inside a phone-width viewport when the trigger sits near the right edge", () => {
+ useBreakpointMock.mockReturnValue(true);
+ Object.defineProperty(window, "innerWidth", {
+ configurable: true,
+ value: 390,
+ });
+ vi.spyOn(
+ HTMLUListElement.prototype,
+ "getBoundingClientRect",
+ ).mockReturnValue({ width: 237 } as DOMRect);
+ const anchor = createAnchor({ left: 193, right: 217 });
+
+ renderWithProviders(
+ <ConversationNameContextMenu
+ onClose={vi.fn()}
+ onRename={vi.fn()}
+ anchorRef={{ current: anchor }}
+ />,
+ );
+
+ const menu = screen.getByTestId("conversation-name-context-menu");
+ const wrapper = menu.parentElement as HTMLDivElement;
+ // 390 viewport - 8 gutter - 237 menu width.
+ expect(wrapper.style.left).toBe("145px");
+ // A sideways transform would push the clamped box back past the edge.
+ expect(menu.className).not.toMatch(/translate-x/);
+ });
+
+ it("closes on Escape and returns focus to the trigger", async () => {
+ const user = userEvent.setup();
+ const onClose = vi.fn();
+ const anchor = createAnchor();
+ renderWithProviders(
+ <ConversationNameContextMenu
+ onClose={onClose}
+ onRename={vi.fn()}
+ anchorRef={{ current: anchor }}
+ />,
+ );
+ screen.getByTestId("rename-button").focus();
+
+ await user.keyboard("{Escape}");
+
+ expect(onClose).toHaveBeenCalledTimes(1);
+ expect(anchor).toHaveFocus();
+ });
+
it("repositions when the anchored element changes between renders", () => {
const firstAnchor = createAnchor({ left: 12, top: 20, bottom: 48 });
const secondAnchor = createAnchor({ left: 64, top: 80, bottom: 112 });
diff --git a/__tests__/components/features/conversation/conversation-name.test.tsx b/__tests__/components/features/conversation/conversation-name.test.tsx
index bb2ca2fdaa..e3dd17c1b9 100644
--- a/__tests__/components/features/conversation/conversation-name.test.tsx
+++ b/__tests__/components/features/conversation/conversation-name.test.tsx
@@ -346,6 +346,20 @@ describe("ConversationName", () => {
).not.toBeInTheDocument();
});
+ it("closes the more-options menu on Escape and refocuses its trigger", async () => {
+ const user = userEvent.setup();
+ renderConversationNameWithRouter();
+ await user.click(screen.getByTestId("ellipsis-button"));
+ screen.getByTestId("rename-button").focus();
+
+ await user.keyboard("{Escape}");
+
+ expect(
+ screen.queryByTestId("conversation-name-context-menu"),
+ ).not.toBeInTheDocument();
+ expect(screen.getByTestId("ellipsis-button")).toHaveFocus();
+ });
+
it("should focus input when entering edit mode", async () => {
const user = userEvent.setup();
renderConversationNameWithRouter();
diff --git a/src/components/features/conversation/conversation-name-context-menu.tsx b/src/components/features/conversation/conversation-name-context-menu.tsx
index 36ebd664ad..4d75047e05 100644
--- a/src/components/features/conversation/conversation-name-context-menu.tsx
+++ b/src/components/features/conversation/conversation-name-context-menu.tsx
@@ -6,6 +6,7 @@ import { useActiveBackend } from "#/contexts/active-backend-context";
import { useActiveConversation } from "#/hooks/query/use-active-conversation";
import { useClickOutsideElement } from "#/hooks/use-click-outside-element";
import { useBreakpoint } from "#/hooks/use-breakpoint";
+import { clampLeftToViewport } from "#/hooks/use-popover-fixed-placement";
import { cn } from "#/utils/utils";
import { ContextMenu } from "#/ui/context-menu";
import { ContextMenuListItem } from "../context-menu/context-menu-list-item";
@@ -43,7 +44,8 @@ interface ConversationNameContextMenuProps {
* Element the menu should anchor against. When provided, the menu renders
* into a portal at the document body using fixed positioning so it cannot be
* clipped by ancestors with `overflow: hidden` (e.g. the chat panel that
- * sits next to the right-side tabs panel).
+ * sits next to the right-side tabs panel). Pass the trigger itself: Escape
+ * returns focus to it.
*/
anchorRef?: React.RefObject<HTMLElement | null>;
}
@@ -114,6 +116,9 @@ export function ConversationNameContextMenu({
const anchorElement = anchorRef?.current ?? null;
const [portalStyle, setPortalStyle] = React.useState<React.CSSProperties>();
+ // The menu's width is only measurable once it is in the portal, so placement
+ // runs again right after the first positioned render, still before paint.
+ const isMenuMounted = portalStyle !== undefined;
React.useLayoutEffect(() => {
if (!anchorElement) return undefined;
@@ -130,7 +135,10 @@ export function ConversationNameContextMenu({
} else {
style.top = rect.bottom + gap;
}
- style.left = rect.left;
+ // At phone width the trigger sits near the right edge: keep the whole
+ // menu, and every label, inside the viewport.
+ const menuWidth = ref.current?.getBoundingClientRect().width ?? 0;
+ style.left = clampLeftToViewport(rect.left, menuWidth);
setPortalStyle(style);
};
@@ -141,7 +149,18 @@ export function ConversationNameContextMenu({
window.removeEventListener("resize", updatePosition);
window.removeEventListener("scroll", updatePosition, true);
};
- }, [anchorElement, position]);
+ }, [anchorElement, position, isMenuMounted, ref]);
+
+ React.useEffect(() => {
+ const handleEscape = (event: KeyboardEvent) => {
+ if (event.key !== "Escape") return;
+ onClose();
+ anchorElement?.focus();
+ };
+
+ document.addEventListener("keydown", handleEscape);
+ return () => document.removeEventListener("keydown", handleEscape);
+ }, [onClose, anchorElement]);
const hasTools = Boolean(onShowAgentTools || onShowSkills || onShowHooks);
const hasInfo = Boolean(onDisplayCost);
const hasControl = Boolean(onStop || onDelete);
@@ -165,7 +184,9 @@ export function ConversationNameContextMenu({
position={position}
alignment="left"
className={cn(
- isMobile ? "right-0 translate-x-[34%] left-auto" : "",
+ // The mobile offset only applies to the inline, absolutely positioned
+ // menu; the portaled menu gets its coordinates from `portalStyle`.
+ !isPortaled && isMobile ? "right-0 translate-x-[34%] left-auto" : "",
portalClassName,
)}
>
diff --git a/src/components/features/conversation/conversation-name.tsx b/src/components/features/conversation/conversation-name.tsx
index 8d191226e5..3d59abac8d 100644
--- a/src/components/features/conversation/conversation-name.tsx
+++ b/src/components/features/conversation/conversation-name.tsx
@@ -26,7 +26,7 @@ export function ConversationName() {
const [transcriptExportModalVisible, setTranscriptExportModalVisible] =
React.useState(false);
const inputRef = React.useRef<HTMLInputElement>(null);
- const ellipsisAnchorRef = React.useRef<HTMLDivElement>(null);
+ const ellipsisAnchorRef = React.useRef<HTMLButtonElement>(null);
// Use the custom hook for context menu handlers
const {
@@ -169,11 +169,9 @@ export function ConversationName() {
)}
{titleMode !== "edit" && (
- <div
- ref={ellipsisAnchorRef}
- className="relative flex items-center shrink-0"
- >
+ <div className="relative flex items-center shrink-0">
<EllipsisButton
+ ref={ellipsisAnchorRef}
onClick={handleEllipsisClick}
ariaLabel={t(I18nKey.COMMON$MORE_OPTIONS)}
/>