π OpenHands: AI-Driven Development
fix: portal ModalBackdrop to document.body (#688)
Hiep Le6a96ca388e1c
diff --git a/__tests__/components/shared/modals/modal-backdrop.test.tsx b/__tests__/components/shared/modals/modal-backdrop.test.tsxnew file mode 100644index 0000000000..a3fd819589--- /dev/null+++ b/__tests__/components/shared/modals/modal-backdrop.test.tsx@@ -0,0 +1,69 @@+import { describe, expect, it, vi } from "vitest";+import { render, screen } from "@testing-library/react";+import userEvent from "@testing-library/user-event";++import { ModalBackdrop } from "#/components/shared/modals/modal-backdrop";++describe("ModalBackdrop", () => {+ it("portals out of a transformed ancestor so position: fixed resolves against the viewport", () => {+ // Arrange: a transformed ancestor would otherwise become the+ // containing block for `position: fixed` descendants and trap the+ // modal inside it (the OnboardingModal / InstallServerModal bug).+ render(+ <div data-testid="transformed-ancestor" style={{ transform: "translateX(0)" }}>+ <ModalBackdrop onClose={vi.fn()}>+ <p>modal content</p>+ </ModalBackdrop>+ </div>,+ );++ // Act+ const dialog = screen.getByRole("dialog");+ const transformedAncestor = screen.getByTestId("transformed-ancestor");++ // Assert+ expect(transformedAncestor.contains(dialog)).toBe(false);+ expect(document.body.contains(dialog)).toBe(true);+ });++ it("calls onClose when the user presses Escape", async () => {+ // Arrange+ const user = userEvent.setup();+ const onClose = vi.fn();+ render(+ <ModalBackdrop onClose={onClose}>+ <p>modal content</p>+ </ModalBackdrop>,+ );++ // Act+ await user.keyboard("{Escape}");++ // Assert+ expect(onClose).toHaveBeenCalledTimes(1);+ });++ it("calls onClose when the backdrop is clicked but not when the content is clicked", async () => {+ // Arrange+ const user = userEvent.setup();+ const onClose = vi.fn();+ render(+ <ModalBackdrop onClose={onClose}>+ <button type="button">inside content</button>+ </ModalBackdrop>,+ );++ // Act: click the content first β this must NOT close.+ await user.click(screen.getByRole("button", { name: "inside content" }));+ const callsAfterContentClick = onClose.mock.calls.length;++ // Act: now click the backdrop overlay (the dialog root's backdrop child).+ const dialog = screen.getByRole("dialog");+ const backdrop = dialog.firstElementChild as HTMLElement;+ await user.click(backdrop);++ // Assert+ expect(callsAfterContentClick).toBe(0);+ expect(onClose).toHaveBeenCalledTimes(1);+ });+});diff --git a/src/components/shared/modals/modal-backdrop.tsx b/src/components/shared/modals/modal-backdrop.tsxindex 7f5634c57b..fb53d657c0 100644--- a/src/components/shared/modals/modal-backdrop.tsx+++ b/src/components/shared/modals/modal-backdrop.tsx@@ -1,4 +1,5 @@import React from "react";+import { createPortal } from "react-dom";interface ModalBackdropProps {children: React.ReactNode;@@ -28,7 +29,14 @@ export function ModalBackdrop({if (e.target === e.currentTarget) onClose?.(); // only close if the click was on the backdrop};- return (+ if (typeof document === "undefined") return null;++ // Portal to document.body so the modal's `position: fixed` resolves+ // against the viewport. Otherwise a transformed ancestor (e.g. the+ // onboarding slide rail) would become the containing block and the+ // modal would render trapped inside it instead of overlapping its+ // parent modal.+ return createPortal(<divrole="dialog"aria-modal="true"@@ -40,6 +48,7 @@ export function ModalBackdrop({className="fixed inset-0 bg-black opacity-60"/><div className="relative">{children}</div>- </div>+ </div>,+ document.body,);}