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:
I noticed these differences:
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.
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).