forked from LeaVerou/stretchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stretchy.js
187 lines (149 loc) · 4.52 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
/*
* 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 url email tel".split(" ").join('"], input[type="') + '"]',
filter: "*"
},
// Script element this was included with, if any
script: $$("script").pop(),
// Autosize one element. The core of Stretchy.
resize: function(element) {
if (!_.resizes(element)) {
return;
}
var cs = getComputedStyle(element);
var offset = 0;
if (!element.value && element.placeholder) {
var 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;
}
element.style.height = element.scrollHeight + offset + "px";
}
else if(type == "input") {
element.style.width = "0";
if (cs.boxSizing == "border-box") {
offset = element.offsetWidth;
}
else if (cs.boxSizing == "padding-box") {
offset = element.clientWidth;
}
// Safari misreports scrollWidth, so we will instead set scrollLeft to a
// huge number, and read that back to see what it was clipped to
element.scrollLeft = 1e+10;
var width = Math.max(element.scrollLeft + offset, element.scrollWidth - element.clientWidth);
element.style.width = width + "px";
}
else if (type == "select") {
// Need to use dummy element to measure :(
var option = document.createElement("_");
option.textContent = element.options[element.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) {
if (!/^(width|webkitLogicalWidth)$/.test(property)) {
//console.log(property, option.offsetWidth, cs[property]);
option.style[property] = cs[property];
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") || "*";
_.resizeAll();
},
$$: $$
};
// Autosize all elements once the DOM is loaded
// DOM already loaded?
if (document.readyState !== "loading") {
_.init();
}
else {
// Wait for it
document.addEventListener("DOMContentLoaded", _.init);
}
// Listen for changes
var listener = function(evt) {
if (_.active) {
_.resize(evt.target);
}
};
document.body.addEventListener("input", listener);
// Firefox fires a change event instead of an input event
document.body.addEventListener("change", listener);
// Listen for new elements
if (self.MutationObserver) {
(new MutationObserver(function(mutations) {
if (_.active) {
mutations.forEach(function(mutation) {
if (mutation.type == "childList") {
Stretchy.resizeAll(mutation.addedNodes);
}
});
}
})).observe(document.body, {
childList: true,
subtree: true
});
}
})();