Disable auto focus in dialog- modal in Angular 2/ material Disable auto focus in dialog- modal in Angular 2/ material angular angular

Disable auto focus in dialog- modal in Angular 2/ material


Since @angular/material@5.0.0-rc.1 there is special option autoFocus on MatDialogConfig

/** Whether the dialog should focus the first focusable element on open. */autoFocus?: boolean = true;

So you can use it as follows:

let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {  width: '250px',  data: { name: this.name, animal: this.animal },  autoFocus: false   <============================== this line});

Stackblitz Example


I used the solution to disable auto focus when open the dialog and also avoid auto focus on the last selected button. I see that it works well for both dialog and bottom sheet control in Angular Material, see the code:

this.dialog.open(YourComponent, {      data: inputData,      width: '100%',       autoFocus: false,       restoreFocus: false    });this.matBottomSheet.open(FilePickerComponent, {      data: inputData,      autoFocus: false,      restoreFocus: false});