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

flexfield

v1.0.0

Published

Plugin to automatically resize textarea elements to the size of the content.

Downloads

4

Readme

Flexfield

A standalone JavaScript plugin to automatically resize a textarea field as the user types.

Demo

Getting Started

Installing

Browser

Simply load the script on the page and initialise the plugin.

<textarea class="js-flexfield">foo</textarea>
<textarea class="js-flexfield">bar</textarea>

<script src="flexfield.min.js"></script>
<script>flexfield.init();</script>

npm

Alternatively, require Flexfield as part of a project and avoid polluting the global scope.

Install via npm:

npm i flexfield

Then require:

require('flexfield').init();

Styling

Recommended Styling

For smooth performance, the following styles are recommended:

.js-flexfield {
    overflow: hidden;
    vertical-align: top; /* Prevents a flicker on IE9 */
    resize: none; /* Makes little sense when field height is being set automatically */
}

Minimum Height

For a single-row textarea that resembles a generic input field, set line-height, min-height and height to be equal:

.js-flexfield {
    font-size: 16px;
    line-height: 18px;
    min-height: 18px;
    height: 18px;
}

The above example assumes that the box-sizing property is set to the default "content-box". For border-box examples, see demo.

Methods

flexfield.init(className)

Initialise the plugin.

This applies the event listeners to the document. An optional className can be supplied to target instead of the default "js-flexfield".

flexfield.trigger(element)

The single call to flexfield.init() already applies the necessary event listeners to resize all textareas on user interaction. However, when creating a new textarea element, or changing the value of a textarea dynamically with JavaScript, a resize must be triggered manually:

var elem = document.getElementById('main-input');
elem.value = 'Something else';
flexfield.trigger(elem);

Events

flexfield-resize

Fired when a field's height changes. Event.detail.change is a string value describing the type of change.

A jQuery example:

$(document).on('flexfield-resize', '.js-flexfield', function (e) {
    var change  = e.originalEvent.detail.change, // 'grow' or 'shrink'
        height  = $(this).height();

    alert(change + ' to ' + height + 'px'); // eg "grow to 45px"
});

Why?

Flexfield has a few advantages over similar plugins:

  • Simplicity
  • No dependencies
  • Makes uses of event delegation so it does not need to be initialised for every element. Because of this, textarea elements can be added or removed dynamically, without having to worry about memory leaks or calling .destroy() methods.
  • Textarea height is calculated from the element's scrollHeight property, so there is no need for a "shadow element", which can lead to glitches caused by mismatched styling.
  • Unobtrusive - the plugin only changes the textarea's height and overflow, allowing everything else to be defined with CSS.
  • Self-contained - no prototype pollution.

Compatability

Works in all modern browsers. IE9 and greater.

License

This project is licensed under the ISC License - see the LICENSE.md file for details