Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda