I solved this by following (sort of) the Accessibility section of Angular Material.
Here is my AppComponent:
export class AppComponent implements AfterViewInit {
@ViewChild("appContainer") appContainer: ElementRef<HTMLElement>;
appContainerOrigin = this._formatOrigin(null);
constructor(
private _focusMonitor: FocusMonitor,
private _cdr: ChangeDetectorRef,
private _ngZone: NgZone,
) {}
ngAfterViewInit(): void {
this._focusMonitor.monitor(this.appContainer, true).subscribe((origin) =>
this._ngZone.run(() => {
this.appContainerOrigin = this._formatOrigin(origin);
this._cdr.markForCheck();
}),
);
}
private _formatOrigin(origin: FocusOrigin): string {
return origin ? origin + " focused" : "blurred";
}
}
Its template:
<div #appContainer class="app-container">
<app-header></app-header>
<div class="content-container">
<router-outlet></router-outlet>
</div>
<app-footer></app-footer>
</div>
And the styles.scss file at the root of the project:
.cdk-mouse-focused *:focus {
--mdc-list-list-item-focus-state-layer-color: none;
}
FocusMonitor adds a CSS class to the .app-container div which tells you how the focus was made. I then remove the color layer associated to the focus from the focused element if it was made by a mouse click. That way you get the same behavior as the Angular Material doc, as you can see in the Buttons doc. I don't think the Angular Material team implemented it like that, and I also don't think that's the ideal way to achieve it (a simple directive built in Angular Material by default would be nice) but it's the closest I could get from the expected behavior.
background-color: none;
to your &:focus CSS ? – Singlestabindex="-1"
on the button if you want to remove it permanently – ByandbycdkFocusInitial
on the button. It should make that button focused by default – Plumbism