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

How do I include an aria-label on my IonCheckbox and still have the visible label render?

I’m using Ionic 7 and React 18. I have an IonCheckbox that I build like so

<IonItem>
  <IonLabel id="label1">Jeff Lebowski</IonLabel>
  <IonCheckbox value="123" checked={true} />
</IonItem>  

This produces the accessibility warning

[Ionic Warning]: ion-checkbox now requires providing a label with 
either the default slot or the "aria-label" attribute. To migrate, 
remove any usage of "ion-label" and pass the label text to either 
the component or the "aria-label" attribute.

Example:

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

<ion-checkbox>Label</ion-checkbox>

Example with aria-label:

<ion-checkbox aria-label="Label"></ion-checkbox>

However, if I include an aria-label,

<IonItem>
  <IonLabel id="label1">Jeff Lebowski</IonLabel>
  <IonCheckbox value="123" checked={true} aria-label="Jeff Lebowski" />
</IonItem>

I can no longer see text next to my checkbox. THe example of this is here — https://stackblitz.com/edit/an5yjh-ef4kj7?file=src%2FApp.tsx,src%2Fcomponents%2FMyForm.tsx

>Solution :

<IonCheckbox value="123" checked={true}>
  Jeff Lebowski
</IonCheckbox>

seems to produce the desired outcome.


In more detail, providing an aria-label is not the same with providing a label. aria-label is not rendered, it’s used by screen readers to announce the checkbox. Whereas the slot contents (the children of <ion-checkbox />) are displayed as label.

The ion-label has been deprecated in favour of the two above. But each of them has its own separate purpose.


Note: if, for any reason, you want to use the old syntax, you can add legacy={true} to <ion-checkbox />, along with the ion-label attribute.

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