npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-autosize-textarea

v7.1.0

Published

replacement for built-in textarea which auto resizes itself

Downloads

585,125

Readme

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:

|Name|Type|Default|Description| |----|----|-------|-----------| | onResize | Function | | optional. Called whenever the textarea resizes | | rows | Number | | optional. Minimum number of visible rows | | maxRows | Number | | optional. Maximum number of visible rows | | async | Boolean | false | optional. 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. | ref | Function | | optional. 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

| Chrome | Firefox | IE | Safari | Android | | ------------- | ------------- | ----- | ------ | ------- | | Yes | Yes | 9+ | Yes | 4+ |

Credits

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

Check out his repo or website for more documentation.