Skip to content

Commit d0a4e9f

Browse files
crisbetommalerba
authored andcommitted
fix(text-field): autosize textarea not scrolling to end of input on firefox (#10403)
Fixes the textarea not being scrolled to the text cursor when the textarea is resized on Firefox. Fixes #10400.
1 parent e409ec4 commit d0a4e9f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/cdk/text-field/autosize.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,22 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
191191
textarea.style.overflow = 'hidden';
192192
textarea.placeholder = '';
193193

194+
const height = textarea.scrollHeight;
195+
194196
// Use the scrollHeight to know how large the textarea *would* be if fit its entire value.
195-
textarea.style.height = `${textarea.scrollHeight}px`;
197+
textarea.style.height = `${height}px`;
196198
textarea.style.overflow = '';
197199
textarea.placeholder = placeholderText;
198200

201+
// On Firefox resizing the textarea will prevent it from scrolling to the caret position.
202+
// We need to re-set the selection in order for it to scroll to the proper position.
203+
if (typeof requestAnimationFrame !== 'undefined') {
204+
this._ngZone.runOutsideAngular(() => requestAnimationFrame(() => {
205+
const {selectionStart, selectionEnd} = textarea;
206+
textarea.setSelectionRange(selectionStart, selectionEnd);
207+
}));
208+
}
209+
199210
this._previousValue = value;
200211
}
201212

0 commit comments

Comments
 (0)