Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Navigation menu keyboard events #487

Merged
merged 2 commits into from
Oct 4, 2024
Merged
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
8 changes: 8 additions & 0 deletions components/DropdownMenu/DropdownSubMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const DropdownSubMenu = ({ items }: DropdownMenuProps) => {
index === activeTab ? styles.active : ""
)}
onClick={() => setActiveTab(index)}
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
setActiveTab(index);
}
}}
aria-expanded={activeTab === index}
>
{submenuTitle}
<Icon name="arrowRight" size="sm" />
Expand Down
21 changes: 21 additions & 0 deletions components/Menu/Category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const MenuCategory = ({
? submenus
: columns
: null;

const toggleOpened = useCallback(
(e: React.MouseEvent<HTMLAnchorElement>) => {
if (children) {
Expand All @@ -81,13 +82,25 @@ const MenuCategory = ({
},
[opened, children, id, onToggleOpened, onClick]
);

const open = useCallback(
(e: React.MouseEvent<HTMLAnchorElement>) => {
onHover(id);
},
[id, onHover]
);

const keyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
!opened ? onToggleOpened(id) : onToggleOpened(null);
}
if (e.key === "Escape") onToggleOpened(null);
},
[id, opened, onToggleOpened]
);

const containsSubCategories = !!columns || !!submenus;

return (
Expand All @@ -99,6 +112,11 @@ const MenuCategory = ({
)}
ref={ref}
onMouseLeave={() => toggleOpened(null)}
onKeyDown={(e) => {
if (e.key === "Escape") {
onToggleOpened(null);
}
}}
>
{isDropdown === "link" ? (
<Link
Expand All @@ -116,7 +134,10 @@ const MenuCategory = ({
className={cn(styles.menuButton, opened ? styles.opened : "")}
onClick={toggleOpened}
onMouseEnter={open}
tabIndex={0}
data-testid={testId}
onKeyDown={keyDown}
aria-expanded={opened}
>
{title}
{isDropdown === "dropdown" && (
Expand Down
Loading