This commit is contained in:
Timothy Jaeryang Baek
2026-05-09 05:03:38 +09:00
parent 3d48596c9e
commit c978a788c8
+22
View File
@@ -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>