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: right-click add button to add Markdown or SQL (#1722)

Myles Scolnick4fe9cc4d40e4
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index c9d0687de2..5043aa6e7e 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -224,11 +224,13 @@ const CellComponent = (
});
const createBelow = useCallback(
- () => createNewCell({ cellId, before: false }),
+ (opts: { code?: string } = {}) =>
+ createNewCell({ cellId, before: false, ...opts }),
[cellId, createNewCell],
);
const createAbove = useCallback(
- () => createNewCell({ cellId, before: true }),
+ (opts: { code?: string } = {}) =>
+ createNewCell({ cellId, before: true, ...opts }),
[cellId, createNewCell],
);
@@ -423,7 +425,7 @@ const CellComponent = (
>
{userConfig.display.cell_output === "above" && outputArea}
<div className="tray">
- <div className="absolute flex flex-col gap-[2px] justify-center h-full left-[-34px] z-2 hover-action">
+ <div className="absolute flex flex-col gap-[2px] justify-center h-full left-[-34px] z-2">
<CreateCellButton
tooltipContent={renderShortcut("cell.createAbove")}
appClosed={appClosed}
diff --git a/frontend/src/components/editor/cell/CreateCellButton.tsx b/frontend/src/components/editor/cell/CreateCellButton.tsx
index 23a4fd9035..39be193677 100644
--- a/frontend/src/components/editor/cell/CreateCellButton.tsx
+++ b/frontend/src/components/editor/cell/CreateCellButton.tsx
@@ -1,7 +1,17 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { PlusIcon } from "lucide-react";
+import { DatabaseIcon, PlusIcon } from "lucide-react";
import { Button } from "@/components/editor/inputs/Inputs";
import { Tooltip } from "../../ui/tooltip";
+import {
+ ContextMenu,
+ ContextMenuTrigger,
+ ContextMenuContent,
+ ContextMenuItem,
+} from "@/components/ui/context-menu";
+import { MarkdownLanguageAdapter } from "@/core/codemirror/language/markdown";
+import { MarkdownIcon } from "./code/icons";
+import { SQLLanguageAdapter } from "@/core/codemirror/language/sql";
+import { cn } from "@/utils/cn";
export const CreateCellButton = ({
appClosed,
@@ -10,20 +20,68 @@ export const CreateCellButton = ({
}: {
appClosed: boolean;
tooltipContent: React.ReactNode;
- onClick?: () => void;
+ onClick: ((opts: { code: string }) => void) | undefined;
}) => {
return (
- <Tooltip content={tooltipContent} usePortal={false}>
- <Button
- onClick={onClick}
- className={`ShoulderButton ${appClosed ? " inactive-button" : ""}`}
- shape="circle"
- size="small"
- color="hint-green"
- data-testid="create-cell-button"
- >
- <PlusIcon strokeWidth={1.8} />
- </Button>
- </Tooltip>
+ <CreateCellButtonContextMenu onClick={onClick}>
+ <Tooltip content={tooltipContent} usePortal={false}>
+ <Button
+ onClick={() => onClick?.({ code: "" })}
+ className={cn(
+ "shoulder-button hover-action",
+ appClosed && " inactive-button",
+ )}
+ shape="circle"
+ size="small"
+ color="hint-green"
+ data-testid="create-cell-button"
+ >
+ <PlusIcon strokeWidth={1.8} />
+ </Button>
+ </Tooltip>
+ </CreateCellButtonContextMenu>
+ );
+};
+
+const CreateCellButtonContextMenu = (props: {
+ onClick: ((opts: { code: string }) => void) | undefined;
+ children: React.ReactNode;
+}) => {
+ const { children, onClick } = props;
+
+ if (!onClick) {
+ return children;
+ }
+
+ return (
+ <ContextMenu>
+ <ContextMenuTrigger>{children}</ContextMenuTrigger>
+ <ContextMenuContent>
+ <ContextMenuItem
+ key="markdown"
+ onSelect={(evt) => {
+ evt.stopPropagation();
+ onClick({ code: new MarkdownLanguageAdapter().defaultCode });
+ }}
+ >
+ <div className="mr-3">
+ <MarkdownIcon />
+ </div>
+ Markdown cell
+ </ContextMenuItem>
+ <ContextMenuItem
+ key="sql"
+ onSelect={(evt) => {
+ evt.stopPropagation();
+ onClick({ code: new SQLLanguageAdapter().defaultCode });
+ }}
+ >
+ <div className="mr-3">
+ <DatabaseIcon size={13} strokeWidth={1.5} />
+ </div>
+ SQL cell
+ </ContextMenuItem>
+ </ContextMenuContent>
+ </ContextMenu>
);
};
diff --git a/frontend/src/css/app/Cell.css b/frontend/src/css/app/Cell.css
index db17caccc1..41553986c2 100644
--- a/frontend/src/css/app/Cell.css
+++ b/frontend/src/css/app/Cell.css
@@ -235,7 +235,7 @@
.cm .cm-gutters,
.Cell .cm-editor.cm-focused .cm-activeLineGutter,
.Cell .cm-editor.cm-focused .cm-activeLine,
- .Cell .ShoulderButton {
+ .Cell .shoulder-button {
background-color: transparent;
}
diff --git a/frontend/src/css/common.css b/frontend/src/css/common.css
index 0da4a45976..d9aa976a00 100644
--- a/frontend/src/css/common.css
+++ b/frontend/src/css/common.css
@@ -7,16 +7,20 @@
}
}
-.hover-action:hover {
+.hover-action:hover,
+.hover-action:focus,
+.hover-action:focus-within {
display: inline-flex !important;
}
-.hover-actions-parent:hover .hover-action {
+.hover-actions-parent:hover .hover-action,
+.hover-actions-parent:focus .hover-action,
+.hover-actions-parent:focus-within .hover-action {
display: inline-flex !important;
}
/* If parent has any children that have an open pop, show hover actions */
-.hover-actions-parent:has(button[aria-haspopup][data-state="open"])
+.hover-actions-parent:has([data-state="open"])
.hover-action {
display: inline-flex !important;
}