refac
This commit is contained in:
@@ -35,6 +35,19 @@
|
||||
|
||||
const resize = () => {
|
||||
if (textareaElement) {
|
||||
// Find all ancestors that currently have an active scroll offset.
|
||||
// This is extremely fast because reading `scrollTop` on a clean layout doesn't trigger reflow,
|
||||
// and it makes the fix 100% robust without relying on hardcoded CSS classes.
|
||||
let activeScrollParents = [];
|
||||
let p = textareaElement.parentNode;
|
||||
while (p && p !== document.body) {
|
||||
if (p instanceof HTMLElement && p.scrollTop > 0) {
|
||||
activeScrollParents.push({ el: p, top: p.scrollTop });
|
||||
}
|
||||
p = p.parentNode;
|
||||
}
|
||||
const windowScrollY = window.scrollY;
|
||||
|
||||
textareaElement.style.height = '';
|
||||
|
||||
let height = textareaElement.scrollHeight;
|
||||
@@ -46,6 +59,15 @@
|
||||
}
|
||||
|
||||
textareaElement.style.height = `${height}px`;
|
||||
|
||||
// Only restore scroll for elements that were actually scrolled.
|
||||
// This prevents layout thrashing that happens if we blindly set `.scrollTop` on every parent.
|
||||
activeScrollParents.forEach((p) => {
|
||||
if (p.el.scrollTop !== p.top) p.el.scrollTop = p.top;
|
||||
});
|
||||
if (window.scrollY !== windowScrollY) {
|
||||
window.scrollTo(window.scrollX, windowScrollY);
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user