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.

don't steal focus when embedded in an iframe (#7798)

Pepijn de Vos8b28c2c3deba
diff --git a/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts b/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts
index 1fc3127844..a72eac31bf 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts
+++ b/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts
@@ -12,6 +12,8 @@ describe("useFocusFirstEditor", () => {
cb(0);
return 0;
});
+ // Mock document.hasFocus() to return true so focus logic runs
+ vi.spyOn(document, "hasFocus").mockReturnValue(true);
});
afterEach(() => {
@@ -120,4 +122,29 @@ describe("useFocusFirstEditor", () => {
writable: true,
});
});
+
+ it("should not focus when document does not have focus", () => {
+ // Mock document.hasFocus() to return false (e.g., when embedded in iframe)
+ vi.spyOn(document, "hasFocus").mockReturnValue(false);
+
+ const mockEditor = { focus: vi.fn() };
+ vi.spyOn(cellsModule, "getNotebook").mockReturnValue({
+ cellIds: { iterateTopLevelIds: ["cell-1"] },
+ cellData: {
+ "cell-1": { config: { hide_code: false } },
+ },
+ cellHandles: {
+ "cell-1": { current: { editorView: mockEditor } },
+ },
+ } as unknown as cellsModule.NotebookState);
+
+ renderHook(() => useFocusFirstEditor());
+
+ act(() => {
+ vi.advanceTimersByTime(100);
+ });
+
+ // Focus should NOT be called when document doesn't have focus
+ expect(mockEditor.focus).not.toHaveBeenCalled();
+ });
});
diff --git a/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts b/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts
index 11484ad0a1..4c8fe1332d 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts
+++ b/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts
@@ -19,6 +19,12 @@ export function useFocusFirstEditor() {
const timeout = setTimeout(() => {
// Let the DOM render
requestAnimationFrame(() => {
+ // Skip auto-focus if the document doesn't have focus to avoid
+ // stealing focus from outside (e.g., when embedded in an iframe)
+ if (!document.hasFocus()) {
+ return;
+ }
+
// Check if the URL contains a scrollTo parameter
const hash = window.location.hash;
const cellName = extractCellNameFromHash(hash);
diff --git a/frontend/src/core/cells/scrollCellIntoView.ts b/frontend/src/core/cells/scrollCellIntoView.ts
index d3611cea2b..a0b109cdaf 100644
--- a/frontend/src/core/cells/scrollCellIntoView.ts
+++ b/frontend/src/core/cells/scrollCellIntoView.ts
@@ -53,8 +53,9 @@ export function focusAndScrollCellIntoView({
Logger.warn("scrollCellIntoView: editor not found", cellId);
return;
}
- // If already focused, do nothing.
- if (editor.hasFocus) {
+ // Skip auto-focus if already focused, or if the document doesn't have
+ // focus to avoid stealing focus from outside (e.g., when embedded in an iframe)
+ if (editor.hasFocus || !document.hasFocus()) {
return;
}