Search code examples
javascripthtmlangularangular-router

Button retains focused after click


I have a button, that when clicked, navigates to another route. The relative code is:

<button (click)="goBack()" mat-button
      aria-label="Previous Screen" role="navigation">
      <mat-icon>keyboard_arrow_left</mat-icon>
</button>

And the click handler:

  goBack() {
    this.location.back();
  }

When I click the button, the route changes, but the button gains focus. Is that what am I doing wrong?

Demonstration


Solution

  • You want to be looking at the HTMLElement.blur() functionality. I'm not too familiar with Angular, but you need to have access to the event and its target in order to do so.

    Something like this should work:

    <button (click)="goBack($event)" mat-button
          aria-label="Previous Screen" role="navigation">
          <mat-icon>keyboard_arrow_left</mat-icon>
    </button>
    

    And for the JS:

    goBack(event) {
        event.target.blur()
        this.location.back();
      }