|
| 1 | +<p align="center"><img src="img/social_banner.jpg"></p> |
| 2 | +<h1 align="center">Auto-expandable Textarea for Javascript</h1> |
| 3 | +<p align="center">Auto-expandable Textarea snippet for HTML textarea fields built with native javascript</p> |
| 4 | +<p align="center"> |
| 5 | + <img src="https://img.shields.io/david/giray123/javascript-autoexpandable-textarea?style=for-the-badge" /> |
| 6 | + <img src="https://img.shields.io/github/size/giray123/javascript-autoexpandable-textarea/js/textarea-expander.js?label=JS&logo=javascript&style=for-the-badge" /> |
| 7 | + <a href="https://github.com/giray123/javascript-autoexpandable-textarea/blob/master/LICENSE"><img src="https://img.shields.io/github/license/giray123/javascript-autoexpandable-textarea?style=for-the-badge" /></a> |
| 8 | +</p> |
| 9 | + |
| 10 | +<p align="center"><img src="img/expand_both_ways.gif"></p> |
| 11 | + |
| 12 | +This snippet: |
| 13 | +- :fire: is built with **native javascript** |
| 14 | +- :package: requires **no dependencies** |
| 15 | +- :hammer_and_pick: highly **customizable** yet very **simple** |
| 16 | + |
| 17 | +## Demo |
| 18 | +- [JSfiddle](https://jsfiddle.net/giray123/y0wh7xmL/12/) |
| 19 | +- [Github Pages](https://giray123.github.io/javascript-autocomplete/) |
| 20 | +## Usage |
| 21 | + |
| 22 | +Add either unminified or minified JS onto your html |
| 23 | +```html |
| 24 | +<script src="/js/textarea-expander.js"></script> |
| 25 | +<!-- minififed --> |
| 26 | +<script src="/js/textarea-expander.min.js"></script> |
| 27 | +``` |
| 28 | + |
| 29 | +### CDN |
| 30 | +You can also use below CDN links. Feel free to change version number with respect to the releases |
| 31 | +```html |
| 32 | +< script src= "https://cdn.jsdelivr.net/gh/giray123/[email protected]/js/textarea-expander.js"></ script> |
| 33 | +<!-- minififed --> |
| 34 | +< script src= "https://cdn.jsdelivr.net/gh/giray123/[email protected]/js/textarea-expander.min.js"></ script> |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +Initialize Autocomplete object with your configurations |
| 39 | +### Auto-expandable Textarea Both Vertically and Horizontally |
| 40 | +<p align="center"><img src="img/expand_both_ways.gif" style="margin: 20px;"></p> |
| 41 | + |
| 42 | +```js |
| 43 | +var expander1 = new textAreaAutoExpander('#textarea_expand_both_ways') |
| 44 | +``` |
| 45 | +### Auto-expandable Textarea Vertically |
| 46 | +<p align="center"><img src="img/expand_vertically.gif" style="margin: 20px;"></p> |
| 47 | + |
| 48 | +```js |
| 49 | +var expander2 = new textAreaAutoExpander({ |
| 50 | + selector: '#textarea_expand_vertically', |
| 51 | + safetyMargin: 200, |
| 52 | + autoHeight: true, |
| 53 | + autoWidth: false |
| 54 | +}) |
| 55 | +``` |
| 56 | +### Auto-expandable Textarea Horizontally |
| 57 | +<p align="center"><img src="img/expand_horizontally.gif" style="margin: 20px;"></p> |
| 58 | + |
| 59 | +```js |
| 60 | +var expander3 = new textAreaAutoExpander({ |
| 61 | + selector: '#textarea_expand_horizontally', |
| 62 | + safetyMargin: 200, |
| 63 | + autoHeight: false, |
| 64 | + autoWidth: true |
| 65 | +}) |
| 66 | +``` |
| 67 | +## How it Works? |
| 68 | +Auto expanding a textarea based on user input is a tedious task. However, there is a workaround which this snippet is using. When you initialize the snippet, it creates a copy div element of the textarea which is not possible, it then syncs all the related CSS attributes so that their width and height changes the same amount. When user types, it calculates the width/height of the hidden element and copies it to the textarea. Because of this dynamic, you can not style your textarea during expansion and expect it to continue expanding properly. You need to call `expander.refresh()` after you style your textarea so that the snippet syncs CSS parameters again. |
| 69 | + |
| 70 | +## Configuration |
| 71 | +### Global Options |
| 72 | +| attribute | type | default | description | |
| 73 | +| ------------- | -------- | ---- | ------------- | |
| 74 | +| `selector` | string | required | html input element query selector (`document.querySelector(selector)`) |
| 75 | +| `safetyMargin` | integer | `100` | minimum distance between text and the border before expanding starts |
| 76 | +| `autoHeight` | boolean | `true` | whether to expand vertically |
| 77 | +| `autoWidth` | boolean | `true` | whether to expand horizontally |
| 78 | +## Methods |
| 79 | +| attribute | description | |
| 80 | +| -----------| ----------- | |
| 81 | +| `state()` | returns information about the current state |
| 82 | +| `refresh()`| syncs fontFamily, whiteSpace, fontSize, lineHeight between the textarea and the hidden element. You can `expander.refresh({fontSize: 20, lineHeight: 20})` to change fontSize and lineHeight directly |
| 83 | + |
| 84 | + |
| 85 | +## LICENSE |
| 86 | +This project is [licensed]("https://github.com/giray123/javascript-autoexpandable-textarea/blob/master/LICENSE") under the terms of the MIT license. |
0 commit comments