Skip to content

Commit 66403ca

Browse files
fix(cdk/text-field): avoid page jump on auto-resize
When we measure the size of the autosize textarea, we make it temporarily smaller which can cause the scroll position to shift. These changes add a workaround that assign a temporary margin-bottom while measuring. Fixes #23834
1 parent 8cff9c5 commit 66403ca

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/cdk/text-field/autosize.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,14 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
237237
const element = this._textareaElement;
238238
const previousMargin = element.style.marginBottom || '';
239239
const isFirefox = this._platform.FIREFOX;
240-
const needsMarginFiller = isFirefox && this._hasFocus;
241240
const measuringClass = isFirefox
242241
? 'cdk-textarea-autosize-measuring-firefox'
243242
: 'cdk-textarea-autosize-measuring';
244243

245-
// In some cases the page might move around while we're measuring the `textarea` on Firefox. We
244+
// In some cases the page might move around while we're measuring the `textarea`. We
246245
// work around it by assigning a temporary margin with the same height as the `textarea` so that
247-
// it occupies the same amount of space. See #23233.
248-
if (needsMarginFiller) {
249-
element.style.marginBottom = `${element.clientHeight}px`;
250-
}
246+
// it occupies the same amount of space. See #23233 and #23834.
247+
element.style.marginBottom = `${element.clientHeight}px`;
251248

252249
// Reset the textarea height to auto in order to shrink back to its default size.
253250
// Also temporarily force overflow:hidden, so scroll bars do not interfere with calculations.
@@ -257,9 +254,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
257254
const scrollHeight = element.scrollHeight - 4;
258255
element.classList.remove(measuringClass);
259256

260-
if (needsMarginFiller) {
261-
element.style.marginBottom = previousMargin;
262-
}
257+
element.style.marginBottom = previousMargin;
263258

264259
return scrollHeight;
265260
}

0 commit comments

Comments
 (0)