Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(ui): add overlay layer system; fix clipped search-bar error tooltip (#14342)

Nikita Kabardin492e8f3f85ca
diff --git a/web/AGENTS.md b/web/AGENTS.md
index 0d1f3b0e36..cdfec370a6 100644
--- a/web/AGENTS.md
+++ b/web/AGENTS.md
@@ -114,6 +114,17 @@ changes.
`top-banner-offset`, `pt-banner-offset`, `h-screen-with-banner`, or
`min-h-screen-with-banner` instead of raw `top-0` so banners do not overlap
the UI.
+- **Z-index / layers β€” key idea: we are migrating from z-indexes to a layer
+ system** (start of a developing design system; extend it, don't work around
+ it). **To put something on top of something else, use a layer, not a
+ z-index.** The app renders inside `#__next`, isolated into one stacking
+ context (`globals.css`), so its z-indexes can't escape; overlays go in layers
+ that sit outside it and always win. Today there's one layer, `tooltip`:
+ containers declared in `_document.tsx`, ordered by `LAYER_ORDER` (later = on
+ top), no z-index; render via `<Layer name="…">` (`src/components/ui/layer.tsx`).
+ Add a layer only when an overlay must escape the app (else use a Radix
+ `*.Portal`) by adding a name to `LAYER_ORDER`. z-index stays local to a layer
+ or component (1–2 max), never to escape the app.
- Public API routes should use
`src/features/public-api/server/withMiddlewares.ts`, define strict request and
response types in `src/features/public-api/types/*`, add server tests, and
diff --git a/web/src/components/ui/layer.tsx b/web/src/components/ui/layer.tsx
new file mode 100644
index 0000000000..f44c56ceb2
--- /dev/null
+++ b/web/src/components/ui/layer.tsx
@@ -0,0 +1,62 @@
+import * as React from "react";
+import { createPortal } from "react-dom";
+
+/**
+ * App overlay layers β€” the seed of an app-wide layer system.
+ *
+ * The whole app renders inside `#__next`, isolated into its own stacking
+ * context (`isolation: isolate`, in globals.css). That caps every z-index in
+ * the app, so nothing inside can escape it. The overlay layer containers are
+ * declared once in `_document.tsx` as `<body>` siblings AFTER `#__next`, so
+ * they paint on top purely by DOM order β€” no z-index anywhere in this system.
+ *
+ * Layers stack by ORDER, not z-index: the containers are rendered in this order
+ * (later = on top) and each is its own isolated stacking context. z-index stays
+ * a LOCAL tool for ordering content WITHIN one layer (or within the app), never
+ * a global escalation. To add a layer, append its name here β€” `_document.tsx`
+ * maps this list to the containers β€” and render `<Layer name="…">`.
+ *
+ * TODO (follow-up): integrate Radix overlays into this system. Today they
+ * portal to `<body>` at `z-50` and so sit above these (z-index-less) layers by
+ * virtue of that number. The end state is to route them through layers too
+ * (e.g. a `popover`/`modal`/`toast` layer above `tooltip`), retiring the last
+ * app-wide z-index numbers so ordering everywhere is layer order.
+ */
+export const LAYER_ORDER = ["tooltip"] as const;
+export type LayerName = (typeof LAYER_ORDER)[number];
+
+function useLayerContainer(name: LayerName): HTMLElement | null {
+ // null on the server and the first client render (SSR parity), then the
+ // matching container declared in _document. It is static HTML, so it always
+ // exists post-hydration β€” no creation, no ordering, no teardown here.
+ const [container, setContainer] = React.useState<HTMLElement | null>(null);
+ React.useEffect(() => {
+ setContainer(
+ document.querySelector<HTMLElement>(
+ `[data-overlay-root] > [data-layer="${CSS.escape(name)}"]`,
+ ),
+ );
+ }, [name]);
+ return container;
+}
+
+/**
+ * Renders its children into the named overlay layer (see {@link LAYER_ORDER}),
+ * escaping ancestor `overflow` clipping and stacking contexts. Renders nothing
+ * until mounted, so it is SSR-safe. Children position themselves (`fixed` /
+ * `absolute`) and opt back into pointer events as needed.
+ *
+ * Prefer the Radix primitives' built-in `*.Portal` for Radix overlays; reach
+ * for `<Layer>` when portaling bespoke, imperatively-positioned content (e.g. a
+ * contenteditable's anchored tooltip) that isn't a Radix component.
+ */
+export function Layer({
+ name,
+ children,
+}: {
+ name: LayerName;
+ children: React.ReactNode;
+}) {
+ const container = useLayerContainer(name);
+ return container ? createPortal(children, container) : null;
+}
diff --git a/web/src/features/search-bar/README.md b/web/src/features/search-bar/README.md
index 7e8ee819a1..5ef2b8a352 100644
--- a/web/src/features/search-bar/README.md
+++ b/web/src/features/search-bar/README.md
@@ -335,10 +335,18 @@ unused planners).
operators, or leave it. Entangled with `tidyQueryText`/chip-removal (which
strips redundant parens and would bail on a now-"invalid" paren) and
removes documented top-level grouping β€” needs its own pass.
-- App-wide **layer system** (z-index): the bar's overlays use a hardcoded local
- ladder (X z-20 < error tooltip z-30 < popover z-50) because the app has no
- shared z scale (overlays are just `z-50` + Radix portals). A proper layering
- system is a separate, app-level ticket.
+- **Layer system**: the bar's in-flow overlays use a hardcoded local ladder (X
+ z-20 < autocomplete popover z-50, both drop into the table below the bar). The
+ **error tooltip is the exception**: when the popover is open it flips ABOVE
+ the bar, into the page header's band, where no in-flow z-index can win β€”
+ `#page > main` is `overflow:hidden` (clips anything above the bar) and the
+ header is inside the app's isolated stacking context. So that one tooltip
+ renders through the `"tooltip"` `<Layer>` (`components/ui/layer.tsx`), which
+ puts it in a `<body>`-level overlay layer that paints above the whole app by
+ DOM order β€” no z-index needed. `<Layer>` is the seed of an app-wide layer
+ system (one layer today, ordered by `LAYER_ORDER`). New overlays that must
+ escape clipping/stacking should reuse `<Layer>` rather than a one-off portal +
+ z-index.
- Optional: extract `SearchComposer`'s contenteditable selection/`beforeinput`
machinery into a `useContentEditableController` hook to fully separate the
imperative integration from the React component.
diff --git a/web/src/features/search-bar/components/SearchComposer.tsx b/web/src/features/search-bar/components/SearchComposer.tsx
index 9feec81e3d..d521e7de75 100644
--- a/web/src/features/search-bar/components/SearchComposer.tsx
+++ b/web/src/features/search-bar/components/SearchComposer.tsx
@@ -18,6 +18,7 @@ import * as React from "react";
import { useShallow } from "zustand/react/shallow";
import { AlertCircle, X } from "lucide-react";
+import { Layer } from "@/src/components/ui/layer";
import { cn } from "@/src/utils/tailwind";
import {
@@ -1221,10 +1222,15 @@ export function SearchComposer({
} | null>(null);
// Anchors for the per-token error tooltip β€” left edge of the token, plus its
// bottom (default placement, just under the block) and top (used to flip the
- // tooltip ABOVE the block while the suggestions popover is open below).
+ // tooltip ABOVE the block while the suggestions popover is open below). These
+ // are VIEWPORT coordinates (not container-relative): the tooltip portals to
+ // document.body and is positioned `fixed`, so it reads the token's own
+ // getClientRects() directly. The bar's band is sticky (it doesn't scroll out
+ // from under the token), so a fixed anchor stays put; it re-measures on draft
+ // change and the container ResizeObserver below, same as the X button.
const [errorPosition, setErrorPosition] = React.useState<{
left: number;
- top: number;
+ belowTop: number;
aboveTop: number;
} | null>(null);
const removeTargetIdActual = removeTarget?.id ?? null;
@@ -1254,9 +1260,9 @@ export function SearchComposer({
top: rect.top - containerRect.top - 8,
});
setErrorPosition({
- left: firstRect.left - containerRect.left,
- top: firstRect.bottom - containerRect.top + 6,
- aboveTop: firstRect.top - containerRect.top,
+ left: firstRect.left,
+ belowTop: firstRect.bottom + 6,
+ aboveTop: firstRect.top,
});
}, [removeTargetIdActual]);
@@ -1360,12 +1366,15 @@ export function SearchComposer({
scoreTypes={scoreTypes}
/>
</div>
- {/* Bar-local overlay stacking ladder (hardcoded for now β€” a proper
- app-wide layer system is a separate ticket): token text (base) <
- remove-X (z-20) < error tooltip (z-30) < autocomplete popover
- (z-50). The popover drops BELOW the bar; when it's open the error
- tooltip flips ABOVE the offending block (see errorAbove), so the two
- never overlap and the z order only needs to be self-consistent. */}
+ {/* Bar-local overlay stacking ladder: token text (base) < remove-X
+ (z-20) < autocomplete popover (z-50). Both the X and the popover drop
+ BELOW the bar, staying in-flow inside the table. The error tooltip is
+ the exception: when the popover is open it flips ABOVE the offending
+ block, into the page header's band β€” an ancestor it can't beat
+ in-flow (`#page > main` is overflow:hidden and the header is its own
+ z-50 stacking context). So it renders through the "tooltip" <Layer>,
+ which portals it to a body-level layer container (see the render
+ below + components/ui/layer.tsx). */}
{removeTarget !== null && removePosition !== null && (
<RemoveTokenButton
segment={removeTarget}
@@ -1373,29 +1382,6 @@ export function SearchComposer({
onRemove={removeSegment}
/>
)}
- {errorTarget !== null && errorPosition !== null && (
- <div
- role="tooltip"
- style={
- plan !== null
- ? {
- left: errorPosition.left,
- top: errorPosition.aboveTop,
- transform: "translateY(calc(-100% - 6px))",
- }
- : { left: errorPosition.left, top: errorPosition.top }
- }
- // pointer-events-none so moving onto the tooltip doesn't change the
- // hovered token (which would make it flicker away).
- className={cn(
- "pointer-events-none absolute z-30 max-w-[min(360px,calc(100vw-32px))]",
- "border-destructive/40 bg-popover text-destructive rounded-md border",
- "px-2 py-1 font-sans text-xs leading-snug shadow-md",
- )}
- >
- {errorTarget.message}
- </div>
- )}
</div>
{showGlobalDiagnostics && (
@@ -1427,6 +1413,36 @@ export function SearchComposer({
containerRef={containerRef}
/>
)}
+
+ {/* Per-token error tooltip. The "tooltip" <Layer> renders it on a
+ body-level container so it escapes the page header's overflow:hidden
+ clip and z-50 stacking context when it flips above the bar β€” an
+ in-flow z-index can't win against either. `fixed` + viewport anchors.
+ pointer-events-none so moving onto it doesn't change the hovered token
+ (which would flicker it away). */}
+ {errorTarget !== null && errorPosition !== null && (
+ <Layer name="tooltip">
+ <div
+ role="tooltip"
+ style={
+ plan !== null
+ ? {
+ left: errorPosition.left,
+ top: errorPosition.aboveTop,
+ transform: "translateY(calc(-100% - 6px))",
+ }
+ : { left: errorPosition.left, top: errorPosition.belowTop }
+ }
+ className={cn(
+ "pointer-events-none fixed max-w-[min(360px,calc(100vw-32px))]",
+ "border-destructive/40 bg-popover text-destructive rounded-md border",
+ "px-2 py-1 font-sans text-xs leading-snug shadow-md",
+ )}
+ >
+ {errorTarget.message}
+ </div>
+ </Layer>
+ )}
</div>
);
}
diff --git a/web/src/pages/_document.tsx b/web/src/pages/_document.tsx
new file mode 100644
index 0000000000..6390f29461
--- /dev/null
+++ b/web/src/pages/_document.tsx
@@ -0,0 +1,29 @@
+import { Html, Head, Main, NextScript } from "next/document";
+
+import { LAYER_ORDER } from "@/src/components/ui/layer";
+
+// The app renders inside #__next (<Main />), which is isolated into its own
+// stacking context (globals.css). The overlay layer containers are declared
+// here as <body> siblings AFTER #__next, so they paint above the whole app by
+// DOM order β€” no z-index needed. They are static HTML (present at SSR), ordered
+// by LAYER_ORDER (later = on top); <Layer> (components/ui/layer.tsx) finds its
+// container and portals into it. Styling lives in globals.css.
+export default function Document() {
+ return (
+ // lang is set explicitly (not left to the i18n config, which is being
+ // phased out in App Router) so screen readers always get the document
+ // language β€” WCAG 2.1 SC 3.1.1.
+ <Html lang="en">
+ <Head />
+ <body>
+ <Main />
+ <div data-overlay-root>
+ {LAYER_ORDER.map((name) => (
+ <div key={name} data-layer={name} />
+ ))}
+ </div>
+ <NextScript />
+ </body>
+ </Html>
+ );
+}
diff --git a/web/src/styles/globals.css b/web/src/styles/globals.css
index 356efbc8a1..6392fa64cb 100644
--- a/web/src/styles/globals.css
+++ b/web/src/styles/globals.css
@@ -396,6 +396,33 @@
div#__next > div {
height: 100%;
}
+
+ /* The whole app renders inside #__next. Isolating it into its own stacking
+ context caps every z-index in the app (page header, modals, popovers, …)
+ so none can escape it. The overlay layer containers (declared in
+ _document.tsx) are <body> siblings AFTER #__next and paint on top purely by
+ DOM order β€” see components/ui/layer.tsx. This is what lets layers carry no
+ z-index of their own; z-index stays a LOCAL tool for ordering within a
+ single layer. */
+ div#__next {
+ isolation: isolate;
+ }
+
+ /* Overlay layer roots (see _document.tsx + components/ui/layer.tsx). The
+ stack is click-through; each layer is its own isolated stacking context and
+ they order by DOM order (later = on top). Overlay content opts back into
+ pointer events on itself. */
+ [data-overlay-root] {
+ position: fixed;
+ inset: 0;
+ pointer-events: none;
+ }
+ [data-overlay-root] > [data-layer] {
+ position: absolute;
+ inset: 0;
+ isolation: isolate;
+ pointer-events: none;
+ }
}
@layer base {