useAutosize
Grows a textarea with what you write between its minimum and maximum height, animating each change without ever touching the text.
pnpm dlx shadcn@latest add https://hextaui.com/r/use-autosize.jsonAdds the hook and anything it depends on to your project.
Copy and paste the following code into your project.
hooks/use-autosize.ts Update the import paths to match your project setup.
<Textarea autoResize> already uses this hook. Call it yourself when you render your own <textarea>, such as a chat composer or an inline editor.
On each change the hook sets the height to auto, reads the natural scrollHeight, and clamps it to the element's own CSS min-height and max-height. All of that happens before paint, so you never see the collapsed frame. The bounds stay in your CSS, so they can be responsive or use lh to count lines.
- Height changes animate over 180ms, and a change that comes in mid-animation starts from the current height, so fast typing never jumps. Under reduced motion the height snaps.
- Past
max-heightthe textarea scrolls. Otherwise its scrollbar stays hidden, so it doesn't flash on each new line. - It refits when the width changes, because text that wraps differently changes the height.
- It listens for
inputonwindowand only measures. It never reads or writes the value, so keystrokes are never delayed or dropped.
Controlled values
Typing fires input, but setting value from code doesn't. Pass the value as the third argument and the textarea refits when you insert a template or clear it.
- Set
rows={1}andresize-none. A manual resize handle fights the automatic height. - Turning
enabledoff or unmounting removes the inline height and overflow, handing the size back to your CSS. - On touch screens, use at least 16px text in the textarea, or iOS zooms in when it's focused.
| Prop | Type | Default |
|---|---|---|
refThe textarea to size. | RefObject<HTMLTextAreaElement | null> | – |
enabledWhether to size automatically. | boolean | – |
valueThe controlled value. Pass undefined for uncontrolled textareas. | unknown | – |
Textarea and InputGroupTextarea.