import * as React from "react"; import { cn } from "@/lib/utils"; export interface TextareaProps extends React.TextareaHTMLAttributes { autoGrow?: boolean; } const Textarea = React.forwardRef( ( { className, autoGrow = false, onInput, value, defaultValue, ...props }, ref, ) => { const textareaRef = React.useRef(null); const syncHeight = React.useCallback( (textarea: HTMLTextAreaElement | null = textareaRef.current) => { if (!autoGrow || !textarea) return; textarea.style.height = "auto"; const computedMaxHeight = Number.parseFloat( window.getComputedStyle(textarea).maxHeight, ); const hasMaxHeight = Number.isFinite(computedMaxHeight); const nextHeight = hasMaxHeight ? Math.min(textarea.scrollHeight, computedMaxHeight) : textarea.scrollHeight; textarea.style.height = `${nextHeight}px`; textarea.style.overflowY = hasMaxHeight && textarea.scrollHeight > computedMaxHeight ? "auto" : "hidden"; }, [autoGrow], ); React.useEffect(() => { syncHeight(); }, [syncHeight, value, defaultValue]); const setRef = React.useCallback( (node: HTMLTextAreaElement | null) => { textareaRef.current = node; if (typeof ref === "function") { ref(node); } else if (ref) { (ref as React.MutableRefObject).current = node; } }, [ref], ); return (