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
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.
Full Signature
maxHeight?number
Maximum height of the textarea in pixels before vertical scrolling is enabled.
Full Signature
onResize?function
Callback fired with the new pixel height whenever the textarea resizes.
Full Signature
Parameters
(heightnumberUseTextareaAutosizeReturn
Control methods returned by the hook.
Property
Specification
resizefunction
Imperatively recalculates and applies the textarea height.
Full Signature