Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions client/components/Menubar/MenubarItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ function MenubarItem({
parent
]);

// Navigate menu-items using arrow_keys
handlers.onKeyDown = ({ key, target }) => {
const { parentNode: targetParentNode } = target;

if (targetParentNode && targetParentNode.parentNode) {
const selector =
targetParentNode.parentNode.getAttribute('role') === 'menu'
? '[role="menuitem"]'
: '[role="option"]';
const nodes = targetParentNode.parentNode.querySelectorAll(selector);

const targetIdx = Array.from(nodes).findIndex((node) => node === target);
if (targetIdx === -1) {
return;
}

if (key === 'ArrowDown') {
const nextIdx = (targetIdx + 1) % nodes.length;
nodes[nextIdx]?.focus();
} else if (key === 'ArrowUp') {
const prevIdx = targetIdx - 1 < 0 ? nodes.length - 1 : targetIdx - 1;
nodes[prevIdx]?.focus();
}
}
};

// Ensures that the menu-items can be navigated using the arrow_keys
// from the place where the mouse is hovering
handlers.onMouseEnter = (event) => {
event.target.focus();
};

if (hideIf) {
return null;
}
Expand Down
20 changes: 20 additions & 0 deletions client/components/Menubar/MenubarSubmenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ export function useMenuProps(id) {
function MenubarTrigger({ id, title, role, hasPopup, ...props }) {
const { isOpen, handlers } = useMenuProps(id);

// Navigate to menu-items using arrow_keys
handlers.onKeyDown = ({ key, target }) => {
const ul = target.nextSibling;
if (ul) {
const selector =
ul.getAttribute('role') === 'menu'
? '[role="menuitem"]'
: '[role="option"]';
const nodes = ul.querySelectorAll(selector);

if (key === 'ArrowDown') {
const nextIdx = 0;
nodes[nextIdx]?.focus();
} else if (key === 'ArrowUp') {
const prevIdx = nodes.length - 1;
nodes[prevIdx]?.focus();
}
}
};

return (
<button
{...handlers}
Expand Down