Greetings! The nights are drawing in pretty quickly here, and it’s also additionally pretty dark in the morning due to the mountain I live next to and the added darkness is making it pretty difficult to maintain my early morning routines. A short and sweet post this week about how to extend the built-in Ember.js Textarea View so that it auto expands to contain the text inside it, but first a short interlude into a recent poll I ran on Ember.js usage:

The results can be seen here: http://strawpoll.me/2695391/r. Admittedly, it’s an extremely small sample size, but I was shocked to see Rails in the minority. It’s good to see Ember-CLI leading the pack, as it’s a great project – and the results overall make me feel more comfortable about writing more Ember-CLI related posts in the near future. On to today’s post…

When we need a Textarea in one of our Templates, we can simply use the textarea helper like so:

And that will render a basic text area bound to the notes property. But it will be a static textarea that does not accommodate content that is larger than its style will permit without scroll bars. What if we wanted the textarea to automatically grow vertically as more text is entered, without resorting to any external jQuery plugins?

First of all, we create a new Component that extends the Ember TextArea:

1 App.AutoExpandingTextAreaComponent = Ember.TextArea.extend({
2   ... snip ...
3 });

If we just left the new AutoExpandingTextAreaComponent as is above, we could use it in any template with `` and its behaviour would be exactly the same as the default textarea. So far, so obvious.

The trick here for us, is that we want the textarea to be aware of any keypresses, and change its size accordingly. We’ll use the didInsertElement hook to attach an event listener for keypresses on the text area:

 1 App.AutoExpandingTextAreaComponent = Ember.TextArea.extend({
 2   didInsertElement: function(){
 3 
 4     Ember.run.next(function() {
 5 
 6       // Focus the text area
 7       this.$().focus();
 8 
 9       // Listen for keypress events and recalculate the height of the text area.
10       this.$().on('keypress', function(e) {
11         var textArea = $(this);
12         var newHeight = this.scrollHeight + parseFloat(textArea.css("borderTopWidth")) + parseFloat(textArea.css("borderBottomWidth"));
13 
14         while(textArea.outerHeight() < newHeight) {
15           $(this).height($(this).height()+1);
16         }
17      });
18 
19     }.bind(this));
20 
21   } // didInsertElement
22 });

We’re careful to make sure that the jQuery event listener runs inside an Ember.run.next in order to make sure it happens after Ember has propagated all property updates and settled it’s internal book-keeping on the Runloop.

Lastly, we want to make sure we tear down the event listener when the element is destroyed, by using the willDestroyElement hook:

 1 App.AutoExpandingTextAreaComponent = Ember.TextArea.extend({
 2   didInsertElement: function(){
 3     Ember.run.next(function() {
 4       this.$().focus();
 5       this.$().on('keypress', function(e) {
 6         var textArea = $(this);
 7         var newHeight = this.scrollHeight + parseFloat(textArea.css("borderTopWidth")) + parseFloat(textArea.css("borderBottomWidth"));
 8 
 9         while(textArea.outerHeight() < newHeight) {
10           $(this).height($(this).height()+1);
11         }
12      });
13     }.bind(this));
14   },
15 
16   willDestroyElement: function() {
17     this.$().off('keypress');
18   }
19 });

Here’s a working example:

Ember Starter Kit

Summary

Ember makes it pretty easy to extend the primitives it ships with, using the built in lifecycle hooks. In this example we took the stock Ember.TextArea view and extended it as a new component that has new behaviour on its appearance.