Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-textarea-autosize

Andarist8.8mMIT8.5.7TypeScript support: included

textarea component for React which grows with content

autosize, grow, react, react-component, textarea

readme

npm version npm

react-textarea-autosize

Drop-in replacement for the textarea component which automatically resizes textarea as content changes. A native React version of the popular jQuery Autosize! Weighs around 1.3KB (minified & gzipped).

This module supports IE9 and above.

import TextareaAutosize from 'react-textarea-autosize';

// If you use CommonJS syntax:
// var TextareaAutosize = require('react-textarea-autosize').default;

React.renderComponent(
  <div>
    <TextareaAutosize />
  </div>,
  document.getElementById('element'),
);

Install

npm install react-textarea-autosize

Demo

https://andarist.github.io/react-textarea-autosize/

Props

Special props:

prop type description
maxRows number Maximum number of rows up to which the textarea can grow
minRows number Minimum number of rows to show for textarea
onHeightChange func Function invoked on textarea height change, with height as first argument. The second function argument is an object containing additional information that might be useful for custom behaviors. Current options include { rowHeight: number }.
cacheMeasurements boolean Reuse previously computed measurements when computing height of textarea. Default: false

Apart from these, the component accepts all props that are accepted by <textarea/>, like style, onChange, value, etc.

FAQ

How to focus

Get a ref to inner textarea:

<TextareaAutosize ref={(tag) => (this.textarea = tag)} />

And then call a focus on that ref:

this.textarea.focus();

To autofocus:

<TextareaAutosize autoFocus />

(all HTML attributes are passed to inner textarea)

How to test it with jest and react-test-renderer if you need ref

Because jest provides polyfills for DOM objects by requiring jsdom and react-test-renderer doesn't provide refs for rendered components out of the box (calling ref callbacks with null), you need to supply a mocked ref in your tests in you need it for your tests. You can do it like this (more can be read here):

const tree = renderer
  .create(<TextareaAutosize />, {
    createNodeMock: () => document.createElement('textarea'),
  })
  .toJSON();

changelog

react-textarea-autosize

8.5.7

Patch Changes

  • #409 8c47e31 Thanks @Andarist! - Fixed an issue with resize not happening after the containing form being reset

8.5.6

Patch Changes

8.5.5

Patch Changes

  • #401 4a34e1b Thanks @olee! - Add missing wordSpacing and scrollbarGutter as properties that can impact sizing

8.5.4

Patch Changes

  • #397 bf3cad8 Thanks @Oyveloper! - Force display: block for the hidden textarea to prevent other styles from overriding it and thus breaking the resizing functionality

8.5.3

Patch Changes

  • #386 b3dc597 Thanks @Andarist! - Distribute completely separate files for the worker condition to avoid bundlers from aliasing files targeting node to the ones targeting browsers through the package.json#browser alias field.

8.5.2

Patch Changes

8.5.1

Patch Changes

  • #377 4087205 Thanks @Andarist! - The provided onChange will get forwarded again to the underlying <textarea/> on the server side.

8.5.0

Minor Changes

  • #373 05b014a Thanks @Andarist! - Compatibility with node's ESM has been improved. import TextareaAutosize from 'react-textarea-autosize'; was always meant to provide you the default export of this package (the exported component) and now node should load it this way.

  • #373 05b014a Thanks @Andarist! - SSR environments should now be able to pick smaller bundles through package.json#exports.

  • #373 05b014a Thanks @Andarist! - This package no longer depends on process.env.NODE_ENV. To get dev-only warnings you have to configure your bundler/runtime to use the development condition.

Patch Changes

8.4.1

Patch Changes

8.4.0

Minor Changes

  • #354 41d10b2 Thanks @Andarist! - exports field has been added to the package.json manifest.

    Thanks to this, the package now includes a worker condition that can be utilized by properly configured bundlers when targeting worker-like environments. It fixes the issue with browser-specific files being prioritized by some bundlers when targeting workers.

8.3.4

Patch Changes

8.3.3

Patch Changes

8.3.2

Patch Changes

  • 3c71884 #311 Thanks @Andarist! - Changed TextareaAutosizeProps to a TS interface which fixes the problem of "resolved" being type alias being inlined in the emitted types declaration which could cause incompatibilities with some versions of @types/react.

8.3.1

Patch Changes

  • 49d7d04 #305 Thanks @mxschmitt! - Moved internal 'resize' listener to the layout effect since React 17 calls cleanups of regular effects asynchronously. This ensures that we don't ever try to access the already unmounted ref in our listener.

8.3.0

Minor Changes

  • a16a46d #296 Thanks @RDIL! - Allow React 17 in the specified peer dependency range.

8.2.0

Minor Changes

  • a1fc99f #284 Thanks @emmenko! - Added { rowHeight: number } as a second parameter to the onHeightChange callback. This is useful to construct custom behaviors according to the height values.

8.1.1

Patch Changes

  • b7c227a #280 Thanks @emdotem! - Fixed a broken call to setProperty that has prevented the library to work correctly.

8.1.0

Minor Changes

  • 722e10a #278 Thanks @emdotem! - Set inline style's height property with the "important" priority.

Patch Changes

  • db872f0 Thanks @Andarist! - TextareaAutosizeProps are now based on React.TextareaHTMLAttributes<HTMLTextAreaElement> instead of JSX.IntrinsicElements['textarea']. The latter one includes a type for ref attribute and it being included as part of TextareaAutosizeProps has caused problems when using TextareaAutosizeProps to declare wrapper components. This is also more semantically correct as ref shouldn't be a part of props. It's rather accepted by a particular JSX element and in case of the react-textarea-autosize this is the type of the exported component which is React.ForwardRefExoticComponent<TextareaAutosizeProps> (a result of React.forwardRef call).

  • 61ca826 Thanks @Andarist! - maxHeight and minHeight has been disallowed as part of TextareaAutosizeProps['style']. The intention to do that was there since the v8 release but it was not implemented correctly and allowed those to slip into the mentioned type.

8.0.1

Patch Changes

  • 2307033 #266 Thanks @vlazh! - Fixed a regression with calculating too high height for textareas with box-sizing: border-box;.

  • 1d1bba2 #265 Thanks @SimenB! - Exported TextareaAutosizeProps type for convenience.

  • da960f4 Thanks @Andarist! - Fixed an issue with internal cache not being populated correctly when using cacheMeasurements prop.

8.0.0

Major Changes

  • The package has been rewritten in TypeScript so type definitions are now included in the package itself. There is no need to install separate types from the DefinitelyTyped.
  • At the same time the package internals have been rewritten to use React's hooks API. This means that the peer dependency requirement for React version had to be changed to ^16.8.0.
  • You can now use ref prop to get access to the underlaying textarea element as React.forwardRef is being used now. The support for innerRef has been completely removed.
  • useCacheForDOMMeasurements prop has been renamed to cacheMeasurements.
  • onHeightChange callback no longer receives the second argument. It was the component's instance (its this), but as the component is now implemented using hooks there no longer is any instance that could be given to a consumer like that.
  • Removed handling props.style.maxHeight and props.style.minHeight values. If you need to control those boundaries you should use maxRows and minRows props respectively.

Minor Changes

  • The height is being set now directly on the underlaying textarea element and not caused by updating internal state and this triggering React's rerender. This shouldn't make for any observable difference for consumers of this package.