Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

325
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda