From bb7c8cee6f03dd802111ca50461063a9a41782fe Mon Sep 17 00:00:00 2001 From: Dennis Hackethal Date: Sun, 18 Jan 2026 00:56:58 -0600 Subject: [PATCH] Prevent scroll jitter on input --- src/textarea-autosize.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/textarea-autosize.js b/src/textarea-autosize.js index dbeb9bb..302c320 100644 --- a/src/textarea-autosize.js +++ b/src/textarea-autosize.js @@ -20,6 +20,15 @@ class TextareaAutoSize { } update() { + // Find nearest scrollable ancestor + let box = this.element.parentElement + + while (box && !/auto|scroll/.test(getComputedStyle(box).overflowY)) { + box = box.parentElement + } + + const prevScroll = box?.scrollTop ?? 0 + const smallestHeight = this._styleProp("fontSize") this.element.style.height = `${smallestHeight}px` @@ -28,6 +37,11 @@ class TextareaAutoSize { // do the same regardless of whether this value is added or not. const newHeight = this.element.scrollHeight + this.verticalBorderSize this.element.style.height = `${newHeight}px` + + // Restore scroll to prevent jumping + if (box) { + box.scrollTop = prevScroll + } } _styleProp(name) {