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(frontend): land focus in target cell on right-click Go to Definition (#9795)

Kiran Gadhave69d37605d3d0
diff --git a/frontend/src/components/editor/cell/cell-context-menu.tsx b/frontend/src/components/editor/cell/cell-context-menu.tsx
index 0b9ae22232..cbbd04e1c1 100644
--- a/frontend/src/components/editor/cell/cell-context-menu.tsx
+++ b/frontend/src/components/editor/cell/cell-context-menu.tsx
@@ -61,6 +61,7 @@ export const CellActionsContextMenu = ({
});
const [imageRightClicked, setImageRightClicked] =
React.useState<HTMLImageElement>();
+ const suppressCloseAutoFocus = React.useRef(false);
const DEFAULT_CONTEXT_MENU_ITEMS: ActionButton[] = [
{
@@ -166,7 +167,10 @@ export const CellActionsContextMenu = ({
handle: () => {
const editorView = getEditorView();
if (editorView) {
- goToDefinitionAtCursorPosition(editorView);
+ // Only suppress focus restoration when we actually navigated;
+ // otherwise let Radix return focus to the trigger cell.
+ suppressCloseAutoFocus.current =
+ goToDefinitionAtCursorPosition(editorView);
}
},
},
@@ -194,7 +198,16 @@ export const CellActionsContextMenu = ({
>
{children}
</ContextMenuTrigger>
- <ContextMenuContent className="w-[300px]" scrollable={true}>
+ <ContextMenuContent
+ className="w-[300px]"
+ scrollable={true}
+ onCloseAutoFocus={(evt) => {
+ if (suppressCloseAutoFocus.current) {
+ evt.preventDefault();
+ suppressCloseAutoFocus.current = false;
+ }
+ }}
+ >
{visibleActions.map((group, i) => (
<Fragment key={i}>
{group.map((action) => {
diff --git a/frontend/src/core/codemirror/go-to-definition/commands.ts b/frontend/src/core/codemirror/go-to-definition/commands.ts
index 23c065755d..dcf99c606b 100644
--- a/frontend/src/core/codemirror/go-to-definition/commands.ts
+++ b/frontend/src/core/codemirror/go-to-definition/commands.ts
@@ -28,10 +28,11 @@ interface VariableDeclaration {
}
function goToPosition(view: EditorView, from: number): void {
- view.focus();
- // Wait for the next frame, otherwise codemirror will
- // add a cursor from a pointer click.
+ // Focus on the next frame: a synchronous focus is a no-op while a Radix
+ // context menu still owns focus, and codemirror would otherwise add a
+ // cursor from the pointer click.
requestAnimationFrame(() => {
+ view.focus();
view.dispatch({
selection: {
anchor: from,