• Stars
    star
    399
  • Rank 108,092 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vertically adjust a textarea based on user input without using a clone or ghost element.

textarea-autosize

This package contains a TextareaAutoSize ES6 class for vertically adjusting a textarea based on user input and controlling any presentation in CSS. No clone or ghost elements are used. No jQuery dependency either, just plain vanilla JS. πŸ˜‰

This was a fun project I started in Aug 2012 as a jQuery plugin. Nowadays, the same hack can be implemented using vanilla JS with such little code that I don't recommend using this package. Instead just implement the component code in your project so it can be modified to your requirements.

Installation

NPM

npm install textarea-autosize

Usage

Import the script and find the desired textarea elements on which to initialize the component with.

<textarea class="js-auto-size" rows="1"></textarea>

<script type="module">
  import { TextareaAutoSize } from './textarea-autosize'
  const wrapper = new TextareaAutoSize(document.querySelector('textarea.js-auto-size'))
</script>

Minimum CSS Requirements

In the following example, the minimum height is one line of text which is determined from the inherited font size, line height, and vertical padding. So if your font-size is 16px, line-height is 1.5, top/bottom padding is 6px each, then the minimum height would be 36px since 16 * 1.5 + 12 = 36.

textarea {
  box-sizing: border-box;
  max-height: 160px; /* optional but recommended */
  min-height: 38px;
}

Increase the min-height to have more initial rows. Once text exceeds that minimum height the textarea will expand naturally until it reaches the maximum height if declared.

Updating Textarea Content

If you pre-fill the textarea before page load the textarea will adjust to fit automatically but if given a value after page load (e.g. single page app) then you will need to trigger an update on the textarea after setting its value for it to size correctly.

wrapper.update()

Cleaning Up

Calling the destroy method will remove the event handler and release the element reference.

wrapper.destroy()

Development

  1. Install Node.js v18+
  2. npm install
  3. npx playwright install
  4. npm test
  5. npm run start

Deploy New Release

Replace {type} with any of the following: patch, minor or major

npm version {type}
git push origin --tags
npm run predeploy && npm publish

Update GitHub Project Page

Run npm run deploy to release on GitHub Pages using gh-pages package.

Resources

More Repositories

1

ruby-koans-completed

These are my completed tests with in depth notes as I'm learning Ruby along the way.
Ruby
56
star
2

dictionary

A Ruby parser for the GCIDE English word dictionary that generates friendly structured JSON files for easy mass database import. Includes other resources if you need more data for an English dictionary database.
XSLT
22
star
3

humble-bundle-key-exporter

A bookmarklet to export a list of your unredeemed Humble Bundle keys into a downloaded formatted JSON file.
JavaScript
18
star
4

learning-ruby-the-hard-way

Completing all the exercises in the online book Learning Ruby The Hard Way.
Ruby
10
star
5

ember-cli-todomvc

Port of Ember.js TodoMVC using ember-cli
JavaScript
6
star
6

dotfiles

DEPRECATED - now part of laptop script in the javierjulio/laptop repo
Ruby
6
star
7

ruby-notes

A collection of Ruby scripts with notes that I've written to learn the language.
Ruby
5
star
8

python-koans-completed

Python
5
star
9

laptop

Shell script to setup my Mac with dotfiles and apps πŸ’»
Shell
4
star
10

tweet-list

A configurable jQuery plugin that displays a formatted list of tweets with customizable HTML output.
JavaScript
2
star
11

website

My website built with Serious (Ruby blog), backed up in Git and deployed to Heroku.
HTML
2
star
12

binlist-elixir

An Elixir client library for the Binlist.net service.
Elixir
2
star
13

now-or-later

A sample To-Dos application to learn how to use Backbone.js and CoffeeScript.
JavaScript
2
star
14

foragers

A simple app that takes a list of places from each of your Foursquare friends and displays them in a Google Map.
2
star
15

jjsparklib

A collection of skinnable Flex 4 (Spark) components. To download the compiled SWC, you can find it in the "bin" folder. Just click the SWC link and then click "View Raw" which will download it.
ActionScript
2
star
16

nonograms-game

A game of endless, randomly generated but solvable Nonogram puzzles. No timers or scores.
JavaScript
2
star
17

javierjulio.github.io

Personal site built in Jekyll and Github Pages
HTML
2
star
18

node-hello-world

An example Node app written in CoffeeScript using Express, Jade and Stylus to learn Node.
CoffeeScript
2
star
19

object-factory

The ObjectFactory is a single, 200-300 line ColdFusion component to help manage your object relationships. Dependencies are defined in XML and allow for unlimited nested elements. You can define relations to other objects in your XML config or specify any default values such as strings, numbers, structs, arrays, etc.
ColdFusion
2
star
20

tetris

A pure AS3, open source Tetris game
JavaScript
1
star
21

featured-carousel

A jQuery plugin that retrieves subsequent content using AJAX rather than having the content inline.
CSS
1
star
22

iphone-bootcamp

These are the projects I completed for the NYC iPhone Bootcamp in June 2010. I learned to develop for iPhone and iPad. An additional project called NotesTest is something I started on my own to build a note manager app.
Objective-C
1
star
23

synapse_payments

DEPRECATED - A Ruby 2.1+ wrapper for SynapsePay v3 API with VCR and integration tests.
Ruby
1
star
24

things-for-rails

A sample todo app to learn Rails from starting from scratch. Based on my favorite todo app Things.
Ruby
1
star
25

clojure-test

A collection of simple apps/scripts I've written to help me learn programming in Clojure and using Leiningen to build, compile and run these apps locally.
Clojure
1
star