import * as React from "react"; import { cn } from "@/lib/utils.ts"; export interface CodeTextareaProps extends Omit, "spellCheck"> { showLineNumbers?: boolean; /** Applied to the outer bordered container when line numbers are shown. */ wrapperClassName?: string; } function countLines(value: string): number { if (!value) return 1; return value.split("\n").length; } const CodeTextarea = React.forwardRef( ({ className, wrapperClassName, value, showLineNumbers = true, onScroll, ...props }, ref) => { const gutterRef = React.useRef(null); const text = typeof value === "string" ? value : String(value ?? ""); const lineCount = countLines(text); const lineNumbers = React.useMemo( () => Array.from({ length: lineCount }, (_, i) => i + 1), [lineCount], ); const gutterWidthCh = Math.max(2, String(lineCount).length) + 1; const handleScroll = (e: React.UIEvent) => { if (gutterRef.current) { gutterRef.current.scrollTop = e.currentTarget.scrollTop; } onScroll?.(e); }; const editorClass = cn( "w-full flex-1 resize-none border-0 bg-transparent px-2 py-2 font-mono text-xs leading-5", "placeholder:text-muted-foreground focus-visible:outline-none", "whitespace-pre overflow-auto", className, ); if (!showLineNumbers) { return (