-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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, | ||
|
@@ -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 = ( | ||
|
@@ -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, | ||
})}> | ||
<NavbarNavLink | ||
aria-haspopup="true" | ||
<button | ||
type="button" | ||
ref={buttonRef} | ||
aria-expanded={showDropdown} | ||
role="button" | ||
href={props.to ? undefined : '#'} | ||
className={clsx('navbar__link', className)} | ||
{...props} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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 thebutton
if it was open with a hover