1 const map = (typeof Map === "function") ? new Map() : (function () {
7 return keys.indexOf(key) > -1;
10 return values[keys.indexOf(key)];
13 if (keys.indexOf(key) === -1) {
19 const index = keys.indexOf(key);
21 keys.splice(index, 1);
22 values.splice(index, 1);
28 let createEvent = (name)=> new Event(name, {bubbles: true});
32 // IE does not support `new Event()`
33 createEvent = (name)=> {
34 const evt = document.createEvent('Event');
35 evt.initEvent(name, true, false);
41 if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || map.has(ta)) return;
43 let heightOffset = null;
44 let clientWidth = null;
45 let cachedHeight = null;
48 const style = window.getComputedStyle(ta, null);
50 if (style.resize === 'vertical') {
51 ta.style.resize = 'none';
52 } else if (style.resize === 'both') {
53 ta.style.resize = 'horizontal';
56 if (style.boxSizing === 'content-box') {
57 heightOffset = -(parseFloat(style.paddingTop)+parseFloat(style.paddingBottom));
59 heightOffset = parseFloat(style.borderTopWidth)+parseFloat(style.borderBottomWidth);
61 // Fix when a textarea is not on document body and heightOffset is Not a Number
62 if (isNaN(heightOffset)) {
69 function changeOverflow(value) {
71 // Chrome/Safari-specific fix:
72 // When the textarea y-overflow is hidden, Chrome/Safari do not reflow the text to account for the space
73 // made available by removing the scrollbar. The following forces the necessary text reflow.
74 const width = ta.style.width;
75 ta.style.width = '0px';
77 /* jshint ignore:start */
79 /* jshint ignore:end */
80 ta.style.width = width;
83 ta.style.overflowY = value;
86 function getParentOverflows(el) {
89 while (el && el.parentNode && el.parentNode instanceof Element) {
90 if (el.parentNode.scrollTop) {
93 scrollTop: el.parentNode.scrollTop,
103 if (ta.scrollHeight === 0) {
104 // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM.
108 const overflows = getParentOverflows(ta);
109 const docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240)
111 ta.style.height = '';
112 ta.style.height = (ta.scrollHeight+heightOffset)+'px';
114 // used to check if an update is actually necessary on window.resize
115 clientWidth = ta.clientWidth;
117 // prevents scroll-position jumping
118 overflows.forEach(el => {
119 el.node.scrollTop = el.scrollTop
123 document.documentElement.scrollTop = docTop;
130 const styleHeight = Math.round(parseFloat(ta.style.height));
131 const computed = window.getComputedStyle(ta, null);
133 // Using offsetHeight as a replacement for computed.height in IE, because IE does not account use of border-box
134 var actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(computed.height)) : ta.offsetHeight;
136 // The actual height not matching the style height (set via the resize method) indicates that
137 // the max-height has been exceeded, in which case the overflow should be allowed.
138 if (actualHeight < styleHeight) {
139 if (computed.overflowY === 'hidden') {
140 changeOverflow('scroll');
142 actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(window.getComputedStyle(ta, null).height)) : ta.offsetHeight;
145 // Normally keep overflow set to hidden, to avoid flash of scrollbar as the textarea expands.
146 if (computed.overflowY !== 'hidden') {
147 changeOverflow('hidden');
149 actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(window.getComputedStyle(ta, null).height)) : ta.offsetHeight;
153 if (cachedHeight !== actualHeight) {
154 cachedHeight = actualHeight;
155 const evt = createEvent('autosize:resized');
157 ta.dispatchEvent(evt);
159 // Firefox will throw an error on dispatchEvent for a detached element
160 // https://bugzilla.mozilla.org/show_bug.cgi?id=889376
165 const pageResize = () => {
166 if (ta.clientWidth !== clientWidth) {
171 const destroy = (style => {
172 window.removeEventListener('resize', pageResize, false);
173 ta.removeEventListener('input', update, false);
174 ta.removeEventListener('keyup', update, false);
175 ta.removeEventListener('autosize:destroy', destroy, false);
176 ta.removeEventListener('autosize:update', update, false);
178 Object.keys(style).forEach(key => {
179 ta.style[key] = style[key];
184 height: ta.style.height,
185 resize: ta.style.resize,
186 overflowY: ta.style.overflowY,
187 overflowX: ta.style.overflowX,
188 wordWrap: ta.style.wordWrap,
191 ta.addEventListener('autosize:destroy', destroy, false);
193 // IE9 does not fire onpropertychange or oninput for deletions,
194 // so binding to onkeyup to catch most of those events.
195 // There is no way that I know of to detect something like 'cut' in IE9.
196 if ('onpropertychange' in ta && 'oninput' in ta) {
197 ta.addEventListener('keyup', update, false);
200 window.addEventListener('resize', pageResize, false);
201 ta.addEventListener('input', update, false);
202 ta.addEventListener('autosize:update', update, false);
203 ta.style.overflowX = 'hidden';
204 ta.style.wordWrap = 'break-word';
214 function destroy(ta) {
215 const methods = map.get(ta);
221 function update(ta) {
222 const methods = map.get(ta);
230 // Do nothing in Node.js environment and IE8 (or lower)
231 if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
233 autosize.destroy = el => el;
234 autosize.update = el => el;
236 autosize = (el, options) => {
238 Array.prototype.forEach.call(el.length ? el : [el], x => assign(x, options));
242 autosize.destroy = el => {
244 Array.prototype.forEach.call(el.length ? el : [el], destroy);
248 autosize.update = el => {
250 Array.prototype.forEach.call(el.length ? el : [el], update);
256 export default autosize;