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.

improvement: Cmd+p to open cell actions , restore focus back to cell (#1024)

Myles Scolnick44ab2fe3b603
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index eaee407643..bd997fc4dc 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -31,7 +31,10 @@ import { Logger } from "../../utils/Logger";
import { CellDragHandle, SortableCell } from "./SortableCell";
import { HTMLCellId } from "../../core/cells/ids";
import { Theme } from "../../theme/useTheme";
-import { CellActionsDropdown } from "./cell/cell-actions";
+import {
+ CellActionsDropdown,
+ CellActionsDropdownHandle,
+} from "./cell/cell-actions";
import { CellActionsContextMenu } from "./cell/cell-context-menu";
import { AppMode } from "@/core/mode";
import useEvent from "react-use-event-hook";
@@ -147,6 +150,7 @@ const CellComponent = (
Logger.debug("Rendering Cell", cellId);
const cellRef = useRef<HTMLDivElement>(null);
+ const cellActionDropdownRef = useRef<CellActionsDropdownHandle>(null);
const editorView = useRef<EditorView | null>(null);
const setAiCompletionCell = useSetAtom(aiCompletionCellAtom);
@@ -322,6 +326,9 @@ const CellComponent = (
derefNotNull(editorView).focus();
}
},
+ "cell.cellActions": () => {
+ cellActionDropdownRef.current?.toggle();
+ },
});
useKeydownOnElement(editing ? cellRef.current : null, {
@@ -449,6 +456,7 @@ const CellComponent = (
needsRun={needsRun}
/>
<CellActionsDropdown
+ ref={cellActionDropdownRef}
cellId={cellId}
status={status}
editorView={editorView.current}
diff --git a/frontend/src/components/editor/cell/cell-actions.tsx b/frontend/src/components/editor/cell/cell-actions.tsx
index 549756afc2..2fe01c5b3c 100644
--- a/frontend/src/components/editor/cell/cell-actions.tsx
+++ b/frontend/src/components/editor/cell/cell-actions.tsx
@@ -1,5 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { Fragment, useState } from "react";
+import { Fragment, useImperativeHandle, useState } from "react";
import {
Command,
@@ -25,15 +25,30 @@ import {
CellActionButtonProps,
useCellActionButtons,
} from "../actions/useCellActionButton";
+import { useRestoreFocus } from "@/components/ui/use-restore-focus";
interface Props extends CellActionButtonProps {
children: React.ReactNode;
}
-export const CellActionsDropdown = ({ children, ...props }: Props) => {
+export interface CellActionsDropdownHandle {
+ toggle: () => void;
+}
+
+const CellActionsDropdownInternal = (
+ { children, ...props }: Props,
+ ref: React.Ref<CellActionsDropdownHandle>,
+) => {
const [open, setOpen] = useState(false);
const actions = useCellActionButtons({ cell: props });
+ // store the last focused element so we can restore it when the popover closes
+ const restoreFocus = useRestoreFocus();
+
+ useImperativeHandle(ref, () => ({
+ toggle: () => setOpen((prev) => !prev),
+ }));
+
return (
<Popover open={open} onOpenChange={setOpen}>
<TooltipRoot delayDuration={400} disableHoverableContent={true}>
@@ -58,17 +73,9 @@ export const CellActionsDropdown = ({ children, ...props }: Props) => {
<PopoverTrigger className="flex">{children}</PopoverTrigger>
</TooltipTrigger>
</TooltipRoot>
- <PopoverContent
- className="w-[300px] p-0 pt-1"
- onOpenAutoFocus={(e) => e.preventDefault()}
- onCloseAutoFocus={(e) => e.preventDefault()}
- >
+ <PopoverContent className="w-[300px] p-0 pt-1" {...restoreFocus}>
<Command>
- <CommandInput
- placeholder="Search actions..."
- className="h-6 m-1"
- autoFocus={true}
- />
+ <CommandInput placeholder="Search actions..." className="h-6 m-1" />
<CommandEmpty>No results</CommandEmpty>
{actions.map((group, i) => (
<Fragment key={i}>
@@ -109,3 +116,7 @@ export const CellActionsDropdown = ({ children, ...props }: Props) => {
</Popover>
);
};
+
+export const CellActionsDropdown = React.memo(
+ React.forwardRef(CellActionsDropdownInternal),
+);
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index 0fa52c54cd..a1eb1c222b 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -173,6 +173,11 @@ const DEFAULT_HOT_KEY = {
group: "Editing",
key: "Mod-Shift-e",
},
+ "cell.cellActions": {
+ name: "Open cell actions",
+ group: "Editing",
+ key: "Mod-p",
+ },
// Global Actions
"global.hideCode": {