React textarea component that automatically resizes to fit content with configurable row limits
npx @tessl/cli install tessl/npm-react-textarea-autosize@8.5.0React Textarea Autosize is a drop-in replacement for the standard HTML textarea element that automatically resizes based on content. The component maintains all standard textarea functionality while intelligently adjusting height as users type, with configurable minimum and maximum row limits.
npm install react-textarea-autosizeimport TextareaAutosize from "react-textarea-autosize";
import type { TextareaAutosizeProps, TextareaHeightChangeMeta } from "react-textarea-autosize";Note: The component is the default export. TypeScript types are available for import using import type syntax.
For CommonJS:
const TextareaAutosize = require("react-textarea-autosize").default;import React from "react";
import TextareaAutosize from "react-textarea-autosize";
function MyForm() {
return (
<div>
<TextareaAutosize
placeholder="Enter your message..."
minRows={3}
maxRows={10}
style={{ width: "100%" }}
/>
</div>
);
}React Textarea Autosize is built around several key components:
TextareaAutosize - A React component using forwardRef for direct textarea accessA React component that renders an auto-resizing textarea element with all standard textarea functionality plus intelligent height adjustment.
declare const TextareaAutosize: React.ForwardRefExoticComponent<
TextareaAutosizeProps & React.RefAttributes<HTMLTextAreaElement>
>;
interface TextareaAutosizeProps extends Omit<React.TextareaHTMLAttributes<HTMLTextAreaElement>, 'style'> {
/** Maximum number of rows the textarea can grow to */
maxRows?: number;
/** Minimum number of rows to show */
minRows?: number;
/** Callback fired when textarea height changes */
onHeightChange?: (height: number, meta: TextareaHeightChangeMeta) => void;
/** Whether to cache measurements for performance optimization */
cacheMeasurements?: boolean;
/** Style object that excludes maxHeight and minHeight properties */
style?: Style;
}
interface TextareaHeightChangeMeta {
/** Height of a single row in pixels */
rowHeight: number;
}
type Style = Omit<
NonNullable<React.TextareaHTMLAttributes<HTMLTextAreaElement>['style']>,
'maxHeight' | 'minHeight'
> & {
/** Height can be set as a number (in pixels) */
height?: number;
};Usage Examples:
import React, { useState } from "react";
import TextareaAutosize from "react-textarea-autosize";
// Basic auto-resizing textarea
function BasicExample() {
return (
<TextareaAutosize
placeholder="Type something..."
style={{ width: "100%", padding: 8 }}
/>
);
}
// Controlled component with value
function ControlledExample() {
const [value, setValue] = useState("");
return (
<TextareaAutosize
value={value}
onChange={(e) => setValue(e.target.value)}
minRows={2}
maxRows={8}
placeholder="Enter your story..."
/>
);
}
// With height change callback
function CallbackExample() {
const handleHeightChange = (height: number, meta: TextareaHeightChangeMeta) => {
console.log(`New height: ${height}px, Row height: ${meta.rowHeight}px`);
};
return (
<TextareaAutosize
onHeightChange={handleHeightChange}
minRows={1}
maxRows={5}
placeholder="Type to see height changes..."
/>
);
}
// With ref for direct access
function RefExample() {
const textareaRef = React.useRef<HTMLTextAreaElement>(null);
const focusTextarea = () => {
textareaRef.current?.focus();
};
return (
<div>
<TextareaAutosize
ref={textareaRef}
placeholder="Click button to focus"
/>
<button onClick={focusTextarea}>Focus Textarea</button>
</div>
);
}
// Performance optimized with caching
function CachedExample() {
return (
<TextareaAutosize
cacheMeasurements={true}
minRows={3}
maxRows={15}
placeholder="Large content with cached measurements..."
/>
);
}Control the minimum and maximum size of the textarea using row-based constraints.
interface RowProps {
/** Minimum number of rows to display (default: 1) */
minRows?: number;
/** Maximum number of rows before scrolling (default: Infinity) */
maxRows?: number;
}minRows: Sets the minimum height in rows - textarea won't shrink below this sizemaxRows: Sets the maximum height in rows - textarea will scroll if content exceeds this sizemaxRows defaulting to Infinity (no limit)Receive notifications when the textarea height changes, useful for layout adjustments or analytics.
type HeightChangeHandler = (height: number, meta: TextareaHeightChangeMeta) => void;
interface TextareaHeightChangeMeta {
/** The calculated height of a single row in pixels */
rowHeight: number;
}The callback provides:
height: New total height of the textarea in pixelsmeta.rowHeight: Height of a single text row, useful for calculating row countsOptional caching mechanism to improve performance when dealing with frequently changing content.
interface CachingOptions {
/** Cache CSS measurements to avoid repeated DOM queries */
cacheMeasurements?: boolean;
}When cacheMeasurements is true:
The component accepts all standard textarea styles except maxHeight and minHeight, which conflict with the row-based sizing system.
type Style = Omit<
NonNullable<React.TextareaHTMLAttributes<HTMLTextAreaElement>['style']>,
'maxHeight' | 'minHeight'
> & {
height?: number;
};Important Style Notes:
maxHeight and minHeight CSS properties are not allowed - use maxRows and minRows props insteadheight can be set as a number (interpreted as pixels) for initial sizingThe component performs validation in development mode and throws descriptive errors for common mistakes:
maxHeight style error: Thrown when style.maxHeight is provided - use maxRows prop insteadminHeight style error: Thrown when style.minHeight is provided - use minRows prop insteadThese errors only occur in development builds to help catch configuration issues early.
The component automatically detects the runtime environment: