Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yuanqing/autosize-input
:balloon: Effortless, dynamic-width text boxes in vanilla JavaScript
https://github.com/yuanqing/autosize-input
autosize dom forms input javascript textbox
Last synced: 5 days ago
JSON representation
:balloon: Effortless, dynamic-width text boxes in vanilla JavaScript
- Host: GitHub
- URL: https://github.com/yuanqing/autosize-input
- Owner: yuanqing
- License: mit
- Created: 2015-06-14T11:46:50.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2024-06-23T13:47:07.000Z (7 months ago)
- Last Synced: 2024-12-25T13:06:20.624Z (13 days ago)
- Topics: autosize, dom, forms, input, javascript, textbox
- Language: JavaScript
- Homepage: https://codepen.io/lyuanqing/pen/xYpmKj
- Size: 104 KB
- Stars: 68
- Watchers: 1
- Forks: 12
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# autosize-input [![npm Version](http://img.shields.io/npm/v/autosize-input.svg?style=flat)](https://www.npmjs.org/package/autosize-input) [![Build Status](https://img.shields.io/travis/yuanqing/autosize-input.svg?branch=master&style=flat)](https://travis-ci.org/yuanqing/autosize-input)
> Effortless, dynamic-width text boxes in vanilla JavaScript.
- Dynamically adjusts the width of the text box to fit its current contents
- Can be initialised to fit its `placeholder` attribute
- Optionally set a `min-width` based on the element’s initial content
- 718 bytes gzipped## Usage
> [**Editable demo (CodePen)**](https://codepen.io/lyuanqing/pen/xYpmKj)
```html
```
```js
const autosizeInput = require('autosize-input')autosizeInput(document.querySelector('#foo'))
autosizeInput(document.querySelector('#bar'))
autosizeInput(document.querySelector('#baz'), { minWidth: true })
```## API
```js
const autosizeInput = require('autosize-input')
```### autosizeInput(element [, options])
`element` is a text `input` element, and `options` is an object literal.
- Returns a “clean up” function for removing the event listener on the `element`.
- If we do not want the text box to “contract” as the user starts to type, set `options.minWidth` to `true`. This will give the `element` a `min-width` that fits it initial contents (ie. either the element’s intial `value`, or its `placeholder`).See [Usage](#usage).
## Implementation details
- A hidden “ghost” `div` element, assigned the same styles as the text box, is used to calculate the correct width to assign to the text box. This width is recomputed and assigned to the text box on every [`input`](https://developer.mozilla.org/en-US/docs/Web/Events/input) event.
- The single “ghost” `div` is shared amongst all the “autosized” text boxes on the page.## Installation
Install via [yarn](https://yarnpkg.com):
```sh
$ yarn add autosize-input
```Or [npm](https://npmjs.com):
```sh
$ npm install --save autosize-input
```## Tests
To test manually, in the browser:
```sh
$ yarn start
```To run the programmatic tests:
```sh
$ yarn test
```## Prior art
- [jQuery.Autosize.Input](https://github.com/MartinF/jQuery.Autosize.Input)
- [React-Input-Autosize](https://github.com/JedWatson/react-input-autosize)This module was written because I needed a standalone, lightweight solution to this rather UI problem.
## License
[MIT](LICENSE.md)