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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Add stopPropagation event handler to input components and improve grid layout's drag and drop feature (#243)
Myles Scolnick8283a504f768
diff --git a/frontend/src/components/ui/input.tsx b/frontend/src/components/ui/input.tsxindex a4879098f7..9148a505a1 100644--- a/frontend/src/components/ui/input.tsx+++ b/frontend/src/components/ui/input.tsx@@ -2,6 +2,7 @@import * as React from "react";import { cn } from "@/lib/utils";+import { Events } from "@/utils/events";export type InputProps = React.InputHTMLAttributes<HTMLInputElement> & {icon?: React.ReactNode;@@ -29,6 +30,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(className)}ref={ref}+ onClick={Events.stopPropagation()}{...props}/>{endAdornment && (diff --git a/frontend/src/components/ui/native-select.tsx b/frontend/src/components/ui/native-select.tsxindex bb54f4b6b0..10dad5b8d2 100644--- a/frontend/src/components/ui/native-select.tsx+++ b/frontend/src/components/ui/native-select.tsx@@ -2,6 +2,7 @@import * as React from "react";import { cn } from "@/lib/utils";+import { Events } from "@/utils/events";const NativeSelect = React.forwardRef<HTMLSelectElement,@@ -9,6 +10,7 @@ const NativeSelect = React.forwardRef<>(({ className, children, ...props }, ref) => (<selectref={ref}+ onClick={Events.stopPropagation()}className={cn("flex h-6 w-fit mb-1 shadow-xsSolid items-center justify-between rounded-sm border border-input bg-transparent px-2 text-sm font-prose ring-offset-background placeholder:text-muted-foreground hover:shadow-smSolid focus:outline-none focus:ring-1 focus:ring-ring focus:border-primary focus:shadow-mdSolid disabled:cursor-not-allowed disabled:opacity-50 cursor-pointer",classNamediff --git a/frontend/src/components/ui/textarea.tsx b/frontend/src/components/ui/textarea.tsxindex 5f27f62411..2d0e8724f9 100644--- a/frontend/src/components/ui/textarea.tsx+++ b/frontend/src/components/ui/textarea.tsx@@ -2,6 +2,7 @@import * as React from "react";import { cn } from "@/lib/utils";+import { Events } from "@/utils/events";export interface TextareaPropsextends React.TextareaHTMLAttributes<HTMLTextAreaElement> {@@ -18,6 +19,7 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>("flex h-20 w-full mb-1 rounded-sm border border-input bg-transparent px-3 py-2 text-sm font-code ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:border-accent disabled:cursor-not-allowed disabled:opacity-50 min-h-[1.5rem]",className)}+ onClick={Events.stopPropagation()}ref={ref}{...props}/>diff --git a/frontend/src/editor/renderers/grid-layout/grid-layout.tsx b/frontend/src/editor/renderers/grid-layout/grid-layout.tsxindex a0b2094156..8b11b8da42 100644--- a/frontend/src/editor/renderers/grid-layout/grid-layout.tsx+++ b/frontend/src/editor/renderers/grid-layout/grid-layout.tsx@@ -19,10 +19,11 @@ import { CellId } from "@/core/model/ids";import { AppMode } from "@/core/mode";import { TinyCode } from "@/editor/cell/TinyCode";import { cn } from "@/lib/utils";-import { XIcon } from "lucide-react";+import { LockIcon, XIcon } from "lucide-react";import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";-import useEvent from "react-use-event-hook";+import { useIsDragging } from "@/hooks/useIsDragging";+import { Switch } from "@/components/ui/switch";type Props = ICellRendererProps<GridLayout>;@@ -37,13 +38,13 @@ const GridLayoutRenderer: React.FC<Props> = ({mode,}) => {const isReading = mode === "read";- const [isDragging, setIsDragging] = useState(false);const inGridIds = new Set(layout.cells.map((cell) => cell.i));const [droppingItem, setDroppingItem] = useState<{i: string;w?: number;h?: number;} | null>(null);+ const [isLocked, setIsLocked] = useState(false);const cols = useMemo(() => ({@@ -55,8 +56,9 @@ const GridLayoutRenderer: React.FC<Props> = ({[layout.columns]);- const handleDraggingTrue = useEvent(() => setIsDragging(true));- const handleDraggingFalse = useEvent(() => setIsDragging(false));+ const { isDragging, ...dragProps } = useIsDragging();++ const enableInteractions = !isReading && !isLocked;const grid = (<ReactGridLayout@@ -91,7 +93,7 @@ const GridLayoutRenderer: React.FC<Props> = ({: undefined}onDrop={(cellLayouts, dropped, _event) => {- setIsDragging(false);+ dragProps.onDragStop();if (!dropped) {return;}@@ -100,12 +102,20 @@ const GridLayoutRenderer: React.FC<Props> = ({cells: [...cellLayouts, dropped],});}}- onDragStart={handleDraggingTrue}- onDragStop={handleDraggingFalse}- // When in read mode, disable dragging and resizing- isDraggable={!isReading}- isDroppable={!isReading}- isResizable={!isReading}+ onDragStart={(_layout, _oldItem, _newItem, _placeholder, event) => {+ dragProps.onDragStart(event);+ }}+ onDrag={(_layout, _oldItem, _newItem, _placeholder, event) => {+ dragProps.onDragMove(event);+ }}+ onDragStop={() => {+ dragProps.onDragStop();+ }}+ // When in read mode or locked, disable dragging and resizing+ isDraggable={enableInteractions}+ isDroppable={enableInteractions}+ isResizable={enableInteractions}+ draggableHandle={enableInteractions ? undefined : "noop"}>{cells.filter((cell) => inGridIds.has(cell.key))@@ -113,8 +123,8 @@ const GridLayoutRenderer: React.FC<Props> = ({<divkey={cell.key}className={cn(- "relative transparent-when-disconnected",- !isReading &&+ "relative transparent-when-disconnected z-10",+ enableInteractions &&"bg-background hover:bg-[var(--slate-2)] border-transparent hover:border-border border hover:rounded hover-actions-parent",isDragging && "bg-[var(--slate-2)] border-border rounded")}@@ -127,7 +137,7 @@ const GridLayoutRenderer: React.FC<Props> = ({status={cell.status}hidden={cell.errored || cell.interrupted || cell.stopped}/>- {!isReading && (+ {enableInteractions && (<div className="absolute top-0 right-0 p-1 hover-action"><XIconclassName="cursor-pointer h-4 w-4 opacity-60 hover:opacity-100"@@ -155,8 +165,9 @@ const GridLayoutRenderer: React.FC<Props> = ({<><div className="flex flex-row absolute left-5 top-4 gap-4"><div className="flex flex-row items-center gap-2">- <Label>Columns</Label>+ <Label htmlFor="columns">Columns</Label><Input+ id="columns"type="number"value={layout.columns}className="w-[60px]"@@ -171,8 +182,9 @@ const GridLayoutRenderer: React.FC<Props> = ({/></div><div className="flex flex-row items-center gap-2">- <Label>Row Height (px)</Label>+ <Label htmlFor="rowHeight">Row Height (px)</Label><Input+ id="rowHeight"type="number"value={layout.rowHeight}className="w-[60px]"@@ -186,17 +198,36 @@ const GridLayoutRenderer: React.FC<Props> = ({}}/></div>+ <div className="flex flex-row items-center gap-2">+ <Label className="flex flex-row items-center gap-1" htmlFor="lock">+ <LockIcon className="h-3 w-3" />+ Lock Grid+ </Label>+ <Switch+ id="lock"+ checked={isLocked}+ size="sm"+ onCheckedChange={setIsLocked}+ />+ </div></div><divclassName={cn("relative flex h-full overflow-hidden gap-2 px-2 pb-2")}><div- className="flex-grow overflow-auto border rounded bg-[var(--slate-2)] shadow-sm transparent-when-disconnected"- style={{- backgroundImage:- "repeating-linear-gradient(var(--gray-4) 0 1px, transparent 1px 100%), repeating-linear-gradient(90deg, var(--gray-4) 0 1px, transparent 1px 100%)",- backgroundSize: `calc((100% / ${layout.columns})) ${layout.rowHeight}px`,- }}+ className={cn(+ "flex-grow overflow-auto border rounded bg-[var(--slate-2)] shadow-sm transparent-when-disconnected",+ !enableInteractions && "bg-background"+ )}+ style={+ enableInteractions+ ? {+ backgroundImage:+ "repeating-linear-gradient(var(--gray-4) 0 1px, transparent 1px 100%), repeating-linear-gradient(90deg, var(--gray-4) 0 1px, transparent 1px 100%)",+ backgroundSize: `calc((100% / ${layout.columns})) ${layout.rowHeight}px`,+ }+ : undefined+ }>{grid}</div>diff --git a/frontend/src/hooks/useIsDragging.tsx b/frontend/src/hooks/useIsDragging.tsxnew file mode 100644index 0000000000..947d70ecaa--- /dev/null+++ b/frontend/src/hooks/useIsDragging.tsx@@ -0,0 +1,39 @@+/* Copyright 2023 Marimo. All rights reserved. */+import React, { useState } from "react";++const OFFSET = 10;++/**+ * Returns whether the user is currently dragging an element.+ * Includes an offset to prevent accidental dragging.+ */+export function useIsDragging() {+ const [mouseIsDown, setMouseIsDown] = useState(false);+ const [initialPosition, setInitialPosition] = React.useState({ x: 0, y: 0 });+ const [isDragging, setIsDragging] = React.useState(false);++ const handleDragStart = (event: MouseEvent) => {+ setMouseIsDown(true);+ setInitialPosition({ x: event.clientX, y: event.clientY });+ };++ const handleDragMove = (event: MouseEvent) => {+ if (mouseIsDown) {+ const dx = event.clientX - initialPosition.x;+ const dy = event.clientY - initialPosition.y;+ setIsDragging(Math.sqrt(dx * dx + dy * dy) > OFFSET);+ }+ };++ const handleDragStop = () => {+ setMouseIsDown(false);+ setIsDragging(false);+ };++ return {+ isDragging: mouseIsDown && isDragging,+ onDragStart: handleDragStart,+ onDragMove: handleDragMove,+ onDragStop: handleDragStop,+ };+}diff --git a/frontend/src/utils/events.ts b/frontend/src/utils/events.tsnew file mode 100644index 0000000000..c46a75f31b--- /dev/null+++ b/frontend/src/utils/events.ts@@ -0,0 +1,13 @@+/* Copyright 2023 Marimo. All rights reserved. */+export const Events = {+ stopPropagation: <E extends Pick<Event, "stopPropagation">>(+ callback?: (evt: E) => void+ ) => {+ return (event: E) => {+ event.stopPropagation();+ if (callback) {+ callback(event);+ }+ };+ },+};