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

Commit

Permalink
Nav keyboard events
Browse files Browse the repository at this point in the history
  • Loading branch information
PietiKinnunen committed Jul 18, 2024
1 parent 229d29d commit 73331e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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
16 changes: 16 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 Down Expand Up @@ -116,7 +129,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

0 comments on commit 73331e4

Please sign in to comment.