Skip to content

Add role="switch" #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

thom-nic
Copy link

Tools like Cypress don't play well with react-toggle because while the input exists, Cypress can tell that it is hidden. So you have to cy.get('input', {force: true}) to select it.

By adding role="switch" aria-checked="checked" it is possible to treat the visible div as the interactive widget. When using testing-library we can then do things like

cy.findByRole('switch', { label: "lights" }).should('be.checked')

As a knock on effect, I think other attributes also should be moved to the outer div as well:

  • aria-label
  • aria-labelledby
  • aria-disabled

Finally, the input should no longer be needed for the screen reader, because the screen reader should understand that the div is the widget. The only purpose that the input serves (I think) would be so it could still be submitted as a form.

This was causing the following error on Node v22:

TypeError: Cannot set property navigator of #<Object> which has only a getter

Tests appear to work fine without it.
and add aria-disabled attribute to the "switch" element.

Moved the focus and blur events to the div as well.  This would affect
anyone who has attached their own event handlers and is expecting the
event target to be the input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant