7.1.0 • Published 4 years ago

react-autosize-textarea v7.1.0

Weekly downloads
147,998
License
MIT
Repository
github
Last release
4 years ago

React Autosize Textarea

A light replacement for built-in <textarea /> component which automatically adjusts its height to match the content.

NB: It does not require any polyfill

import ReactDOM from 'react-dom';
import TextareaAutosize from 'react-autosize-textarea';

ReactDOM.renderComponent(
  <TextareaAutosize {...textareaProps} onResize={(e) => {}} />,
  document.body
);

Install

npm install --save react-autosize-textarea
yarn add react-autosize-textarea

Demo

Live Examples

Usage

TextareaAutosize is perfectly compatible with ReactJS default one, so to get started you can simply replace any <textarea></textarea> with <TextareaAutosize></TextareaAutosize>. It's that easy :)

Props

You can pass any prop you're allowed to use with the default React textarea (valueLink too).

In addition to them, TextareaAutosize comes with some optional custom props to facilitate its usage:

NameTypeDefaultDescription
onResizeFunctionoptional. Called whenever the textarea resizes
rowsNumberoptional. Minimum number of visible rows
maxRowsNumberoptional. Maximum number of visible rows
asyncBooleanfalseoptional. Initialize autosize asynchronously. Enable it if you are using StyledComponents. This is forced to true when maxRows is set. Async initialization will make your page "jump" when the component appears, as it will first be rendered with the normal size, then resized to content.
refFunctionoptional. Ref to the DOM node

onResize

Sometimes you may need to react any time TextareaAutosize resizes itself. To do so, you should use the optional callback onResize which will be triggered at any resize with the autosize:resized event object:

function onResize(event) {
  console.log(event.type); // -> "autosize:resized"
}

<TextareaAutosize onResize={onResize} />

Set min/max height

You can set minHeight and maxHeight through CSS or inline-style as usual:

<TextareaAutosize style={{ minHeight: 20, maxHeight: 80 }} /> // min-height: 20px; max-height: 80px;

NB: you may need to take into account borders and/or padding.

In addition to minHeight, you can force TextareaAutosize to have a minimum number of rows by passing the prop rows:

<TextareaAutosize rows={3} /> // minimun height is three rows

In addition to maxHeight, you can force TextareaAutosize to have a maximum number of rows by passing the prop maxRows:

<TextareaAutosize maxRows={3} /> // maximum height is three rows

Refs to DOM nodes

In order to manually call textarea's DOM element functions like focus() or blur(), you need a ref to the DOM node.

You get one by using the prop ref as shown in the example below:

class Form extends React.Component {
  constructor() {
    this.textarea = React.createRef()
  }
  
  componentDidMount() {
    this.textarea.current.focus();
  }

  render() {
    return (
      <TextareaAutosize ref={this.textarea} />
    );
  }
}

Browser Compatibility

ChromeFirefoxIESafariAndroid
YesYes9+Yes4+

Credits

This module is based on the very popular autosize script written by Jack Moore.

Check out his repo or website for more documentation.

@theia/search-in-workspacezaro-webskilvul-web-componentscl2-frontrete-two-editortwintwoo-editorbloben-reactstadtenergiereact-approveme-ui@infinitebrahmanuniverse/nolb-react-au@twigeducation/ts-fe-components@everything-registry/sub-chunk-2541@oqton/design-system@codeleap/webdg-form-fieldscontentco-componentsdraekien-uiprimeobjects-ui-web-applicationprimeobjects-ui-web-form@infotech/uikit-textareaeehitus-components@hektodaleky/bandpaycomponents@httpeacejs/react-page-editor@g123jp/g123-internal-uirich-markdown-editor3@groovehq/design-system@eventgenius/ui-libepanelrich-maple-markdownrich-markdown-editor-frich-markdown-editor-knfbk-components@interdan/react-localized-selfloading-texts@interdan/react-localized-texts@intellihr/blueberry@intellihr/ui-components@igors-wix/rich-markdown-editorfacaade@inusha1234/edit-sitereact-in-out-textareareact-marky-markdownreact-markerreact-simpl-formreact-simpl-form-fields@finadvise/formsreact-select-inputreact-select-input-v2react-sm-components@gagrigoryanedu/kit@gechiui/editor@gechiui/block-editor@garbin/rich-markdown-editor@golocalinteractive/storage-essentials-components@golpasal/editor@gpsgate/react-componentselegant-editorresources-coehr-componentsehr-my-views-uidutchexchange-abikovandutchexchange-smartcontractsrebulreact-bs-componentsreact-dinamic-contentreact-localized-texts@envoy/polarwind@pitagora/samos@prisma/prisma-adminupstra-uikit@radargovernamental/orbit-style@quillforms/block-editor@quillforms/blocklib-long-text-block@real-system/textareasimplitime-communicationgraveflex-ui-webtaro-inktailor-uisynapsefi-chatbot-ui@mikekreeki/rich-markdown-editor@modelo.io/apiui@nightw0lff/react-native-mentionshis4_componentshomeanswer2@sheets-editor/plugin-equationwaivauth-sdk-reactui-components-master@phongsakron/ui-kit@resources/console@remax/one@ray-core/oneis-ui-library@recogito/recogito-client-coretrollbox-pro@opensumi/ide-core-browsertoken-place-landing@o/editortestepaneltestanswerhenrybuilt-react-librarystorwork-stores
7.1.0

4 years ago

7.0.0

5 years ago

6.0.0

5 years ago

5.0.1

5 years ago

5.0.0

6 years ago

5.0.0-pre

6 years ago

4.0.0

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.4.9

7 years ago

0.4.8

7 years ago

0.4.7

7 years ago

0.4.6

7 years ago

0.4.5

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.6

8 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago