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: auto-grow text input for dataframe sql name (#2096)

Myles Scolnick3c181c00edc0
diff --git a/frontend/src/core/codemirror/language/panel.tsx b/frontend/src/core/codemirror/language/panel.tsx
index c35e8ea156..12e6cabab7 100644
--- a/frontend/src/core/codemirror/language/panel.tsx
+++ b/frontend/src/core/codemirror/language/panel.tsx
@@ -1,22 +1,28 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { EditorView } from "@codemirror/view";
+import type { EditorView } from "@codemirror/view";
import { languageAdapterState } from "./extension";
import { SQLLanguageAdapter } from "./sql";
import { normalizeName } from "@/core/cells/names";
+import { useAutoGrowInputProps } from "@/hooks/useAutoGrowInputProps";
export const LanguagePanelComponent: React.FC<{
view: EditorView;
}> = ({ view }) => {
const languageAdapter = view.state.field(languageAdapterState);
+ const { spanProps, inputProps } = useAutoGrowInputProps({ minWidth: 50 });
let actions: React.ReactNode = <div />;
if (languageAdapter instanceof SQLLanguageAdapter) {
actions = (
- <div className="flex gap-2">
+ <div className="flex gap-2 relative">
Output variable:{" "}
<input
+ {...inputProps}
defaultValue={languageAdapter.dataframeName}
- onChange={(e) => (languageAdapter.dataframeName = e.target.value)}
+ onChange={(e) => {
+ languageAdapter.dataframeName = e.target.value;
+ inputProps.onChange?.(e);
+ }}
onBlur={(e) => {
// Normalize the name to a valid variable name
const name = normalizeName(e.target.value, false);
@@ -32,8 +38,9 @@ export const LanguagePanelComponent: React.FC<{
},
});
}}
- className="w-20 border border-border rounded px-1 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
+ className="min-w-14 w-auto border border-border rounded px-1 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
/>
+ <span {...spanProps} />
</div>
);
}
diff --git a/frontend/src/hooks/useAutoGrowInputProps.ts b/frontend/src/hooks/useAutoGrowInputProps.ts
new file mode 100644
index 0000000000..f8f7687635
--- /dev/null
+++ b/frontend/src/hooks/useAutoGrowInputProps.ts
@@ -0,0 +1,56 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import type React from "react";
+import { useState, useRef } from "react";
+import { useOnMount } from "./useLifecycle";
+
+export function useAutoGrowInputProps(opts: {
+ minWidth?: number;
+ padding?: number;
+}) {
+ const { minWidth = 70, padding = 15 } = opts;
+ const [inputWidth, setInputWidth] = useState(50);
+ const inputRef = useRef<HTMLInputElement>(null);
+ const spanRef = useRef<HTMLSpanElement>(null);
+
+ const adjustWidth = (value: string) => {
+ if (spanRef.current && inputRef.current) {
+ const tmp = spanRef.current;
+ tmp.style.fontSize = window.getComputedStyle(inputRef.current).fontSize;
+ tmp.style.fontFamily = window.getComputedStyle(
+ inputRef.current,
+ ).fontFamily;
+ tmp.textContent = value;
+ const width = tmp.offsetWidth;
+ setInputWidth(width + padding);
+ }
+ };
+
+ // Run on mount
+ useOnMount(() => {
+ adjustWidth(inputRef.current?.value ?? "");
+ });
+
+ const inputProps: React.InputHTMLAttributes<HTMLInputElement> & {
+ ref: React.RefObject<HTMLInputElement>;
+ } = {
+ ref: inputRef,
+ type: "text",
+ onChange: (e) => {
+ adjustWidth(e.target.value);
+ },
+ style: { width: `${inputWidth}px`, minWidth: `${minWidth}px` },
+ };
+
+ const spanProps: React.HTMLAttributes<HTMLSpanElement> & {
+ ref: React.RefObject<HTMLSpanElement>;
+ } = {
+ ref: spanRef,
+ style: {
+ visibility: "hidden",
+ position: "absolute",
+ whiteSpace: "pre",
+ },
+ };
+
+ return { inputProps, spanProps };
+}