Skip to content
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

Don't put event handlers on non-interactive elements #44

Open
oliverjam opened this issue Dec 10, 2021 · 0 comments
Open

Don't put event handlers on non-interactive elements #44

oliverjam opened this issue Dec 10, 2021 · 0 comments

Comments

@oliverjam
Copy link

oliverjam commented Dec 10, 2021

<GiHamburgerMenu
onClick={() => {
setOpen(true);
}}

One of React's biggest flaws is how easy it makes it to add interactive behaviour to inappropriate elements 😅. Here you've added a click handler to an <svg>. However <svg>s are not usually interactive—by default they aren't focusable, you can't trigger them with your keyboard, and assistive tech won't tell users they are clickable.

Generally when you want a clickable thing your first thought should be <button>. Buttons have all the above interactive behaviour built-in. You can style them to be totally invisible and put the icons inside of them.

If you really need to make a non-interactive element like a div or svg clickable you need to do a bunch more work:

  • make it focusable with tabindex
  • add onKeyDown for spacebar/enter to trigger
  • add the right ARIA role for the type of component it is
  • do lots of testing to make sure it works with assistive tech

It's almost always easier to just use a button 😅

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

No branches or pull requests

1 participant