diff --git a/src/cdk/text-field/autosize.ts b/src/cdk/text-field/autosize.ts index 12dcc43676b..568b0c1d61f 100644 --- a/src/cdk/text-field/autosize.ts +++ b/src/cdk/text-field/autosize.ts @@ -191,11 +191,22 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy { textarea.style.overflow = 'hidden'; textarea.placeholder = ''; + const height = textarea.scrollHeight; + // Use the scrollHeight to know how large the textarea *would* be if fit its entire value. - textarea.style.height = `${textarea.scrollHeight}px`; + textarea.style.height = `${height}px`; textarea.style.overflow = ''; textarea.placeholder = placeholderText; + // On Firefox resizing the textarea will prevent it from scrolling to the caret position. + // We need to re-set the selection in order for it to scroll to the proper position. + if (typeof requestAnimationFrame !== 'undefined') { + this._ngZone.runOutsideAngular(() => requestAnimationFrame(() => { + const {selectionStart, selectionEnd} = textarea; + textarea.setSelectionRange(selectionStart, selectionEnd); + })); + } + this._previousValue = value; }