1

I have two angular material buttons. My goal is to programmatically let the button ripple when a certain key is pressed.

Take a look at the stack-blitz project: https://stackblitz.com/edit/angular-material-button-vphppo

Is there a way to let the button ripple on click or on key-press exactly one time?

I tried these examples How can I programmatically trigger ripple effect on Angular MatListItem?

  • But this lets the button ripples two time on click.

1 Answers1

1

MatButton provides direct access to the Ripple in the current Material:

  @ViewChild('btnNext', {static: false}) btnNext: MatButton;

  @HostListener('document:keypress', ['$event'])
  handleKeyboardEvent(event: KeyboardEvent) { 
    if( event.key === 'a') {
      this.btnNext.ripple.launch({centered: true})
    }
  }

The above will proc the ripple when 'a' is pressed.

https://stackblitz.com/edit/angular-3xgxiw

Z. Bagley
  • 8,942
  • 1
  • 40
  • 52