Skip to Content
ohooks

useTextareaAutosize

Automatically adjusts the height of a <textarea> element to fit its content.

Loading preview...

The useTextareaAutosize hook dynamically resizes a <textarea> element's height to match its scrollHeight whenever value changes or the element resizes, respecting minHeight and maxHeight bounds and switching overflowY between 'hidden' and 'auto' automatically.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useTextareaAutosize.json

Usage

Examples

Auto-Growing Chat Composer with Maximum Height

Cap growth with maxHeight so the textarea expands up to a limit and then scrolls internally.

Triggering a Manual Resize Calculation

Call the returned resize() function after programmatic font size or layout changes, or listen to height changes via onResize.

API Reference

UseTextareaAutosizeOptions

Configuration options for minimum/maximum height bounds and resize callbacks.

Property
Specification
minHeight?
number
Minimum height of the textarea in pixels.
maxHeight?
number
Maximum height of the textarea in pixels before vertical scrolling is enabled.
onResize?
function
Callback fired with the new pixel height whenever the textarea resizes.

UseTextareaAutosizeReturn

Control methods returned by the hook.

Property
Specification
resize
function
Imperatively recalculates and applies the textarea height.