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: move toolbar above cell for better gutter usage (#2481)

Myles Scolnicke31776bad498
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 9027832dc8..cc2b9d49d9 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -29,7 +29,7 @@ import { useCellRenderCount } from "../../hooks/useCellRenderCount";
import { Functions } from "../../utils/functions";
import { Logger } from "../../utils/Logger";
import { CellDragHandle, SortableCell } from "./SortableCell";
-import { HTMLCellId } from "../../core/cells/ids";
+import { type CellId, HTMLCellId } from "../../core/cells/ids";
import type { Theme } from "../../theme/useTheme";
import {
CellActionsDropdown,
@@ -48,6 +48,10 @@ import { aiCompletionCellAtom } from "@/core/ai/state";
import { CollapsedCellBanner, CollapseToggle } from "./cell/collapse";
import { canCollapseOutline } from "@/core/dom/outline";
import { StopButton } from "@/components/editor/cell/StopButton";
+import type { CellConfig, RuntimeState } from "@/core/network/types";
+import { MoreHorizontalIcon } from "lucide-react";
+import { Toolbar, ToolbarItem } from "@/components/editor/cell/toolbar";
+import { cn } from "@/utils/cn";
/**
* Imperative interface of the cell.
@@ -458,7 +462,22 @@ const CellComponent = (
<div className={className} id={HTMLId}>
{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">
+ <div className="absolute right-2 -top-4 z-10">
+ <CellToolbar
+ edited={edited}
+ appClosed={appClosed}
+ status={status}
+ cellConfig={cellConfig}
+ needsRun={needsRun}
+ hasOutput={hasOutput}
+ cellActionDropdownRef={cellActionDropdownRef}
+ cellId={cellId}
+ name={name}
+ getEditorView={getEditorView}
+ onRun={handleRun}
+ />
+ </div>
+ <div className="absolute flex flex-col gap-[2px] justify-center h-full left-[-34px] z-20">
<CreateCellButton
tooltipContent={renderShortcut("cell.createAbove")}
appClosed={appClosed}
@@ -492,7 +511,7 @@ const CellComponent = (
editorViewRef={editorView}
hidden={cellConfig.hide_code}
/>
- <div className="shoulder-right">
+ <div className="shoulder-right z-20">
<CellStatusComponent
status={status}
staleInputs={staleInputs}
@@ -504,27 +523,8 @@ const CellComponent = (
runStartTimestamp={runStartTimestamp}
uninstantiated={uninstantiated}
/>
- <div className="flex gap-2 align-bottom">
- <RunButton
- edited={edited}
- onClick={appClosed ? Functions.NOOP : handleRun}
- appClosed={appClosed}
- status={status}
- config={cellConfig}
- needsRun={needsRun}
- />
- <StopButton status={status} appClosed={appClosed} />
- <CellActionsDropdown
- ref={cellActionDropdownRef}
- cellId={cellId}
- status={status}
- getEditorView={getEditorView}
- name={name}
- config={cellConfig}
- hasOutput={hasOutput}
- >
- <CellDragHandle />
- </CellActionsDropdown>
+ <div className="flex gap-2 items-end">
+ <CellDragHandle />
</div>
</div>
<div className="shoulder-bottom hover-action">
@@ -567,4 +567,64 @@ const CellComponent = (
);
};
+interface CellToolbarProps {
+ edited: boolean;
+ appClosed: boolean;
+ status: RuntimeState;
+ cellConfig: CellConfig;
+ needsRun: boolean;
+ hasOutput: boolean;
+ cellActionDropdownRef: React.RefObject<CellActionsDropdownHandle>;
+ cellId: CellId;
+ name: string;
+ getEditorView: () => EditorView | null;
+ onRun: () => void;
+}
+
+const CellToolbar = ({
+ edited,
+ appClosed,
+ status,
+ cellConfig,
+ needsRun,
+ hasOutput,
+ onRun,
+ cellActionDropdownRef,
+ cellId,
+ getEditorView,
+ name,
+}: CellToolbarProps) => {
+ return (
+ <Toolbar
+ className={cn(
+ // Show the toolbar on hover, or when the cell needs to be run
+ !needsRun && "hover-action",
+ )}
+ >
+ <RunButton
+ edited={edited}
+ onClick={appClosed ? Functions.NOOP : onRun}
+ appClosed={appClosed}
+ status={status}
+ config={cellConfig}
+ needsRun={needsRun}
+ />
+ <StopButton status={status} appClosed={appClosed} />
+ <CellActionsDropdown
+ ref={cellActionDropdownRef}
+ cellId={cellId}
+ status={status}
+ getEditorView={getEditorView}
+ name={name}
+ config={cellConfig}
+ hasOutput={hasOutput}
+ >
+ <ToolbarItem variant={"green"} tooltip={null}>
+ <MoreHorizontalIcon strokeWidth={1.5} />
+ </ToolbarItem>
+ </CellActionsDropdown>
+ </Toolbar>
+ );
+};
+
export const Cell = memo(forwardRef<CellHandle, CellProps>(CellComponent));
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index 03e36263b4..651ae9c7f2 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -330,18 +330,18 @@ const ExpandableOutput = React.memo(
<>
<div>
<div className="relative print:hidden">
- <div className="absolute top-1 -right-11 z-[1] flex flex-col gap-1">
+ <div className="absolute -right-10 top-1 z-[1] flex flex-col gap-1">
<Tooltip content="Fullscreen" side="left">
<Button
data-testid="fullscreen-output-button"
- className="hover-action hover:bg-muted"
+ className="hover-action hover:bg-muted p-1 hover:border-border border border-transparent"
onClick={async () => {
await containerRef.current?.requestFullscreen();
}}
size="xs"
variant="text"
>
- <ExpandIcon className="h-4 w-4" strokeWidth={1.25} />
+ <ExpandIcon className="size-4" strokeWidth={1.25} />
</Button>
</Tooltip>
{(isOverflowing || isExpanded) && (
diff --git a/frontend/src/components/editor/cell/DeleteButton.tsx b/frontend/src/components/editor/cell/DeleteButton.tsx
index b5885e083f..8fb65af59e 100644
--- a/frontend/src/components/editor/cell/DeleteButton.tsx
+++ b/frontend/src/components/editor/cell/DeleteButton.tsx
@@ -1,9 +1,9 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { Trash2Icon } from "lucide-react";
+import type { RuntimeState } from "@/core/network/types";
import { Tooltip } from "../../ui/tooltip";
import { Button } from "../../ui/button";
-import { cn } from "../../../utils/cn";
-import type { RuntimeState } from "@/core/network/types";
+import { cn } from "@/utils/cn";
export const DeleteButton = (props: {
status: RuntimeState;
diff --git a/frontend/src/components/editor/cell/RunButton.tsx b/frontend/src/components/editor/cell/RunButton.tsx
index 896466f4e6..08ae9a7efa 100644
--- a/frontend/src/components/editor/cell/RunButton.tsx
+++ b/frontend/src/components/editor/cell/RunButton.tsx
@@ -1,10 +1,8 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { HardDriveDownloadIcon, PlayIcon } from "lucide-react";
-import { Button } from "@/components/editor/inputs/Inputs";
-import { Tooltip } from "../../ui/tooltip";
import { renderShortcut } from "../../shortcuts/renderShortcut";
-import { cn } from "../../../utils/cn";
import type { RuntimeState, CellConfig } from "@/core/network/types";
+import { ToolbarItem } from "./toolbar";
function computeColor(
appClosed: boolean,
@@ -16,12 +14,12 @@ function computeColor(
return "disabled";
}
if (needsRun && !loading) {
- return "yellow";
+ return "stale";
}
if (loading || inactive) {
return "disabled";
}
- return "hint-green";
+ return "green";
}
export const RunButton = (props: {
@@ -38,47 +36,32 @@ export const RunButton = (props: {
const loading = status === "running" || status === "queued";
const inactive =
appClosed || loading || (!config.disabled && blockedStatus && !edited);
- const color = computeColor(appClosed, needsRun, loading, inactive);
+ const variant = computeColor(appClosed, needsRun, loading, inactive);
if (config.disabled) {
return (
- <Tooltip content="Add code to notebook" usePortal={false}>
- <Button
- className={cn(
- !needsRun && "hover-action",
- inactive && "inactive-button",
- )}
- onClick={onClick}
- color={color}
- shape="circle"
- size="small"
- data-testid="run-button"
- >
- <HardDriveDownloadIcon strokeWidth={1.8} />
- </Button>
- </Tooltip>
+ <ToolbarItem
+ tooltip="Add code to notebook"
+ disabled={inactive}
+ onClick={onClick}
+ variant={variant}
+ data-testid="run-button"
+ >
+ <HardDriveDownloadIcon />
+ </ToolbarItem>
);
}
if (!config.disabled && blockedStatus && !edited) {
return (
- <Tooltip
- content="This cell can't be run because it has a disabled ancestor"
- usePortal={false}
+ <ToolbarItem
+ disabled={inactive}
+ tooltip="This cell can't be run because it has a disabled ancestor"
+ onClick={onClick}
+ variant={variant}
+ data-testid="run-button"
>
- <Button
- className={cn(
- !needsRun && "hover-action",
- inactive && "inactive-button",
- )}
- onClick={onClick}
- color={color}
- shape="circle"
- size="small"
- data-testid="run-button"
- >
- <PlayIcon strokeWidth={1.8} />
- </Button>
- </Tooltip>
+ <PlayIcon strokeWidth={1.2} />
+ </ToolbarItem>
);
}
@@ -94,20 +77,14 @@ export const RunButton = (props: {
}
return (
- <Tooltip content={tooltipMsg} usePortal={false}>
- <Button
- className={cn(
- !needsRun && status !== "running" && "hover-action",
- inactive && "inactive-button",
- )}
- onClick={onClick}
- color={color}
- shape="circle"
- size="small"
- data-testid="run-button"
- >
- <PlayIcon strokeWidth={1.8} />
- </Button>
- </Tooltip>
+ <ToolbarItem
+ tooltip={tooltipMsg}
+ disabled={inactive}
+ onClick={onClick}
+ variant={variant}
+ data-testid="run-button"
+ >
+ <PlayIcon strokeWidth={1.2} />
+ </ToolbarItem>
);
};
diff --git a/frontend/src/components/editor/cell/StopButton.tsx b/frontend/src/components/editor/cell/StopButton.tsx
index bc33642471..076d2fea55 100644
--- a/frontend/src/components/editor/cell/StopButton.tsx
+++ b/frontend/src/components/editor/cell/StopButton.tsx
@@ -1,13 +1,11 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { SquareIcon } from "lucide-react";
-import { Button } from "@/components/editor/inputs/Inputs";
-import { Tooltip } from "../../ui/tooltip";
import { renderShortcut } from "../../shortcuts/renderShortcut";
-import { cn } from "../../../utils/cn";
import { sendInterrupt } from "@/core/network/requests";
import { useShouldShowInterrupt } from "./useShouldShowInterrupt";
import type { RuntimeState } from "@/core/network/types";
import { Functions } from "@/utils/functions";
+import { ToolbarItem } from "./toolbar";
export const StopButton = (props: {
status: RuntimeState;
@@ -21,21 +19,14 @@ export const StopButton = (props: {
const showInterrupt = useShouldShowInterrupt(running);
return (
- <Tooltip content={renderShortcut("global.interrupt")} usePortal={false}>
- <Button
- className={cn(
- !showInterrupt && "hover-action",
- (appClosed || !showInterrupt) &&
- "inactive-button active:shadow-xsSolid",
- )}
- onClick={showInterrupt ? sendInterrupt : Functions.NOOP}
- color={showInterrupt ? "yellow" : "disabled"}
- shape="circle"
- size="small"
- data-testid="run-button"
- >
- <SquareIcon strokeWidth={1.5} />
- </Button>
- </Tooltip>
+ <ToolbarItem
+ tooltip={renderShortcut("global.interrupt")}
+ disabled={appClosed || !showInterrupt}
+ onClick={showInterrupt ? sendInterrupt : Functions.NOOP}
+ variant={showInterrupt ? "stale" : "disabled"}
+ data-testid="run-button"
+ >
+ <SquareIcon strokeWidth={1.5} />
+ </ToolbarItem>
);
};
diff --git a/frontend/src/components/editor/cell/cell-actions.tsx b/frontend/src/components/editor/cell/cell-actions.tsx
index b7e1930104..fdde555c23 100644
--- a/frontend/src/components/editor/cell/cell-actions.tsx
+++ b/frontend/src/components/editor/cell/cell-actions.tsx
@@ -25,7 +25,10 @@ import {
TooltipRoot,
TooltipTrigger,
} from "@/components/ui/tooltip";
-import { renderMinimalShortcut } from "@/components/shortcuts/renderShortcut";
+import {
+ renderMinimalShortcut,
+ renderShortcut,
+} from "@/components/shortcuts/renderShortcut";
import React from "react";
import {
type CellActionButtonProps,
@@ -115,23 +118,14 @@ const CellActionsDropdownInternal = (
}
const tooltipContent = (
- <TooltipContent className="w-full bg-card" tabIndex={-1}>
- <div className="text-foreground-muted flex flex-col text-center">
- <span>
- <span className="text-foreground font-semibold">Drag </span>to move
- cell
- </span>
- <span>
- <span className="text-foreground font-semibold">Click </span>to open
- menu
- </span>
- </div>
+ <TooltipContent tabIndex={-1}>
+ {renderShortcut("cell.cellActions")}
</TooltipContent>
);
return (
<Popover open={open} onOpenChange={setOpen}>
- <TooltipRoot delayDuration={400} disableHoverableContent={true}>
+ <TooltipRoot delayDuration={200} disableHoverableContent={true}>
{!open && tooltipContent}
{/* This creates a warning in React due to nested <button> elements.
Adding asChild could fix this, but it also changes the styling (is hidden) of the button when
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 8161d96a66..0ea2383831 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -369,6 +369,12 @@ const CellEditorInternal = ({
className="relative w-full"
onFocus={() => setLastFocusedCellId(cellId)}
>
+ {hidden && <HideCodeButton onClick={showCode} />}
+ <CellCodeMirrorEditor
+ className={cn(hidden && "opacity-20 h-8 overflow-hidden")}
+ editorView={editorViewRef.current}
+ ref={editorViewParentRef}
+ />
{!hidden && (
<div className="absolute top-1 right-5">
<LanguageToggles
@@ -379,12 +385,6 @@ const CellEditorInternal = ({
/>
</div>
)}
- {hidden && <HideCodeButton onClick={showCode} />}
- <CellCodeMirrorEditor
- className={cn(hidden && "opacity-20 h-8 overflow-hidden")}
- editorView={editorViewRef.current}
- ref={editorViewParentRef}
- />
</div>
</AiCompletionEditor>
);
diff --git a/frontend/src/components/editor/cell/code/language-toggle.tsx b/frontend/src/components/editor/cell/code/language-toggle.tsx
index c35d4779df..aeefd4b0f4 100644
--- a/frontend/src/components/editor/cell/code/language-toggle.tsx
+++ b/frontend/src/components/editor/cell/code/language-toggle.tsx
@@ -35,7 +35,7 @@ export const LanguageToggles: React.FC<LanguageTogglesProps> = ({
);
return (
- <div className="absolute right-2 top-0 z-20 flex hover-action">
+ <div className="absolute right-2 top-2 z-20 flex hover-action">
<LanguageToggle
editorView={editorView}
currentLanguageAdapter={currentLanguageAdapter}
diff --git a/frontend/src/components/editor/cell/toolbar.tsx b/frontend/src/components/editor/cell/toolbar.tsx
new file mode 100644
index 0000000000..cf22625ed5
--- /dev/null
+++ b/frontend/src/components/editor/cell/toolbar.tsx
@@ -0,0 +1,95 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import React from "react";
+import { Tooltip } from "@/components/ui/tooltip";
+import { cn } from "@/utils/cn";
+import { Toolbar as ReactAriaToolbar } from "react-aria-components";
+import { cva, type VariantProps } from "class-variance-authority";
+
+const toolbarItemVariants = cva(
+ "rounded-full shadow-xsSolid border p-[6px] transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 [&>svg]:size-3 active:shadow-none",
+ {
+ variants: {
+ variant: {
+ default: "hover:bg-accent hover:text-accent-foreground",
+ stale:
+ "bg-[var(--yellow-3)] hover:bg-[var(--yellow-4)] text-[var(--yellow-11)]",
+ green:
+ "hover:bg-[var(--grass-2)] hover:text-[var(--grass-11)] hover:border-[var(--grass-7)]",
+ disabled: "opacity-50 cursor-not-allowed",
+ danger: "hover:bg-[var(--red-3)] hover:text-[var(--red-11)]",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ },
+ },
+);
+
+interface ToolbarItemProps
+ extends VariantProps<typeof toolbarItemVariants>,
+ React.HTMLAttributes<HTMLButtonElement> {
+ tooltip: React.ReactNode;
+ disabled?: boolean;
+}
+
+export const ToolbarItem: React.FC<ToolbarItemProps> = ({
+ children,
+ tooltip,
+ disabled = false,
+ variant,
+ ...rest
+}) => {
+ const content = (
+ <button
+ disabled={disabled}
+ {...rest}
+ onClick={(evt) => {
+ if (!disabled) {
+ // evt.preventDefault();
+ rest.onClick?.(evt);
+ }
+ }}
+ onMouseDown={(evt) => {
+ // Prevent focus on the toolbar after clicking
+ evt.preventDefault();
+ }}
+ className={cn(
+ toolbarItemVariants({ variant }),
+ disabled && "opacity-50 cursor-not-allowed",
+ rest.className,
+ )}
+ >
+ {children}
+ </button>
+ );
+
+ if (tooltip) {
+ return (
+ <Tooltip
+ content={tooltip}
+ side="top"
+ delayDuration={200}
+ usePortal={false}
+ >
+ {content}
+ </Tooltip>
+ );
+ }
+ return content;
+};
+
+interface ToolbarProps {
+ className?: string;
+ children: React.ReactNode;
+}
+
+export const Toolbar: React.FC<ToolbarProps> = ({ children, className }) => (
+ <ReactAriaToolbar
+ className={cn(
+ "flex items-center gap-1 bg-background p-[2px] rounded-md",
+ className,
+ )}
+ >
+ {children}
+ </ReactAriaToolbar>
+);
diff --git a/frontend/src/components/editor/renderers/CellArray.tsx b/frontend/src/components/editor/renderers/CellArray.tsx
index 47d8e3d1e9..f3ff1ad9f8 100644
--- a/frontend/src/components/editor/renderers/CellArray.tsx
+++ b/frontend/src/components/editor/renderers/CellArray.tsx
@@ -96,6 +96,7 @@ export const CellArray: React.FC<CellArrayProps> = ({
className="pb-[40vh]"
invisible={invisible}
appConfig={appConfig}
+ innerClassName="pr-4" // For the floating actions
>
<PackageAlert />
<NotebookBanner />
diff --git a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
index ae00f1df5c..1218d1eec9 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
@@ -5,6 +5,7 @@ import type { AppConfig } from "@/core/config/config-schema";
interface Props {
className?: string;
+ innerClassName?: string;
appConfig: AppConfig;
invisible?: boolean;
}
@@ -14,6 +15,7 @@ export const VerticalLayoutWrapper: React.FC<PropsWithChildren<Props>> = ({
appConfig,
className,
children,
+ innerClassName,
}) => {
return (
<div
@@ -29,9 +31,10 @@ export const VerticalLayoutWrapper: React.FC<PropsWithChildren<Props>> = ({
appConfig.width === "compact" && "max-w-contentWidth min-w-[400px]",
appConfig.width === "medium" &&
"max-w-contentWidthMedium min-w-[400px]",
- appConfig.width === "full" && "max-w-full pr-10 xl:pr-4",
+ appConfig.width === "full" && "max-w-full",
// Hide the cells for a fake loading effect, to avoid flickering
invisible && "invisible",
+ innerClassName,
)}
>
{children}
diff --git a/frontend/src/core/codemirror/language/panel.tsx b/frontend/src/core/codemirror/language/panel.tsx
index 6031b18276..01859def1b 100644
--- a/frontend/src/core/codemirror/language/panel.tsx
+++ b/frontend/src/core/codemirror/language/panel.tsx
@@ -68,7 +68,7 @@ export const LanguagePanelComponent: React.FC<{
}
return (
- <div className="flex justify-between items-center gap-4 px-2 pt-2">
+ <div className="flex justify-between items-center gap-4 pl-2 pt-2">
{actions}
{showDivider && <div className="h-4 border-r border-border" />}
{languageAdapter.type}
diff --git a/frontend/src/css/app/Cell.css b/frontend/src/css/app/Cell.css
index e094427ab6..2f18c28fcb 100644
--- a/frontend/src/css/app/Cell.css
+++ b/frontend/src/css/app/Cell.css
@@ -202,14 +202,12 @@
right: -94px;
width: 80px;
gap: 4px;
- z-index: 2;
}
.shoulder-bottom {
position: absolute;
bottom: -2px;
right: 2px;
- z-index: 2; /* lie above editor */
}
@apply bg-background;
@@ -304,9 +302,7 @@
.Cell .cm-editor {
border: 1px solid transparent;
padding: 3px;
-
- /* leave some room for UI elements inside the code editor */
- padding-right: 24px;
+ padding-right:24px;
&.cm-focused {
.cm-activeLineGutter {
diff --git a/frontend/src/css/app/codemirror.css b/frontend/src/css/app/codemirror.css
index 239851b40b..6dfcd2a74c 100644
--- a/frontend/src/css/app/codemirror.css
+++ b/frontend/src/css/app/codemirror.css
@@ -133,6 +133,9 @@
color: var(--sky-11);
font-weight: 700;
+ /* room for buttons */
+ margin-right: 30px;
+
@apply text-xs;
}
diff --git a/frontend/src/css/common.css b/frontend/src/css/common.css
index e8e17ad867..affa3dd779 100644
--- a/frontend/src/css/common.css
+++ b/frontend/src/css/common.css
@@ -18,7 +18,6 @@
}
/* If parent has any children that have an open pop, show hover actions */
-.hover-actions-parent:has([data-state="open"])
- .hover-action {
+.hover-actions-parent:has([data-state='open']) .hover-action {
display: inline-flex !important;
}