Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mat-toggle-slide Triggers

I have a regular mat-slide-toggle with a click event like below:

<mat-slide-toggle (click)="do()" [(ngModel)]="checked"></mat-slide-toggle>

This component is triggered by both click and spacebar. But with the spacebar it doesn’t trigger the click event. How can I do so?

And in terms of accessibility. It is right to trigger by spacebar and not with enter key?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

But with the spacebar it doesn’t trigger the click event. How can I do so?

You should use (change)="do()" as the underlying component is an input type="checkbox". This should cover all input types.

And in terms of accessibility. It is right to trigger by spacebar and not with enter key?

Yes, a switch operates in the same way as a checkbox so the expected key is Space. You can read more about expected behaviour of a role="switch" element on MDN.

It is worth noting that if the underlying component for a role="switch" is a <button> then you can use (click)="do()" and it will work with Enter as well, but this isn’t the case in the material library component (useful to know if you ever make your own though!).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading