2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
11 `paper-autogrow-textarea` is an element containing a textarea that grows in height as more
12 lines of input are entered. Unless an explicit height or the `maxRows` property is set, it will
17 <paper-autogrow-textarea id="a1">
18 <textarea id="t1"></textarea>
19 <paper-autogrow-textarea>
21 Because the `textarea`'s `value` property is not observable, if you set the `value` imperatively
22 you must call `update` to notify this element the value has changed.
26 /* using example HTML above */
27 t1.value = 'some\ntext';
31 @element paper-autogrow-textarea
35 <link href=
"../polymer/polymer.html" rel=
"import">
37 <polymer-element name=
"paper-autogrow-textarea" on-input=
"{{inputAction}}">
42 display: inline-block;
57 /* see comments in template */
62 ::content textarea:invalid {
67 <!-- the mirror sizes the input/textarea so it grows with typing -->
68 <div id=
"mirror" class=
"mirror-text" aria-hidden=
"true"> </div>
70 <!-- size the input/textarea with a div, because the textarea has intrinsic size in ff -->
71 <div class=
"textarea-container" fit
>
83 * The textarea that should auto grow.
86 * @type HTMLTextAreaElement
92 * The initial number of rows.
101 * The maximum number of rows this element can grow to until it
102 * scrolls. 0 means no maximum.
114 rows
: 'updateCached',
115 maxRows
: 'updateCached'
118 constrain: function(tokens
) {
120 tokens
= tokens
|| [''];
121 // Enforce the min and max heights for a multiline input to avoid measurement
122 if (this.maxRows
> 0 && tokens
.length
> this.maxRows
) {
123 _tokens
= tokens
.slice(0, this.maxRows
);
125 _tokens
= tokens
.slice(0);
127 while (this.rows
> 0 && _tokens
.length
< this.rows
) {
130 return _tokens
.join('<br>') + ' ';
133 valueForMirror: function(input
) {
134 this.tokens
= (input
&& input
.value
) ? input
.value
.replace(/&/gm, '&').replace(/"/gm, '"').replace(/'/gm, ''').replace(/</gm, '<').replace(/>/gm, '>').split('\n') : [''];
135 return this.constrain(this.tokens);
139 * Sizes this element to fit the input value. This function is automatically called
140 * when the user types in new input, but you must call this function if the value
141 * is updated imperatively.
144 * @param Element The input
146 update: function(input) {
147 this.$.mirror.innerHTML = this.valueForMirror(input);
150 updateCached: function() {
151 this.$.mirror.innerHTML = this.constrain(this.tokens);
154 inputAction: function(e) {
155 this.update(e.target);