🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(web): stop dialogs hopping through document.body on open (#17264)
cursor[bot]fbf99aad292e
diff --git a/web/src/context/LayerContext/LayerContext.clienttest.tsx b/web/src/context/LayerContext/LayerContext.clienttest.tsxindex 0c4f0e072b..b69902791b 100644--- a/web/src/context/LayerContext/LayerContext.clienttest.tsx+++ b/web/src/context/LayerContext/LayerContext.clienttest.tsx@@ -1,4 +1,4 @@-import { render, screen, waitFor } from "@testing-library/react";+import { render, screen } from "@testing-library/react";import { Layer } from "@/src/components/design-system/Layer/Layer";import { LayerProvider, useLayerContainer } from "./LayerContext";@@ -9,7 +9,18 @@ function LayerContainerProbe() {}describe("LayerProvider", () => {- it("provides ordered body-level containers to portals", async () => {+ it("resolves layer containers on the first client render", () => {+ render(+ <LayerProvider>+ <LayerContainerProbe />+ </LayerProvider>,+ );++ expect(screen.getByText("modal")).toBeInTheDocument();+ expect(screen.queryByText("missing")).toBeNull();+ });++ it("provides ordered body-level containers to portals", () => {render(<LayerProvider><LayerContainerProbe />@@ -19,7 +30,7 @@ describe("LayerProvider", () => {</LayerProvider>,);- await waitFor(() => expect(screen.getByText("modal")).toBeInTheDocument());+ expect(screen.getByText("modal")).toBeInTheDocument();expect(screen.getByText("Portaled content").parentElement).toHaveAttribute("data-layer","toast",diff --git a/web/src/context/LayerContext/LayerContext.tsx b/web/src/context/LayerContext/LayerContext.tsxindex c155903fc4..7068b36068 100644--- a/web/src/context/LayerContext/LayerContext.tsx+++ b/web/src/context/LayerContext/LayerContext.tsx@@ -6,28 +6,44 @@ const LayerContext = React.createContext<Map<LayerName, HTMLElement> | null>(null,);-export function LayerProvider({ children }: { children: React.ReactNode }) {- const [layers, setLayers] = React.useState<Map<- LayerName,- HTMLElement- > | null>(null);-- React.useEffect(() => {- const root = document.createElement("div");+function ensureLayerContainers(): Map<LayerName, HTMLElement> {+ let root = document.querySelector<HTMLElement>("[data-overlay-root]");+ if (!root) {+ root = document.createElement("div");root.setAttribute("data-overlay-root", "");-- const layerContainers = new Map<LayerName, HTMLElement>();- for (const name of LAYER_ORDER) {- const layer = document.createElement("div");+ document.body.appendChild(root);+ }++ const layerContainers = new Map<LayerName, HTMLElement>();+ for (const name of LAYER_ORDER) {+ let layer = root.querySelector<HTMLElement>(+ `[data-layer="${CSS.escape(name)}"]`,+ );+ if (!layer) {+ layer = document.createElement("div");layer.setAttribute("data-layer", name);root.appendChild(layer);- layerContainers.set(name, layer);}+ layerContainers.set(name, layer);+ }+ return layerContainers;+}- document.body.appendChild(root);- setLayers(layerContainers);+export function LayerProvider({ children }: { children: React.ReactNode }) {+ // Resolve on the first client render. Creating the nodes in an effect leaves+ // `useLayerContainer` as null for one paint, so Radix/Vaul portals hop+ // through `document.body` and `hideOthers` logs `aria-hidden … not contained+ // inside body`.+ const [layers] = React.useState<Map<LayerName, HTMLElement> | null>(() => {+ if (typeof document === "undefined") {+ return null;+ }+ return ensureLayerContainers();+ });- return () => root.remove();+ React.useEffect(() => {+ const root = document.querySelector("[data-overlay-root]");+ return () => root?.remove();}, []);return (