Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
react-autosize-textarea
Advanced tools
replacement for built-in textarea which auto resizes itself
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
);
npm install --save react-autosize-textarea
yarn add react-autosize-textarea
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 :)
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} />
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
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} />
);
}
}
Chrome | Firefox | IE | Safari | Android |
---|---|---|---|---|
Yes | Yes | 9+ | Yes | 4+ |
This module is based on the very popular autosize script written by Jack Moore.
FAQs
replacement for built-in textarea which auto resizes itself
The npm package react-autosize-textarea receives a total of 94,895 weekly downloads. As such, react-autosize-textarea popularity was classified as popular.
We found that react-autosize-textarea demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.