refac
This commit is contained in:
1 parent
3d48596c9e
commit
c978a788c8
1 file changed
+22
@@ -35,6 +35,19 @@
|
|||||||
|
|
||||||
const resize = () => {
|
const resize = () => {
|
||||||
if (textareaElement) {
|
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 = '';
|
textareaElement.style.height = '';
|
||||||
|
|
||||||
let height = textareaElement.scrollHeight;
|
let height = textareaElement.scrollHeight;
|
||||||
@@ -46,6 +59,15 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
textareaElement.style.height = `${height}px`;
|
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>
|
</script>
|
||||||
|
|||||||
Reference in new issue
Block a user