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 code lens tooltip jitter (#10591)

Akshay Agrawal001c89260bd9
diff --git a/frontend/src/core/codemirror/code-lens/__tests__/popover.test.ts b/frontend/src/core/codemirror/code-lens/__tests__/popover.test.ts
index f7f67a369d..d1a0860b07 100644
--- a/frontend/src/core/codemirror/code-lens/__tests__/popover.test.ts
+++ b/frontend/src/core/codemirror/code-lens/__tests__/popover.test.ts
@@ -56,6 +56,19 @@ describe("CachePopover getCacheInfo throttling", () => {
return () => act(() => dispose());
}
+ it("renders content synchronously for CodeMirror's initial measurement", async () => {
+ const dom = document.createElement("div");
+
+ // Keep the mount outside `act()`. It would flush an asynchronous render
+ // and make this assertion pass without `flushSync`.
+ const dispose = mountLensPopover(dom, CACHE_SPEC);
+
+ expect(dom.textContent).toContain("my_cache");
+ await act(async () => {
+ dispose();
+ });
+ });
+
it("does not refetch on every hover, only after the throttle interval elapses", () => {
const disposeFirst = mount();
expect(getCacheInfoMock).toHaveBeenCalledTimes(1);
diff --git a/frontend/src/core/codemirror/code-lens/popover.tsx b/frontend/src/core/codemirror/code-lens/popover.tsx
index e0b14a618c..9e0935ea2c 100644
--- a/frontend/src/core/codemirror/code-lens/popover.tsx
+++ b/frontend/src/core/codemirror/code-lens/popover.tsx
@@ -4,6 +4,7 @@ import { Provider, useAtomValue } from "jotai";
import { DatabaseZapIcon } from "lucide-react";
import type React from "react";
import { useEffect } from "react";
+import { flushSync } from "react-dom";
import { createRoot } from "react-dom/client";
import { useLocale } from "react-aria";
import { ProtocolIcon } from "@/components/storage/components";
@@ -39,11 +40,16 @@ export function mountLensPopover(
spec: CodeLensSpec,
): () => void {
const root = createRoot(dom);
- root.render(
- <Provider store={store}>
- <LensPopover spec={spec} />
- </Provider>,
- );
+ // CodeMirror measures and positions the tooltip as soon as this function
+ // returns. Render now so it measures the popover content, not an empty
+ // container that later grows over the hovered icon.
+ flushSync(() => {
+ root.render(
+ <Provider store={store}>
+ <LensPopover spec={spec} />
+ </Provider>,
+ );
+ });
// Defer so tearing the tooltip down from inside a React event (e.g. the
// icon's click handler) doesn't unmount mid-render
return () => queueMicrotask(() => root.unmount());