Skip to content

Commit

Permalink
minor refactor to be more descriptive & to consolidate scrollBehavior…
Browse files Browse the repository at this point in the history
… handling code
  • Loading branch information
jackmoore committed Nov 21, 2022
1 parent d2164f9 commit 31543e1
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ function assign(ta) {
ta.style.overflowY = value;
}

function bookmarkOverflows(el) {
function cacheScrollTops(el) {
const arr = [];

while (el && el.parentNode && el.parentNode instanceof Element) {
if (el.parentNode.scrollTop) {
el.parentNode.style.scrollBehavior = 'auto';
arr.push([el.parentNode, el.parentNode.scrollTop]);
}
el = el.parentNode;
}

return ()=> arr.forEach(([node, scrollTop]) => {
node.style.scrollBehavior = 'auto';
node.scrollTop = scrollTop;
node.style.scrollBehavior = null;
});
Expand All @@ -106,16 +106,16 @@ function assign(ta) {
return;
}

// remove smooth scroll & prevent scroll-position jumping by restoring original scroll position
const restoreOverflows = bookmarkOverflows(ta);
// ensure the scrollTop values of parent elements are not modified as a consequence of calculating the textarea height
const restoreScrollTops = cacheScrollTops(ta);

ta.style.height = '';
ta.style.height = (ta.scrollHeight+heightOffset)+'px';

// used to check if an update is actually necessary on window.resize
clientWidth = ta.clientWidth;

restoreOverflows();
restoreScrollTops();
}

function update() {
Expand Down

0 comments on commit 31543e1

Please sign in to comment.