Skip to content

Commit 83453c1

Browse files
crisbetommalerba
authored andcommitted
fix(autosize): textarea receiving focus on init (#11460)
Fixes autosizeable textareas receiving focus when they're initialized. Fixes #11451. Fixes #11308.
1 parent 82299da commit 83453c1

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/cdk/text-field/autosize.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
233233
// IE will throw an "Unspecified error" if we try to set the selection range after the
234234
// element has been removed from the DOM. Assert that the directive hasn't been destroyed
235235
// between the time we requested the animation frame and when it was executed.
236-
if (!this._destroyed.isStopped) {
236+
// Also note that we have to assert that the textarea is focused before we set the
237+
// selection range. Setting the selection range on a non-focused textarea will cause
238+
// it to receive focus on IE and Edge.
239+
if (!this._destroyed.isStopped && document.activeElement === textarea) {
237240
textarea.setSelectionRange(selectionStart, selectionEnd);
238241
}
239242
}));

0 commit comments

Comments
 (0)