Skip to content

Commit

Permalink
feat(navbar keyboard navigation): optionally navigate to submenu item…
Browse files Browse the repository at this point in the history
…s using the `Tab` and `Enter` keys. (#3482)

* feat(navbar keyboard navigation): add arrow keys navigation to navbar

* feat(navbar keyboard navigation): add arrow keys navigation to navbar

* feat(navbar keyboard navigation): add event types

* feat(navbar keyboard navigation): cleanup unused code

* feat(navbar keyboard navigation): make Tab and Enter key primarily for keyboard navigation

* feat(navbar keyboard navigation): make Tab and Enter key primarily for keyboard navigation

* feat(navbar keyboard navigation): tabIndex to focus submenu container

* feat(navbar keyboard navigation): update changeset

* feat(navbar keyboard navigation): rename state variable

* feat(navbar keyboard navigation): prevent default enter behaviour

* feat(navbar keyboard navigation): handle submenu state after onBlur

* feat(keyboard navigation): add submenu preview on navigation

* feat(keyboard navigation): remove unused variables

* feat(accessibility): remove unused hook

* feat(main nav accessibility): update focus when navigating to a new submenu

---------

Co-authored-by: Ddouglasz <douglas.egiemeh@gmail.com>
  • Loading branch information
ddouglasz and Ddouglasz committed Apr 23, 2024
1 parent 2431917 commit 3836786
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-clouds-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-frontend/application-shell': minor
---

Improve keyboard accessibility by switching between the menu items and submenu items using mainly the Tab and Enter keys.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export type MenuGroupProps = {
children?: ReactNode;
submenuVerticalPosition?: number;
isSubmenuAboveMenuItem?: boolean;
handleKeyDown?: React.KeyboardEventHandler<HTMLUListElement>;
};

const MenuGroup = forwardRef<HTMLUListElement, MenuGroupProps>((props, ref) => {
Expand All @@ -189,6 +190,7 @@ const MenuGroup = forwardRef<HTMLUListElement, MenuGroupProps>((props, ref) => {
const isSublistActiveWhileIsMenuCollapsed = Boolean(
props.level === 2 && props.isActive && !props.isExpanded
);

return (
<MenuList
ref={ref && props.level === 2 ? ref : null}
Expand All @@ -200,6 +202,7 @@ const MenuGroup = forwardRef<HTMLUListElement, MenuGroupProps>((props, ref) => {
isSublistActiveWhileIsMenuExpanded ||
isSublistActiveWhileIsMenuCollapsed
}
onKeyDown={props.handleKeyDown}
className={classnames(
{
'sublist-expanded__active': isSublistActiveWhileIsMenuExpanded,
Expand Down Expand Up @@ -249,6 +252,7 @@ type MenuItemProps = {
| FocusEventHandler<HTMLElement>;
children: ReactNode;
identifier?: string;
onKeyDown?: (e: React.KeyboardEvent<HTMLLIElement>) => void;
};
const MenuItem = (props: MenuItemProps) => {
return (
Expand All @@ -259,6 +263,7 @@ const MenuItem = (props: MenuItemProps) => {
onMouseLeave={props.onMouseLeave as MouseEventHandler<HTMLElement>}
onFocus={props.onMouseEnter as FocusEventHandler<HTMLElement>}
onBlur={props.onMouseLeave as FocusEventHandler<HTMLElement>}
onKeyDown={props.onKeyDown}
data-menuitem={props.identifier}
className={classnames({
active: props.isActive,
Expand All @@ -281,6 +286,7 @@ export type MenuItemLinkProps = {
onClick?: (event: SyntheticEvent<HTMLAnchorElement>) => void;
useFullRedirectsForLinks?: boolean;
isSubmenuLink?: boolean;
isSubmenuFocused?: boolean;
};
const menuItemLinkDefaultProps: Pick<MenuItemLinkProps, 'exactMatch'> = {
exactMatch: false,
Expand All @@ -306,6 +312,7 @@ const MenuItemLink = (props: MenuItemLinkProps) => {
activeClassName="highlighted"
data-link-level={linkLevel}
css={getMenuItemLinkStyles(Boolean(props.isSubmenuLink))}
tabIndex={props.isSubmenuLink && !props.isSubmenuFocused ? -1 : 0}
onClick={(event) => {
if (props.linkTo && props.useFullRedirectsForLinks) {
event.preventDefault();
Expand Down
18 changes: 16 additions & 2 deletions packages/application-shell/src/components/navbar/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const getIsSubmenuRouteActive = (
export const ApplicationMenu = (props: ApplicationMenuProps) => {
const [submenuVerticalPosition, setSubmenuVerticalPosition] = useState(0);
const [isSubmenuAboveMenuItem, setIsSubmenuAboveMenuItem] = useState(false);
const [isSubmenuFocused, setIsSubmenuFocused] = useState(false);
const observerRef = useRef<IntersectionObserver | null>(null);
const submenuRef = useRef<HTMLUListElement>(null);

const hasSubmenu =
Expand Down Expand Up @@ -149,8 +151,6 @@ export const ApplicationMenu = (props: ApplicationMenuProps) => {
[menuItemIdentifier, props.isMenuOpen]
);

const observerRef = useRef<IntersectionObserver | null>(null);

useLayoutEffect(() => {
observerRef.current = new IntersectionObserver(callbackFn, {
rootMargin: '-100% 0px 0px 0px', // we want to observe if the submenu crosses the bottom line of the viewport - therefore we set the root element top margin to -100% of the viewport height
Expand All @@ -166,6 +166,7 @@ export const ApplicationMenu = (props: ApplicationMenuProps) => {
if (observer && currentSubmenuRef) {
observer.observe(currentSubmenuRef);
}
setIsSubmenuFocused(false);
return () => observer?.disconnect();
}, [
menuItemIdentifier,
Expand Down Expand Up @@ -195,6 +196,17 @@ export const ApplicationMenu = (props: ApplicationMenuProps) => {
? getMenuVisibilitiesOfSubmenus(props.menu)
: getMenuVisibilityOfMainmenu(props.menu);

const handleKeyDown = (e: React.KeyboardEvent<HTMLLIElement>) => {
const currentlyFocusedItem = submenuRef.current?.querySelector(':focus');

if (e.key === 'Enter') {
setIsSubmenuFocused(true);
if (!currentlyFocusedItem) {
submenuRef.current?.querySelector('a')?.focus();
}
}
};

return (
<RestrictedMenuItem
key={props.menu.key}
Expand All @@ -213,6 +225,7 @@ export const ApplicationMenu = (props: ApplicationMenuProps) => {
isMainMenuRouteActive={isMainMenuRouteActive}
isMenuOpen={props.isMenuOpen}
onClick={props.handleToggleItem}
onKeyDown={handleKeyDown}
onMouseEnter={props.handleToggleItem}
onMouseLeave={props.shouldCloseMenuFly}
identifier={menuItemIdentifier}
Expand Down Expand Up @@ -282,6 +295,7 @@ export const ApplicationMenu = (props: ApplicationMenuProps) => {
}
onClick={props.onMenuItemClick}
isSubmenuLink
isSubmenuFocused={isSubmenuFocused}
>
<MenuLabel
labelAllLocales={submenu.labelAllLocales}
Expand Down

0 comments on commit 3836786

Please sign in to comment.