Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

refactor: replace hand-rolled auto-resize textareas with shared component (#176)

Gener744233105aea
diff --git a/src/web/src/components/agent-form-fields.tsx b/src/web/src/components/agent-form-fields.tsx
index c4450a704..250afc9b9 100644
--- a/src/web/src/components/agent-form-fields.tsx
+++ b/src/web/src/components/agent-form-fields.tsx
@@ -44,6 +44,7 @@ import {
import { useAgentContext } from "@/contexts/agent-context";
import { ApiError } from "@/lib/errors";
import { toast } from "sonner";
+import { AutoResizeTextarea } from "@/components/ui/auto-resize-textarea";
export function nameToHandle(name: string): string {
return name
@@ -132,23 +133,13 @@ export function GeneralFields({
</div>
{/* Description — frameless, auto-growing */}
- <textarea
+ <AutoResizeTextarea
id="agent-description"
value={description}
- onChange={(e) => {
- setDescription(e.target.value);
- e.target.style.height = "auto";
- e.target.style.height = `${e.target.scrollHeight}px`;
- }}
- ref={(el) => {
- if (el) {
- el.style.height = "auto";
- el.style.height = `${el.scrollHeight}px`;
- }
- }}
+ onChange={(e) => setDescription(e.target.value)}
placeholder="Add a description…"
rows={1}
- className="w-full resize-none overflow-hidden border-0 bg-transparent px-0 py-0.5 text-sm text-foreground shadow-none outline-none placeholder:text-muted-foreground/40 focus-visible:ring-0"
+ className="w-full border-0 bg-transparent px-0 py-0.5 text-sm text-foreground shadow-none outline-none placeholder:text-muted-foreground/40 focus-visible:ring-0"
/>
{emailHandleSlot}
@@ -238,23 +229,13 @@ export function GeneralFields({
{setInstructions && (
<div className="space-y-1">
<Label className="text-xs text-muted-foreground">Instructions</Label>
- <textarea
+ <AutoResizeTextarea
id="agent-instructions"
value={instructions ?? ""}
- onChange={(e) => {
- setInstructions(e.target.value);
- e.target.style.height = "auto";
- e.target.style.height = `${e.target.scrollHeight}px`;
- }}
- ref={(el) => {
- if (el) {
- el.style.height = "auto";
- el.style.height = `${el.scrollHeight}px`;
- }
- }}
+ onChange={(e) => setInstructions(e.target.value)}
placeholder="System prompt or instructions..."
rows={3}
- className="w-full resize-none overflow-hidden border-0 bg-transparent px-0 py-1 text-sm text-foreground shadow-none outline-none placeholder:text-muted-foreground/40 focus-visible:ring-0"
+ className="w-full border-0 bg-transparent px-0 py-1 text-sm text-foreground shadow-none outline-none placeholder:text-muted-foreground/40 focus-visible:ring-0"
/>
</div>
)}
diff --git a/src/web/src/components/calendar/calendar-event-sheet.tsx b/src/web/src/components/calendar/calendar-event-sheet.tsx
index 732990d89..c1ff9a126 100644
--- a/src/web/src/components/calendar/calendar-event-sheet.tsx
+++ b/src/web/src/components/calendar/calendar-event-sheet.tsx
@@ -34,6 +34,7 @@ import type {
CalendarEvent,
UpdateCalendarEventRequest,
} from "@alook/shared";
+import { AutoResizeTextarea } from "@/components/ui/auto-resize-textarea";
import { CalendarDatePicker } from "./calendar-date-picker";
import { CalendarTimePicker } from "./calendar-time-picker";
import {
@@ -555,34 +556,15 @@ export function CalendarEventSheet({
? title.trim() || "Untitled event"
: title.trim() || "New calendar event";
- const titleRef = useRef<HTMLTextAreaElement | null>(null);
-
- const resizeTitle = (el: HTMLTextAreaElement | null) => {
- if (!el) return;
- el.style.height = "auto";
- el.style.height = `${el.scrollHeight}px`;
- };
-
- useEffect(() => {
- resizeTitle(titleRef.current);
- }, [title]);
-
const titleInput = readonly ? (
<p className="w-full px-0 py-1 font-news text-xl sm:text-2xl md:text-3xl font-medium leading-[1.2] tracking-tight">
{title || "Untitled event"}
</p>
) : (
- <textarea
- ref={(el) => {
- titleRef.current = el;
- resizeTitle(el);
- }}
+ <AutoResizeTextarea
aria-label="Event title"
value={title}
- onChange={(e) => {
- setTitle(e.target.value);
- resizeTitle(e.target);
- }}
+ onChange={(e) => setTitle(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
@@ -593,7 +575,7 @@ export function CalendarEventSheet({
autoFocus={mode === "create"}
rows={1}
className={cn(
- "w-full resize-none overflow-hidden rounded-none border-0 bg-transparent px-0 py-1 font-news text-xl sm:text-2xl md:text-3xl font-medium leading-[1.2] tracking-tight",
+ "w-full rounded-none border-0 bg-transparent px-0 py-1 font-news text-xl sm:text-2xl md:text-3xl font-medium leading-[1.2] tracking-tight",
"shadow-none outline-none focus-visible:border-0 focus-visible:ring-0 focus-visible:ring-offset-0",
"placeholder:text-muted-foreground/40 placeholder:font-normal"
)}
diff --git a/src/web/src/components/issues/issue-sheet.tsx b/src/web/src/components/issues/issue-sheet.tsx
index 735b6cf02..5a1433f24 100644
--- a/src/web/src/components/issues/issue-sheet.tsx
+++ b/src/web/src/components/issues/issue-sheet.tsx
@@ -36,6 +36,7 @@ import type { TraceTask } from "@/lib/api";
import { updateIssue } from "@/lib/api";
import { AvatarRenderer, parseAvatarUrl } from "@/components/avatar";
import { ConfirmDialog } from "@/components/ui/confirm-dialog";
+import { AutoResizeTextarea } from "@/components/ui/auto-resize-textarea";
// --- Constants ---
@@ -243,7 +244,6 @@ export function IssueSheet({
const [confirmAgent, setConfirmAgent] = useState<Agent | null>(null);
const [dispatching, setDispatching] = useState(false);
- const titleRef = useRef<HTMLTextAreaElement>(null);
const descriptionRef = useRef<HTMLDivElement>(null);
const timelineRef = useRef<HTMLDivElement>(null);
const dragging = useRef(false);
@@ -325,18 +325,6 @@ export function IssueSheet({
prevOpenRef.current = open;
}, [open, mode, flushAutoSave]);
- // --- Title auto-resize ---
- const resizeTitle = useCallback((el?: HTMLTextAreaElement | null) => {
- const target = el ?? titleRef.current;
- if (!target) return;
- target.style.height = "auto";
- target.style.height = `${target.scrollHeight}px`;
- }, []);
-
- useEffect(() => {
- requestAnimationFrame(() => resizeTitle());
- }, [title, resizeTitle]);
-
// --- Drag handle ---
const onPointerDown = useCallback((e: React.PointerEvent) => {
e.preventDefault();
@@ -523,21 +511,14 @@ export function IssueSheet({
<>
{/* Title */}
<div className="shrink-0 px-2 sm:px-3 pt-5 pb-1">
- <textarea
- ref={(el) => {
- titleRef.current = el;
- resizeTitle(el);
- }}
+ <AutoResizeTextarea
value={title}
- onChange={(e) => {
- setTitle(e.target.value);
- resizeTitle(e.target);
- }}
+ onChange={(e) => setTitle(e.target.value)}
onKeyDown={onTitleKeyDown}
placeholder={mode === "create" ? "New issue" : "Untitled"}
autoFocus={mode === "create"}
rows={1}
- className="w-full resize-none overflow-hidden rounded-none border-0 bg-transparent px-0 py-1 font-news text-2xl md:text-3xl font-medium leading-[1.2] tracking-tight shadow-none outline-none focus-visible:border-0 focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:text-muted-foreground/40 placeholder:font-normal"
+ className="w-full rounded-none border-0 bg-transparent px-0 py-1 font-news text-2xl md:text-3xl font-medium leading-[1.2] tracking-tight shadow-none outline-none focus-visible:border-0 focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:text-muted-foreground/40 placeholder:font-normal"
/>
</div>
diff --git a/src/web/src/components/ui/auto-resize-textarea.tsx b/src/web/src/components/ui/auto-resize-textarea.tsx
new file mode 100644
index 000000000..d31534a10
--- /dev/null
+++ b/src/web/src/components/ui/auto-resize-textarea.tsx
@@ -0,0 +1,44 @@
+import * as React from "react"
+import { cn } from "@/lib/utils"
+
+const resize = (el: HTMLTextAreaElement | null) => {
+ if (!el) return
+ el.style.height = "auto"
+ el.style.height = `${el.scrollHeight}px`
+}
+
+const AutoResizeTextarea = React.forwardRef<
+ HTMLTextAreaElement,
+ React.ComponentProps<"textarea">
+>(({ className, onChange, ...props }, forwardedRef) => {
+ const innerRef = React.useRef<HTMLTextAreaElement | null>(null)
+
+ const setRef = React.useCallback(
+ (el: HTMLTextAreaElement | null) => {
+ innerRef.current = el
+ if (typeof forwardedRef === "function") forwardedRef(el)
+ else if (forwardedRef) forwardedRef.current = el
+ resize(el)
+ },
+ [forwardedRef]
+ )
+
+ React.useEffect(() => {
+ resize(innerRef.current)
+ }, [props.value])
+
+ return (
+ <textarea
+ ref={setRef}
+ className={cn("resize-none overflow-hidden", className)}
+ onChange={(e) => {
+ resize(e.target)
+ onChange?.(e)
+ }}
+ {...props}
+ />
+ )
+})
+AutoResizeTextarea.displayName = "AutoResizeTextarea"
+
+export { AutoResizeTextarea }