forked from googlearchive/paper-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-autogrow-textarea.html
162 lines (132 loc) · 4.08 KB
/
paper-autogrow-textarea.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
The complete set of authors may be found at http://polymer.github.io/AUTHORS
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
-->
<!--
`paper-autogrow-textarea` is an element containing a textarea that grows in height as more
lines of input are entered. Unless an explicit height or the `maxRows` property is set, it will
never scroll.
Example:
<paper-autogrow-textarea id="a1">
<textarea id="t1"></textarea>
<paper-autogrow-textarea>
Because the `textarea`'s `value` property is not observable, if you set the `value` imperatively
you must call `update` to notify this element the value has changed.
Example:
/* using example HTML above */
t1.value = 'some\ntext';
a1.update();
@group Paper Elements
@element paper-autogrow-textarea
@status unstable
-->
<link href="../polymer/polymer.html" rel="import">
<polymer-element name="paper-autogrow-textarea" on-input="{{inputAction}}">
<template>
<style>
:host {
display: inline-block;
position: relative;
width: 400px;
}
.mirror-text {
visibility: hidden;
word-wrap: break-word;
}
::content textarea {
padding: 0;
margin: 0;
border: none;
outline: none;
resize: none;
/* see comments in template */
width: 100%;
height: 100%;
}
::content textarea:invalid {
box-shadow: none;
}
</style>
<!-- the mirror sizes the input/textarea so it grows with typing -->
<div id="mirror" class="mirror-text" aria-hidden="true"> </div>
<!-- size the input/textarea with a div, because the textarea has intrinsic size in ff -->
<div class="textarea-container" fit>
<content></content>
</div>
</template>
<script>
Polymer({
publish: {
/**
* The textarea that should auto grow.
*
* @attribute target
* @type HTMLTextAreaElement
* @default null
*/
target: null,
/**
* The initial number of rows.
*
* @attribute rows
* @type number
* @default 1
*/
rows: 1,
/**
* The maximum number of rows this element can grow to until it
* scrolls. 0 means no maximum.
*
* @attribute maxRows
* @type number
* @default 0
*/
maxRows: 0
},
tokens: null,
observe: {
rows: 'updateCached',
maxRows: 'updateCached'
},
constrain: function(tokens) {
var _tokens;
tokens = tokens || [''];
// Enforce the min and max heights for a multiline input to avoid measurement
if (this.maxRows > 0 && tokens.length > this.maxRows) {
_tokens = tokens.slice(0, this.maxRows);
} else {
_tokens = tokens.slice(0);
}
while (this.rows > 0 && _tokens.length < this.rows) {
_tokens.push('');
}
return _tokens.join('<br>') + ' ';
},
valueForMirror: function(input) {
this.tokens = (input && input.value) ? input.value.replace(/&/gm, '&').replace(/"/gm, '"').replace(/'/gm, ''').replace(/</gm, '<').replace(/>/gm, '>').split('\n') : [''];
return this.constrain(this.tokens);
},
/**
* Sizes this element to fit the input value. This function is automatically called
* when the user types in new input, but you must call this function if the value
* is updated imperatively.
*
* @method update
* @param Element The input
*/
update: function(input) {
this.$.mirror.innerHTML = this.valueForMirror(input);
},
updateCached: function() {
this.$.mirror.innerHTML = this.constrain(this.tokens);
},
inputAction: function(e) {
this.update(e.target);
}
});
</script>
</polymer-element>