2.0.9 • Published 10 years ago
textarea-autoresize v2.0.9
textarea-autoresize
This package provides a function, that, given textarea DOM Element and (optionally) a set of options,
calculates it's height and overflow-y for the size of it's content.

Key points
- Browser support: IE10+
- Written in pure Javscript(ES6+), framework-agnostic
- It DOES NOT use ghost elements to calculate the style, so there are no bugs due to the styles mismatch.
- There are helpful errors and warnings for non-production environment (sanity checks)
The calculation takes into account every possible combination of:
heightof the content inside the textarea(including placeholder)maxRows(if passed in options) andmax-heightin CSS(if present), whichever is smallerminRows(if passed in options),rowsattribute ontextareaelement(if present) andmin-heightin CSS(if present), whichever is biggerbox-sizingpadding-topandpadding-bottomborder-topandborder-bottom
Installation
npm install --save textarea-autoresizeUsage:
import { getTextareaStyle } from 'textarea-autoresize';
// Default options
const options = {
minRows: 0,
maxRows: Infinity,
canShrink: true
};
const { height, overflowY } = getTextareaStyle(textarea, options);
textarea.style.height = height;
textarea.style.overflowY = overflowY;Methods
getTextareaStyle(textarea[, options])
- Description:
- Calculates
heightandoverflow-yfortextareaelement to match the height of it's content, includingpaddingandborder.
- Calculates
Arguments:
- textarea:
- Type:
Element - Description: Textarea DOM Node
- Type:
- options (optional):
- Type:
Object - Properties:
- minRows (optional):
- Type:
Number - Description: minimum amount of rows allowed, expected to be an integer >= 0
- Default:
0
- Type:
- maxRows (optional):
- Type:
Number - Description: maximum amount of rows allowed, expected to be an integer >= minRows
- Default:
Infinity
- Type:
- canShrink (optional):
- Type
Boolean - Description: whether textarea is allowed to shrink in height(or only expand)
- Default:
true
- Type
- minRows (optional):
- Type:
- textarea:
Return value:
- Type:
Object - Description: new calculated style for textarea
- Properties:
- height:
- Type:
String - Description: new textarea
heightinpx, e.g.100px
- Type:
- overflowY:
- Type:
String - Description: new textarea
overflow-yvalue - One of:
hiddenauto- if calculated height is bigger, than maximum allowed height
- Type:
- height:
- Type: