UNPKG

autoresize

Version:
1 lines 1.85 kB
!function(){"use strict";var e=Object.freeze({minimumRows:1,maximumRows:1/0,rowHeight:null,assumeRendered:!1});function n(e){return window.getComputedStyle(e)}function t(e,n){const t=n[e];return t&&t.endsWith("px")?parseInt(t.slice(0,-2),10):t}function r(e){let n=!1;return function(...t){n||requestAnimationFrame(()=>{n=!1,e(...t)}),n=!0}}function o(e){const{id:n,className:t,type:r}=e;if(n)return null!=document.getElementById(n);const o=t.replace(/\s/,"."),s=o?`${r}.${o}`:r;return[...document.querySelectorAll(s)].some(n=>n===e)}function s(e,o){const{maximumRows:s}=o;let i,{rowHeight:u}=o;const l=null==u,c=function(e,n){const t=function(e,n){const{value:t}=e;e.value="",e.rows=1;const{scrollHeight:r}=e;return e.value=t,e.rows=n,r}(e,n.minimumRows),{minimumRows:r,maximumRows:o}=n;return n=>{e.rows=1;const l=e.scrollHeight-t+n,c=Math.ceil(l/n);e.rows=(u=o,(s=r)<(i=c)?i<u?i:s<u?u:s:i>u?i:s>u?u:s)};var s,i,u}(e,o),m=()=>c(u);if(l){u=t("lineHeight",n(e)),i=r(()=>{u=t("lineHeight",n(e)),m()}),window.addEventListener("resize",i)}const a=r(m);e.addEventListener("keydown",a);const d=function(e){return n=>{n.target.rows<e&&(n.target.scrollTop=0)}}(s);return e.addEventListener("scroll",d),()=>{e.removeEventListener("keydown",a),e.removeEventListener("scroll",d),l&&window.removeEventListener("resize",i)}}var i=Object.freeze({maxWidth:null,assumeRendered:!1});window.autoresize=function(t,r={}){return t instanceof HTMLTextAreaElement?function(n,t){const r=Object.assign({},e,t);let i,u=()=>{};return r.assumeRendered||o(n)?u=s(n,r):i=setInterval(()=>{o(n)&&(clearInterval(i),u=s(n,r))},1e3),()=>{clearInterval(i),u()}}(t,r):t instanceof HTMLInputElement?function(e,t){return Object.assign({},i,t),n(e),()=>{}}(t,r):(console.error("Element type is not supported. Supported item types are: HTMLTextAreaElement, HTMLInputElement."),null)}}();