Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(nav): clamp dropdown to viewport and portal into fullscreen root (#10297)

Myles Scolnick9c7568d52f64
diff --git a/frontend/e2e-tests/nav-menu.spec.ts b/frontend/e2e-tests/nav-menu.spec.ts
new file mode 100644
index 0000000000..ab8b20105d
--- /dev/null
+++ b/frontend/e2e-tests/nav-menu.spec.ts
@@ -0,0 +1,23 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { expect, test } from "@playwright/test";
+import { getAppUrl } from "../playwright.config";
+
+const appUrl = getAppUrl("nav_menu.py");
+
+test("nav menu dropdown is not clipped by the cell output area", async ({
+ page,
+}) => {
+ await page.goto(appUrl);
+
+ const trigger = page.getByRole("button", { name: "Links" }).first();
+ await trigger.scrollIntoViewIfNeeded();
+ await trigger.hover();
+
+ const link = page.getByRole("link", { name: "GitHub" }).first();
+ await expect(link).toBeVisible();
+ // The dropdown is portaled out of the cell's overflow container; a trial
+ // click performs Playwright's hit-target check, which fails if the link is
+ // clipped or covered by the cell output area, without navigating away.
+ await link.click({ trial: true });
+});
diff --git a/frontend/e2e-tests/py/nav_menu.py b/frontend/e2e-tests/py/nav_menu.py
new file mode 100644
index 0000000000..8c3ef672c0
--- /dev/null
+++ b/frontend/e2e-tests/py/nav_menu.py
@@ -0,0 +1,37 @@
+import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+
+ return (mo,)
+
+
+def _(mo):
+ # A horizontal nav menu with a nested group. The group's dropdown is
+ # portaled out of the cell's overflow container; this exercises the
+ # clipping/positioning fix in NavigationMenuPlugin.
+ mo.nav_menu(
+ {
+ "#section1": "Section 1",
+ "#section2": "Section 2",
+ "Links": {
+ "https://marimo.io": "marimo.io",
+ "https://github.com/marimo-team/marimo": {
+ "label": "GitHub",
+ "description": "GitHub repository",
+ },
+ },
+ },
+ orientation="horizontal",
+ )
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/frontend/playwright.config.ts b/frontend/playwright.config.ts
index a4670f3b0d..63f549e2a5 100644
--- a/frontend/playwright.config.ts
+++ b/frontend/playwright.config.ts
@@ -48,6 +48,7 @@ const appToOptions = {
"shutdown.py": { command: "edit", port: port() },
// Run
"components.py": { port: port(), command: "run" },
+ "nav_menu.py": { port: port(), command: "run" },
"layout_grid.py//run": { port: port(), command: "run" },
"layout_grid_max_width.py//run": { port: port(), command: "run" },
"layout_grid_with_sidebar.py//run": { port: port(), command: "run" },
diff --git a/frontend/src/plugins/layout/NavigationMenuPlugin.tsx b/frontend/src/plugins/layout/NavigationMenuPlugin.tsx
index 21db019869..696dcec341 100644
--- a/frontend/src/plugins/layout/NavigationMenuPlugin.tsx
+++ b/frontend/src/plugins/layout/NavigationMenuPlugin.tsx
@@ -3,6 +3,7 @@ import type { JSX, PropsWithChildren } from "react";
import React from "react";
import { createPortal } from "react-dom";
import { z } from "zod";
+import { withFullScreenAsRoot } from "@/components/ui/fullscreen";
import {
NavigationMenu,
NavigationMenuContent,
@@ -68,11 +69,13 @@ const HorizontalMenuGroup = ({
const triggerRef = React.useRef<React.ComponentRef<
typeof NavigationMenuTrigger
> | null>(null);
+ const [open, setOpen] = React.useState(false);
return (
<NavigationMenuPrimitive.Root
className="relative z-10 max-w-max flex-1 items-center justify-center"
orientation="horizontal"
+ onValueChange={(value) => setOpen(value !== "")}
>
<NavigationMenuList>
<NavigationMenuItem>
@@ -96,20 +99,37 @@ const HorizontalMenuGroup = ({
</NavigationMenuContent>
</NavigationMenuItem>
</NavigationMenuList>
- <NavigationMenuViewportPortal anchorRef={triggerRef}>
+ <NavigationMenuViewportPortal anchorRef={triggerRef} open={open}>
<NavigationMenuViewport />
</NavigationMenuViewportPortal>
</NavigationMenuPrimitive.Root>
);
};
+// Portal into the fullscreen element (or VSCode output container) when
+// present; `position: fixed` elements portaled to document.body render
+// behind a fullscreened cell.
+const NavigationMenuPortal = withFullScreenAsRoot(
+ ({
+ container,
+ children,
+ }: PropsWithChildren<{
+ container?: Element | DocumentFragment | null;
+ }>) => createPortal(children, container ?? document.body),
+);
+
+const VIEWPORT_MARGIN = 8;
+
const NavigationMenuViewportPortal = ({
anchorRef,
+ open,
children,
}: PropsWithChildren<{
anchorRef: React.RefObject<HTMLElement | null>;
+ open: boolean;
}>): React.ReactElement | null => {
const [position, setPosition] = React.useState<PortalPosition | null>(null);
+ const contentRef = React.useRef<HTMLDivElement | null>(null);
const updatePosition = React.useCallback(() => {
if (!anchorRef.current) {
@@ -117,15 +137,26 @@ const NavigationMenuViewportPortal = ({
}
const rect = anchorRef.current.getBoundingClientRect();
+ // Clamp so a wide dropdown doesn't run off the right edge of the viewport.
+ const contentWidth =
+ contentRef.current?.firstElementChild?.scrollWidth ?? 0;
+ const maxLeft = window.innerWidth - contentWidth - VIEWPORT_MARGIN;
setPosition({
- left: rect.left,
+ left: Math.max(VIEWPORT_MARGIN, Math.min(rect.left, maxLeft)),
top: rect.bottom,
});
}, [anchorRef]);
+ // Recompute whenever the menu opens: the trigger may have moved since mount
+ // due to layout shifts that emit no scroll/resize events. The rAF re-measures
+ // after the dropdown content has rendered, so clamping sees its real width.
React.useLayoutEffect(() => {
updatePosition();
- }, [updatePosition]);
+ if (open) {
+ const raf = requestAnimationFrame(updatePosition);
+ return () => cancelAnimationFrame(raf);
+ }
+ }, [open, updatePosition]);
useResizeObserver({
ref: anchorRef,
@@ -150,14 +181,16 @@ const NavigationMenuViewportPortal = ({
return null;
}
- return createPortal(
- <div
- className="fixed z-50"
- style={{ left: position.left, top: position.top }}
- >
- {children}
- </div>,
- document.body,
+ return (
+ <NavigationMenuPortal>
+ <div
+ ref={contentRef}
+ className="fixed z-50"
+ style={{ left: position.left, top: position.top }}
+ >
+ {children}
+ </div>
+ </NavigationMenuPortal>
);
};
@@ -301,23 +334,25 @@ const ListItem = React.forwardRef<
>(({ className, label, children, ...props }, ref) => {
return (
<li>
- <a
- ref={ref}
- className={cn(
- "block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-hidden transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground",
- className,
- )}
- {...props}
- >
- <div className="text-base font-medium leading-none">
- {renderHTML({ html: label })}
- </div>
- {children && (
- <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
- {children}
- </p>
- )}
- </a>
+ <NavigationMenuLink asChild={true}>
+ <a
+ ref={ref}
+ className={cn(
+ "block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-hidden transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground",
+ className,
+ )}
+ {...props}
+ >
+ <div className="text-base font-medium leading-none">
+ {renderHTML({ html: label })}
+ </div>
+ {children && (
+ <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
+ {children}
+ </p>
+ )}
+ </a>
+ </NavigationMenuLink>
</li>
);
});