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

Form elements do not have associated labels, but it is a MUI theme switch

I have a theme switch (light to dark) on my page, using Material UI switch. I get an error from lighthouse stating that it needs a label, but if I make a text label it will impact the design and make it worse. Are there any solutions to this?
enter image description here

Also, here is the error from Lighthouse

enter image description here

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 :

Add an aria-label to the Switch:

<Switch
  checked = {checked}
  onChange = {handleChange}
  inputProps = {{ 'aria-label': 'theme' }}
/>

You may use other attributes in inputProps as well (e.g. aria-labelledby, title).

One of the categories Lighthouse checks for is Accessibility. When certain elements like your Switch do not have an accessible name, an aria-label may be used to provide the element with an accessible name.

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