Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
React: How to make an accessible menu?

Here's my codesandbox link: https://codesandbox.io/s/accessible-menu-ejzte

I have a menu that pops up when you click on a button. It's functional, but I am noticing that it's different from examples I find online.

Menu.tsx is just a ul with children:

    <Container
      role="menu"
      id={id}
      ref={menu_ref}
      is_visible={open}
      onKeyDown={handleKeyPress}
      onClick={(e: React.MouseEvent) => {
        e.stopPropagation();
        onClose();
      }}
      {...rest}
    >
      {children}
    </Container>

MenuItem.tsx is an li with buttons in it:

const MenuItem = ({ start_text, onClick }: MenuItemProps) => (
  <Container role="none">
    <ClickableWrapper
      onClick={(e: React.MouseEvent) => onClick && onClick(e)}
      role="menuitem"
    >
      <TextContainer>
        {start_text && <StartText>{start_text}</StartText>}
      </TextContainer>
    </ClickableWrapper>
  </Container>
);

However, when I look at other websites, their menus behave differently from mine:

  1. https://www.w3.org/TR/wai-aria-practices/examples/menu-button/menu-button-links.html
  2. https://mui.com/components/menus/

I noticed these differences:

  1. The 1st menu item is automatically focused on when you open the menu by tabbing to the button and pressing Enter
  2. If the menu is open and you press Esc, the menu closes and the button is focused

Does this functionality come from Javascript? Or is it default behavior for menus that are built correctly?

I thought accessible elements were all achievable through native HTML, but so far I can't find any differences between my HTML markup and the 2 examples above, so this makes me think that this can only be achieved through JS.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The examples you provide are using JS.

w3 example: For closing on Esc see this file:

https://www.w3.org/TR/wai-aria-practices/examples/menu-button/js/MenuItemLinks.js And search for 'ESC'

And for focusing on the first child see these 2 files and search for 'setFocusToFirstItem':

https://www.w3.org/TR/wai-aria-practices/examples/menu-button/js/MenuItemLinks.js https://www.w3.org/TR/wai-aria-practices/examples/menu-button/js/PopupMenuLinks.js

For the MUI example you can search through the source code: https://github.com/mui-org/material-ui/blob/master/packages/mui-material/src/MenuList/MenuList.js

You must have updated your sandbox because closing on Esc is working (via JS).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!