1
0
Fork 0

feat(npc-dialogue): auto-grow the dialogue line box to fit its content

The Line textarea in the dialogue node editor grows to fit its content
up to a 200px default max, so long lines no longer scroll in a
fixed-height box. It stays drag-resizable (resize-y) and the user can
still expand it past the max.

The auto-grow hooks run above the early-return guard so their call
order never changes, fixing a Rules of Hooks violation the change would
otherwise introduce.
This commit is contained in:
Jason Fraley 2026-10-07 14:00:15 -04:00
parent f643028638
commit 9f0f7db612

View file

@ -1,6 +1,6 @@
"use client";
import { useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { XIcon } from "lucide-react";
import {
@ -64,6 +64,10 @@ const CONDITION_OP_LABELS: Record<DialogueCondition["op"], string> = {
/** Ops that compare against a number (everything except is_true/is_false). */
const NUMERIC_OPS: DialogueCondition["op"][] = ["gte", "lte", "gt", "lt", "eq"];
/** Max height (px) the dialogue Line box auto-grows to. Longer content scrolls
* inside the box from here; the user can still drag-resize it taller. */
const MAX_LINE_HEIGHT_PX = 200;
/** Short readable label for a node in the target pickers, with a text snippet. */
function nodeLabel(node: DialogueNode): string {
const d = node.data;
@ -139,11 +143,30 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N
setDraft(node?.data ?? null);
}, [node?.id, open]);
if (!node || !draft) return null;
const patch = (partial: Partial<DialogueNodeData>) =>
setDraft((prev) => (prev ? { ...prev, ...partial } : prev));
const lineRef = useRef<HTMLTextAreaElement>(null);
/** Auto-grow the line box to its content up to MAX_LINE_HEIGHT_PX so long
* lines do not scroll in a fixed-height box. The user can still drag-resize
* it taller; typing re-sizes it to fit. */
const autoSizeLine = useCallback(() => {
const el = lineRef.current;
if (!el) return;
el.style.height = "auto";
el.style.height = `${Math.min(el.scrollHeight, MAX_LINE_HEIGHT_PX)}px`;
}, []);
useEffect(() => {
autoSizeLine();
}, [draft?.body, autoSizeLine]);
// Guard lives below every hook so their call order never changes: on the
// first render draft is still null, and returning early here would skip the
// hooks defined above it on later renders (a Rules of Hooks violation).
if (!node || !draft) return null;
const patchEffect = (partial: Partial<NpcDialogueEffect>) =>
patch({ effect: { type: "notify", ...draft.effect, ...partial } });
@ -211,11 +234,15 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N
<div className="flex flex-col gap-1.5">
<Label className="text-[10px] uppercase tracking-wider text-white/50">Line</Label>
<Textarea
ref={lineRef}
value={draft.body ?? ""}
onChange={(e) => patch({ body: e.target.value || undefined })}
onChange={(e) => {
patch({ body: e.target.value || undefined });
autoSizeLine();
}}
placeholder="The NPC says…"
rows={4}
className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30"
className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30 resize-y"
/>
<p className="text-[10px] text-white/40">
Line breaks are kept. **bold**, *italic*, {`{red}color{/}`} and {'`code`'} render