Reputation: 481
I noticed that button gets classes cdk-focused and cdk-program-focused added after the dialog it triggered is closed. If I click anywhere effect disappears.
app.component.html [fragment]
<mat-cell *matCellDef="let element">
<span matTooltip="Delete" matTooltipPosition="right">
<button mat-icon-button color="warn" (click)="openDeleteAssociationDialog()">
<mat-icon>delete</mat-icon>
</button>
</span>
</mat-cell>
Upvotes: 21
Views: 7539
Reputation: 1608
Here the better solution is using the configuration property restoreFocus: false
matDialog.open(<your-component>, {
restoreFocus: false
});
In this case, when you close matDialog using matDialogRef.close(), the focus will not apply to that delete button/icon.
Refer: https://github.com/angular/components/issues/8420 - it has other alternative solutions including restoreFocus: false
.
Upvotes: 14
Reputation: 534
Based off @yuzhou, I found that some buttons still retain some form of focus. Instead of removing the classes, I call blur on the element, this solves my problem.
dialogRef.afterClosed().subscribe(result => {
this.changeButton['_elementRef'].nativeElement.blur();
}
Upvotes: 0
Reputation: 11
Use it like this
.mat-icon-button,
a.mat-button {
&.cdk-focused,
&.cdk-program-focused {
background-color: transparent;
.mat-button-focus-overlay {
display: none;
}
}
}
Upvotes: 1
Reputation: 3098
You can simply over ride the css in the mat-dialog consuming file
.mat-button-focus-overlay {
background-color: transparent!important;
}
In your case, add this css in mat-cell
component's css file.
Upvotes: 0
Reputation: 56
This style fixed for me:
.mat-button, .mat-flat-button, .mat-stroked-button
{
&.cdk-program-focused .mat-button-focus-overlay
{
opacity: 0 !important;
}
}
Upvotes: 1
Reputation: 492
In my case, the real problem stay in button structure, 'material' build various sub components and last one is a 'div' with css class 'mat-button-focus-overlay':
My solution is simply, in 'style.css' file, add or sobrescribe the 'mat-button-focus-overlay'
.mat-button-focus-overlay { background-color: transparent!important; }
Upvotes: 2
Reputation: 86
<button #changeButton mat-icon-button (click)="changeItem(element)" disableRipple>
<mat-icon>edit</mat-icon>
</button>
{ ViewChild, ElementRef } from '@angular/core';
@ViewChild('changeButton') private changeButton: ElementRef;
dialogRef.afterClosed().subscribe(result => {
this.changeButton['_elementRef'].nativeElement
.classList.remove('cdk-program-focused');
}
Upvotes: 6