autoresize
Version:
Automatically resize the inputs.
1 lines • 2.2 kB
JavaScript
function _toConsumableArray(e){if(Array.isArray(e)){for(var n=0,r=Array(e.length);n<e.length;n++)r[n]=e[n];return r}return Array.from(e)}!function(){var e=Object.freeze({minimumRows:1,maximumRows:1/0,rowHeight:null,assumeRendered:!1});function n(e){return window.getComputedStyle(e)}function r(e,n){var r=n[e];return r&&r.endsWith("px")?parseInt(r.slice(0,-2),10):r}function t(e){var n=!1;return function(){for(var r=arguments.length,t=Array(r),o=0;o<r;o++)t[o]=arguments[o];n||requestAnimationFrame(function(){n=!1,e.apply(void 0,t)}),n=!0}}function o(e){var n=e.id,r=e.className,t=e.type;if(n)return null!=document.getElementById(n);var o=r.replace(/\s/,"."),i=o?t+"."+o:t,u=document.querySelectorAll(i);return[].concat(_toConsumableArray(u)).some(function(n){return n===e})}function i(e,o){var i=o.maximumRows,u=o.rowHeight,a=void 0,s=null==u,l=function(e,n){var r=function(e,n){var r=e.value;e.value="",e.rows=1;var t=e.scrollHeight;return e.value=r,e.rows=n,t}(e,n.minimumRows),t=n.minimumRows,o=n.maximumRows;return function(n){e.rows=1;var i,u,a,s=e.scrollHeight-r+n,l=Math.ceil(s/n);e.rows=(a=o,(i=t)<(u=l)?u<a?u:i<a?a:i:u>a?u:i>a?a:i)}}(e,o),c=function(){return l(u)};if(s){u=r("lineHeight",n(e));a=t(function(){u=r("lineHeight",n(e)),c()}),window.addEventListener("resize",a)}var m=t(c);e.addEventListener("keydown",m);var v=function(e){return function(n){n.target.rows<e&&(n.target.scrollTop=0)}}(i);return e.addEventListener("scroll",v),function(){e.removeEventListener("keydown",m),e.removeEventListener("scroll",v),s&&window.removeEventListener("resize",a)}}var u=Object.freeze({maxWidth:null,assumeRendered:!1});window.autoresize=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return r instanceof HTMLTextAreaElement?function(n,r){var t=Object.assign({},e,r),u=void 0,a=function(){};return t.assumeRendered||o(n)?a=i(n,t):u=setInterval(function(){o(n)&&(clearInterval(u),a=i(n,t))},1e3),function(){clearInterval(u),a()}}(r,t):r instanceof HTMLInputElement?function(e,r){return Object.assign({},u,r),n(e),function(){}}(r,t):(console.error("Element type is not supported. Supported item types are: HTMLTextAreaElement, HTMLInputElement."),null)}}();
;