π OpenHands: AI-Driven Development
fix(automations): flip the kebab menu above the trigger when it would clip (#16926)
Vasco Schiavof92ec6fea0f4
diff --git a/__tests__/components/automations/kebab-menu.test.tsx b/__tests__/components/automations/kebab-menu.test.tsxindex 686c6bfa1d..41cd86ee33 100644--- a/__tests__/components/automations/kebab-menu.test.tsx+++ b/__tests__/components/automations/kebab-menu.test.tsx@@ -2,6 +2,30 @@ import { render, screen, fireEvent } from "@testing-library/react";import { describe, it, expect, vi } from "vitest";import { KebabMenu } from "#/components/features/automations/kebab-menu";+const ITEMS = ["Run now", "View", "Export", "Edit", "Turn on", "Delete"].map(+ (label) => ({ label, icon: <span />, onClick: vi.fn() }),+);++/** Place the trigger at a fixed viewport position before the menu opens. */+function openMenuWithTriggerAt(top: number, bottom: number) {+ const trigger = screen.getByRole("button", {+ name: "AUTOMATIONS$ACTIONS_MENU",+ });+ vi.spyOn(trigger, "getBoundingClientRect").mockReturnValue({+ top,+ bottom,+ left: 900,+ right: 1000,+ width: 100,+ height: bottom - top,+ x: 900,+ y: top,+ toJSON: () => ({}),+ } as DOMRect);+ fireEvent.click(trigger);+ return document.querySelector<HTMLElement>('div[style*="position: fixed"]');+}+describe("KebabMenu", () => {it("opens the menu and invokes an item's onClick when selected", async () => {const onClick = vi.fn();@@ -17,4 +41,23 @@ describe("KebabMenu", () => {expect(onClick).toHaveBeenCalledTimes(1);});++ it("anchors below the trigger when the menu fits in the viewport", () => {+ render(<KebabMenu items={ITEMS} />);++ const portal = openMenuWithTriggerAt(68, 100);++ expect(portal?.style.top).toBe("104px");+ expect(portal?.style.bottom).toBe("");+ });++ it("flips above the trigger when the menu would clip at the viewport bottom", () => {+ render(<KebabMenu items={ITEMS} />);++ // window.innerHeight is 768 in jsdom; 700 + 4 + (6 * 36) overflows it.+ const portal = openMenuWithTriggerAt(668, 700);++ expect(portal?.style.bottom).toBe("104px");+ expect(portal?.style.top).toBe("");+ });});diff --git a/src/components/features/automations/kebab-menu.tsx b/src/components/features/automations/kebab-menu.tsxindex a9f015b756..fcec134dc7 100644--- a/src/components/features/automations/kebab-menu.tsx+++ b/src/components/features/automations/kebab-menu.tsx@@ -36,22 +36,35 @@ export function KebabMenu({ items, triggerClassName }: KebabMenuProps) {if (!rect) return;const gap = 4;+ // The real height is only measurable once the portal has painted; the+ // first pass falls back to an items-derived estimate (~36px per row).+ const measured = menuRef.current?.getBoundingClientRect().height ?? 0;+ const menuHeight = measured || items.length * 36;+ const overflowsBelow =+ rect.bottom + gap + menuHeight > window.innerHeight;+setPortalStyle({position: "fixed",zIndex: 9999,- top: rect.bottom + gap,+ // Flip above the trigger when the menu would clip at the viewport+ // bottom. Bottom-anchoring keeps it hugging the trigger at any height.+ ...(overflowsBelow+ ? { bottom: window.innerHeight - rect.top + gap }+ : { top: rect.bottom + gap }),right: window.innerWidth - rect.right,});};updatePosition();+ const frame = window.requestAnimationFrame(updatePosition);window.addEventListener("resize", updatePosition);window.addEventListener("scroll", updatePosition, true);return () => {+ window.cancelAnimationFrame(frame);window.removeEventListener("resize", updatePosition);window.removeEventListener("scroll", updatePosition, true);};- }, [open]);+ }, [open, items.length]);useEffect(() => {if (!open) return undefined;