UNPKG

@nathanaelregesdev/autoresizetextarea

Version:
1 lines 3.13 kB
{"version":3,"file":"index.mjs","sources":["../src/index.js"],"sourcesContent":["export default function autoResizeTextArea ({ele, minHeight, maxHeight}) {\n\n const module = {}\n let lastHeight = 0\n \n //If the text area overflow isn't hidden it won't work.\n ele.style.overflow = 'hidden'\n\n ele.addEventListener('keydown', test)\n\n\n\n module.forceTest = test\n\n module.reset = () => {\n ele.style.height = minHeight + \"px\";\n }\n\n return module\n\n\n\n function test () {\n\n requestAnimationFrame(() => {\n \n let eleScrolled = false\n\n //If the page is scrollable and the user is typing a new line out of the viewport\n //the browser by default scrolls down so the line which the user is in becomes visible.\n //You might want to scroll a little more to show some related button or to center correctly\n //the bottom of the element.\n //When this happens this module will call it's onTypedNewLine method and you can assign a function\n //to it and make the adjustments that you need.\n if(module.onTypedNewLine){\n if(ele.scrollTop != 0) \n eleScrolled = true\n }\n\n \n //Force scrollHeight to appear\n ele.style.height = \"10px\" \n\n\n //Add margin so the space that the element takes on the page doesn't suddenly jumps to 10px \n //and then to the adjusted height, what could possibly mess up the scrollTop of the page.\n //That's the reason why we need to keep the lastHeight Value.\n ele.style.marginBottom = lastHeight - 10 + 'px' \n \n\n //scrollHeight = height of the content\n let newHeight = ele.scrollHeight\n if(minHeight && newHeight < minHeight) newHeight = minHeight\n if(maxHeight && newHeight > maxHeight) newHeight = maxHeight\n\n\n ele.style.height = newHeight + \"px\";\n ele.style.marginBottom = ''\n \n lastHeight = newHeight\n\n\n if(eleScrolled){\n module.onTypedNewLine()\n }\n \n if(module.callBack){\n module.callBack()\n }\n\n })\n\n }\n\n}\n\n\n"],"names":["ref","module","lastHeight","ele","style","overflow","addEventListener","test","forceTest","reset","height","minHeight","requestAnimationFrame","let","eleScrolled","onTypedNewLine","scrollTop","marginBottom","newHeight","scrollHeight","maxHeight","callBack"],"mappings":"eAAe,SAA6BA,2CAElCC,KACFC,EAAa,EAejB,OAZAC,EAAIC,MAAMC,SAAW,SAErBF,EAAIG,iBAAiB,UAAWC,GAIhCN,EAAOO,UAAYD,EAEnBN,EAAOQ,iBACHN,EAAIC,MAAMM,OAASC,EAAY,MAG5BV,EAIP,SAASM,IAELK,iCAEIC,IAAIC,GAAc,EAQfb,EAAOc,gBACc,GAAjBZ,EAAIa,YACHF,GAAc,GAKtBX,EAAIC,MAAMM,OAAS,OAMnBP,EAAIC,MAAMa,aAAef,EAAa,GAAK,KAI3CW,IAAIK,EAAYf,EAAIgB,aACjBR,GAAaO,EAAYP,IAAWO,EAAYP,GAChDS,GAAaF,EAAYE,IAAWF,EAAYE,GAGnDjB,EAAIC,MAAMM,OAASQ,EAAY,KAC/Bf,EAAIC,MAAMa,aAAe,GAEzBf,EAAagB,EAGVJ,GACCb,EAAOc,iBAGRd,EAAOoB,UACNpB,EAAOoB"}