Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A11Y improvements for DropdownNavbarItemDesktop #9439

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {KeyboardEventHandler} from 'react';
import React, {useState, useRef, useEffect} from 'react';
import clsx from 'clsx';
import {
Expand All @@ -13,12 +14,14 @@ import {
Collapsible,
} from '@docusaurus/theme-common';
import {isSamePath, useLocalPathname} from '@docusaurus/theme-common/internal';
import useIsBrowser from '@docusaurus/useIsBrowser';
import NavbarNavLink from '@theme/NavbarItem/NavbarNavLink';
import NavbarItem, {type LinkLikeNavbarItemProps} from '@theme/NavbarItem';
import type {
DesktopOrMobileNavBarItemProps,
Props,
} from '@theme/NavbarItem/DropdownNavbarItem';
import styles from './styles.module.css';

function isItemActive(
item: LinkLikeNavbarItemProps,
Expand Down Expand Up @@ -51,7 +54,17 @@ function DropdownNavbarItemDesktop({
...props
}: DesktopOrMobileNavBarItemProps) {
const dropdownRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);

const [showDropdown, setShowDropdown] = useState(false);
const isBrowser = useIsBrowser();

const onKeyDown: KeyboardEventHandler = (e) => {
if (e.key === 'Escape') {
setShowDropdown(false);
buttonRef.current?.focus();
}
};

useEffect(() => {
const handleClickOutside = (
Expand All @@ -78,28 +91,27 @@ function DropdownNavbarItemDesktop({
}, [dropdownRef]);

return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
ref={dropdownRef}
className={clsx('navbar__item', 'dropdown', 'dropdown--hoverable', {
onMouseEnter={() => setShowDropdown(true)}
onMouseLeave={() => setShowDropdown(false)}
onKeyDown={onKeyDown}
className={clsx('navbar__item', 'dropdown', {
'dropdown--right': position === 'right',
'dropdown--show': showDropdown,
// When hydrated, handle hover state in JS
// It need to be kept in sync with the button onClick
'dropdown--hoverable': !isBrowser,
})}>
Comment on lines +97 to 106
Copy link
Author

@srapilly srapilly Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the same behavior as https://squareup.com navbar and https://www.radix-ui.com/primitives/docs/components/navigation-menu

The dropdown can be collapsed with a click on the button if it was open with a hover

<NavbarNavLink
aria-haspopup="true"
<button
type="button"
ref={buttonRef}
aria-expanded={showDropdown}
role="button"
href={props.to ? undefined : '#'}
className={clsx('navbar__link', className)}
{...props}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this draft/test I removed from now the props spread

onClick={props.to ? undefined : (e) => e.preventDefault()}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
setShowDropdown(!showDropdown);
}
}}>
className={clsx(styles.dropdownButton, className)}
onClick={() => setShowDropdown(!showDropdown)}>
{props.children ?? props.label}
</NavbarNavLink>
</button>
<ul className="dropdown__menu">
{items.map((childItemProps, i) => (
<NavbarItem
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.dropdownButton {
font-size: inherit;
border: none;
background: none;
color: var(--ifm-navbar-link-color);
font-weight: var(--ifm-font-weight-semibold);
cursor: pointer;
padding: 0;
}

.dropdownButton:hover {
color: var(--ifm-navbar-link-hover-color);
}

.dropdownButton::after {
display: inline-block;
border-color: currentColor transparent;
border-style: solid;
border-width: 0.4em 0.4em 0;
content: '';
margin-left: 0.3em;
position: relative;
top: 2px;
transform: translateY(-50%);
}
Comment on lines +8 to +32
Copy link
Author

@srapilly srapilly Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I used a native button, I needed to reset native styles. I guess everything from this file should be moved/refactored to this repo https://github.com/facebookincubator/infima

I didn't reuse the navbar__link in this current PR because they were some styles I didn't want, for example:

https://github.com/facebookincubator/infima/blob/79b4f86e4d161a8a61205ab0bc4676f97460f9ae/packages/core/styles/components/navbar.pcss#L135