forked from LeaVerou/stretchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stretchy.js
219 lines (174 loc) · 5.42 KB
/
stretchy.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Stretchy: Form element autosizing, the way it should be.
* by Lea Verou http://lea.verou.me
* MIT license
*/
(function() {
if (!self.Element) {
return; // super old browser
}
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.webkitMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector || null;
}
if (!Element.prototype.matches) {
return;
}
function $$(expr, con) {
return expr instanceof Node || expr instanceof Window? [expr] :
[].slice.call(typeof expr == "string"? (con || document).querySelectorAll(expr) : expr || []);
}
var _ = self.Stretchy = {
selectors: {
base: 'textarea, select:not([size]), input:not([type]), input[type="' + "text number url email tel".split(" ").join('"], input[type="') + '"]',
filter: "*"
},
// Script element this was included with, if any
script: document.currentScript || $$("script").pop(),
// Autosize one element. The core of Stretchy.
resize: function(element) {
if (!_.resizes(element)) {
return;
}
var cs = getComputedStyle(element);
var offset = 0;
var empty;
if (!element.value && element.placeholder) {
empty = true;
element.value = element.placeholder;
}
var type = element.nodeName.toLowerCase();
if (type == "textarea") {
element.style.height = "0";
if (cs.boxSizing == "border-box") {
offset = element.offsetHeight;
}
else if (cs.boxSizing == "content-box") {
offset = -element.clientHeight + parseFloat(cs.minHeight);
}
element.style.height = element.scrollHeight + offset + "px";
}
else if (type == "input") {
// First test that it is actually visible, otherwise all measurements are off
element.style.width = "1000px";
if (element.offsetWidth) {
element.style.width = "0";
if (cs.boxSizing == "border-box") {
offset = element.offsetWidth;
}
else if (cs.boxSizing == "padding-box") {
offset = element.clientWidth;
}
else if (cs.boxSizing == "content-box") {
offset = parseFloat(cs.minWidth);
}
var width = Math.max(offset, element.scrollWidth - element.clientWidth);
element.style.width = width + "px";
// To bulletproof, we will set scrollLeft to a
// huge number, and read that back to see what it was clipped to
// and increment width by that much, iteratively
for (var i=0; i<10; i++) { // max iterations
element.scrollLeft = 1e+10;
if (element.scrollLeft == 0) {
break;
}
width += element.scrollLeft;
element.style.width = width + "px";
}
}
else {
// Element is invisible, just set to something reasonable
element.style.width = element.value.length + 1 + "ch";
}
}
else if (type == "select") {
var selectedIndex = element.selectedIndex > 0? element.selectedIndex : 0;
// Need to use dummy element to measure :(
var option = document.createElement("_");
option.textContent = element.options[selectedIndex].textContent;
element.parentNode.insertBefore(option, element.nextSibling);
// The name of the appearance property, as it might be prefixed
var appearance;
for (var property in cs) {
var value = cs[property];
if (!/^(width|webkitLogicalWidth|length)$/.test(property) && typeof value == "string") {
option.style[property] = value;
if (/appearance$/i.test(property)) {
appearance = property;
}
}
}
option.style.width = "";
if (option.offsetWidth > 0) {
element.style.width = option.offsetWidth + "px";
if (!cs[appearance] || cs[appearance] !== "none") {
// Account for arrow
element.style.width = "calc(" + element.style.width + " + 2em)";
}
}
option.parentNode.removeChild(option);
option = null;
}
if (empty) {
element.value = "";
}
},
// Autosize multiple elements
resizeAll: function(elements) {
$$(elements || _.selectors.base).forEach(function (element) {
_.resize(element);
});
},
active: true,
// Will stretchy do anything for this element?
resizes: function(element) {
return element &&
element.parentNode &&
element.matches &&
element.matches(_.selectors.base) &&
element.matches(_.selectors.filter);
},
init: function(){
_.selectors.filter = _.script.getAttribute("data-filter") ||
($$("[data-stretchy-filter]").pop() || document.body).getAttribute("data-stretchy-filter") || _.selectors.filter;
_.resizeAll();
// Listen for new elements
if (self.MutationObserver && !_.observer) {
_.observer = new MutationObserver(function(mutations) {
if (_.active) {
mutations.forEach(function(mutation) {
if (mutation.type == "childList") {
_.resizeAll(mutation.addedNodes);
}
});
}
});
_.observer.observe(document.documentElement, {
childList: true,
subtree: true
});
}
},
$$: $$
};
// Autosize all elements once the DOM is loaded
// DOM already loaded?
if (document.readyState !== "loading") {
requestAnimationFrame(_.init);
}
else {
// Wait for it
document.addEventListener("DOMContentLoaded", _.init);
}
window.addEventListener("load", function(){
_.resizeAll();
});
// Listen for changes
var listener = function(evt) {
if (_.active) {
_.resize(evt.target);
}
};
document.documentElement.addEventListener("input", listener);
// Firefox fires a change event instead of an input event
document.documentElement.addEventListener("change", listener);
})();