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

expanding-textareas

v1.0.2

Published

An elegant approach to making textareas automatically grow.

Downloads

6,054

Readme

ExpandingTextareas

Build Status

An elegant approach to making textareas automatically grow. Based on Expanding Text Areas Made Elegant by Neil Jenkins.

Installation

Expanding can be installed via NPM, Bower, or by downloading the script located at dist/expanding.js. It can be required via CommonJS, AMD, or as a global (e.g. window.Expanding).

via npm:

npm install expanding-textareas
…
var Expanding = require('expanding-textareas')

via bower:

bower install expanding-textareas

The library is also available as a jQuery plugin (see below).

Usage

Expanding is a constructor which takes a textarea DOM node as its only argument:

var textarea = document.querySelector('textarea')
var expanding = new Expanding(textarea)

That's it! The textarea will now expand as the user types.

update

Updates the textarea height. This method is called automatically when the user types, but when setting the textarea content programmatically, it can be used to ensure the height expands as needed. For example:

var textarea = document.querySelector('textarea')
var expanding = new Expanding(textarea)

textarea.value = 'Hello\nworld!' // Height is not yet updated
expanding.update() // Height is now updated

refresh

Resets the styles of the internal elements to match those of the textarea. This may be useful if the textarea has percentage padding, and the browser window resizes, or if the textarea styles change after Expanding is called.

destroy

Removes the behavior. It unbinds the internal event listeners and removes the DOM nodes created by the library.

jQuery Plugin

Download the jQuery plugin located at dist/expanding.jquery.js, and include it in your page (after jQuery). For example:

<script src="http://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src='PATH/TO/expanding.js'></script>

Then, include the expanding class in any textarea you wish to add the behavior to:

<textarea class="expanding"></textarea>

The plugin will attach the behavior to every .expanding textarea when the DOM is ready.

Customizing the Initial Selector

To change the selector used for automatic initialization, modify $.expanding.initialSelector. For example:

$.expanding = {
  initialSelector: '[data-behavior=expanding]'
}

Disabling Automatic Initialization

To disable auto-initialization, set $.expanding.autoInitialize to false:

$.expanding = {
  autoInitialize: false
}

Manual Initialization

To manually initialize the plugin call expanding() on the jQuery selection. For example to apply the behavior to all textareas:

$('textarea').expanding()

Options

destroy

'destroy' will remove the behavior:

$('textarea').expanding('destroy')

active

'active' will check whether it has the expanding behavior applied:

$('textarea').expanding('active') // returns true or false

Note: this behaves like .hasClass(): it will return true if any of the nodes in the selection have the expanding behaviour.

refresh

'refresh' will update the styles (see above for more details):

$('textarea').expanding('refresh')

Caveats

Textareas must be visible for the library to function properly. The library creates a textarea clone with identical dimensions to that of the original. It therefore requires that the textarea be in place in the DOM for these dimensions to be correct.

Any styling applied to the target textarea will be maintained with the exception of margins and widths. (Margins are reset to 0 to ensure that the textarea maintains the correct size and positioning.)

After the expanding behavior has been applied, the textarea will appear like a block-level element: its width will expand to fill its container. To restrict the textarea width, apply a width declaration to a parent element. The library's wrapper (.expanding-wrapper) element may be useful in this case:

.expanding-wrapper {
   width: 50%;
}

Flash of unstyled content can be avoided by adding the following styles (adjust the selector as necessary):

textarea.expanding {
  margin: 0;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
  width: 100%;
}

Browser Support

The library aims to support modern versions of the following browsers: Chrome, Firefox, IE (9+), Opera, and Safari (incl. iOS). View the test suite to see if check if your browser is fully supported. (If there are no failures then you're good to go!)

Development & Testing

This library has been developed with ES2015 modules and bundled with Rollup. To get started with development, first clone the project:

git clone [email protected]:bgrins/ExpandingTextareas.git

Then navigate to the project and install the dependencies:

cd ExpandingTextareas
npm install

To bundle the source files:

npm run build

And finally to test:

npm test

Run the tests in a browser by opening test/index.html.