Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: prevent LLM profile actions menu from being clipped by scroll container (#523)

Hiep Leb4d26635b0f9
diff --git a/__tests__/components/settings/llm-profiles/profile-row.test.tsx b/__tests__/components/settings/llm-profiles/profile-row.test.tsx
index 9b01e34f44..08d14311db 100644
--- a/__tests__/components/settings/llm-profiles/profile-row.test.tsx
+++ b/__tests__/components/settings/llm-profiles/profile-row.test.tsx
@@ -157,4 +157,15 @@ describe("ProfileRow", () => {
const menuTrigger = screen.getByTestId("profile-menu-trigger");
expect(menuTrigger).toHaveAttribute("aria-label", "Profile menu");
});
+
+ it("renders the opened menu outside the row container so ancestor overflow cannot clip it", async () => {
+ const user = userEvent.setup();
+
+ const { container } = render(<ProfileRow {...defaultProps} />);
+
+ await user.click(screen.getByTestId("profile-menu-trigger"));
+
+ const menu = screen.getByTestId("profile-actions-menu");
+ expect(container.contains(menu)).toBe(false);
+ });
});
diff --git a/src/components/features/conversation-panel/ellipsis-button.tsx b/src/components/features/conversation-panel/ellipsis-button.tsx
index 41b975c2e2..f8bf09b1b3 100644
--- a/src/components/features/conversation-panel/ellipsis-button.tsx
+++ b/src/components/features/conversation-panel/ellipsis-button.tsx
@@ -33,14 +33,16 @@ interface EllipsisButtonProps {
* variant with a custom hover background β€” do NOT replace that one
* with this component; it is intentionally different.
*/
-export function EllipsisButton({
- onClick,
- className,
- ariaLabel,
- testId = "ellipsis-button",
-}: EllipsisButtonProps) {
+export const EllipsisButton = React.forwardRef<
+ HTMLButtonElement,
+ EllipsisButtonProps
+>(function EllipsisButton(
+ { onClick, className, ariaLabel, testId = "ellipsis-button" },
+ ref,
+) {
return (
<button
+ ref={ref}
data-testid={testId}
type="button"
onClick={onClick}
@@ -55,4 +57,4 @@ export function EllipsisButton({
<ThreeDotsVerticalIcon className="w-4 h-4" />
</button>
);
-}
+});
diff --git a/src/components/features/settings/llm-profiles/profile-actions-menu.tsx b/src/components/features/settings/llm-profiles/profile-actions-menu.tsx
index 78d7f8c502..d94aef36dd 100644
--- a/src/components/features/settings/llm-profiles/profile-actions-menu.tsx
+++ b/src/components/features/settings/llm-profiles/profile-actions-menu.tsx
@@ -1,4 +1,11 @@
-import { useEffect, useRef, useCallback } from "react";
+import {
+ useEffect,
+ useLayoutEffect,
+ useRef,
+ useCallback,
+ useState,
+} from "react";
+import ReactDOM from "react-dom";
import { useTranslation } from "react-i18next";
import { cn } from "#/utils/utils";
import { I18nKey } from "#/i18n/declaration";
@@ -55,6 +62,13 @@ interface ProfileActionsMenuProps {
isActive: boolean;
isActivating: boolean;
onClose: () => void;
+ /**
+ * 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: auto/hidden` (e.g. the settings
+ * `<main>` scroll container).
+ */
+ anchorRef?: React.RefObject<HTMLElement | null>;
}
export function ProfileActionsMenu({
@@ -65,11 +79,40 @@ export function ProfileActionsMenu({
isActive,
isActivating,
onClose,
+ anchorRef,
}: ProfileActionsMenuProps) {
const { t } = useTranslation("openhands");
const menuRef = useRef<HTMLDivElement>(null);
const menuItemsRef = useRef<(HTMLButtonElement | null)[]>([]);
+ const anchorElement = anchorRef?.current ?? null;
+ const [portalStyle, setPortalStyle] = useState<React.CSSProperties>();
+
+ useLayoutEffect(() => {
+ if (!anchorElement) return undefined;
+
+ const updatePosition = () => {
+ const rect = anchorElement.getBoundingClientRect();
+ if (!rect) return;
+ // 4px gap matches the previous `mt-1` spacing.
+ const gap = 4;
+ setPortalStyle({
+ position: "fixed",
+ zIndex: 9999,
+ top: rect.bottom + gap,
+ right: window.innerWidth - rect.right,
+ });
+ };
+
+ updatePosition();
+ window.addEventListener("resize", updatePosition);
+ window.addEventListener("scroll", updatePosition, true);
+ return () => {
+ window.removeEventListener("resize", updatePosition);
+ window.removeEventListener("scroll", updatePosition, true);
+ };
+ }, [anchorElement]);
+
// Focus first item when menu opens
useEffect(() => {
menuItemsRef.current[0]?.focus();
@@ -124,11 +167,20 @@ export function ProfileActionsMenu({
);
const setActiveDisabled = isActive || isActivating;
+ const isPortaled = Boolean(anchorElement);
- return (
+ const menu = (
<div
ref={menuRef}
- className="absolute right-0 top-full mt-1 z-10 bg-base-secondary border border-[var(--oh-border)] rounded-md shadow-lg py-1 min-w-[160px]"
+ className={cn(
+ "absolute right-0 top-full mt-1 z-10 bg-base-secondary border border-[var(--oh-border)] rounded-md shadow-lg py-1 w-[160px]",
+ // When portaled the menu is positioned via the wrapper's inline
+ // `style` (fixed coords from the anchor rect), so we must neutralize
+ // the Tailwind absolute positioning that would otherwise pin it to
+ // its now-irrelevant offset parent.
+ isPortaled &&
+ "!static !top-auto !bottom-auto !left-auto !right-auto !mt-0",
+ )}
role="menu"
aria-orientation="vertical"
data-testid="profile-actions-menu"
@@ -169,4 +221,16 @@ export function ProfileActionsMenu({
/>
</div>
);
+
+ if (isPortaled) {
+ if (typeof document === "undefined" || !portalStyle) {
+ return null;
+ }
+ return ReactDOM.createPortal(
+ <div style={portalStyle}>{menu}</div>,
+ document.body,
+ );
+ }
+
+ return menu;
}
diff --git a/src/components/features/settings/llm-profiles/profile-row.tsx b/src/components/features/settings/llm-profiles/profile-row.tsx
index 3e87d558b1..2d049c8c30 100644
--- a/src/components/features/settings/llm-profiles/profile-row.tsx
+++ b/src/components/features/settings/llm-profiles/profile-row.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { ProfileActionsMenu } from "./profile-actions-menu";
import { ProfileInfo } from "#/api/profiles-service/profiles-service.api";
@@ -27,6 +27,7 @@ export function ProfileRow({
}: ProfileRowProps) {
const { t } = useTranslation("openhands");
const [menuOpen, setMenuOpen] = useState(false);
+ const triggerRef = useRef<HTMLButtonElement>(null);
return (
<div
@@ -59,12 +60,14 @@ export function ProfileRow({
</div>
<div className="relative shrink-0">
<EllipsisButton
+ ref={triggerRef}
onClick={() => setMenuOpen((open) => !open)}
ariaLabel={t(I18nKey.SETTINGS$PROFILE_MENU)}
testId="profile-menu-trigger"
/>
{menuOpen && (
<ProfileActionsMenu
+ anchorRef={triggerRef}
onEdit={() => onEdit(profile)}
onRename={() => onRename(profile)}
onSetActive={() => onActivate(profile.name)}