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

fix(v2): add a11y support for dropdown #2672

Merged
merged 2 commits into from
Apr 27, 2020
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
76 changes: 43 additions & 33 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function NavLink({
to,
href,
label,
position,
activeClassName = 'navbar__link--active',
...props
}) {
Expand Down Expand Up @@ -58,11 +57,14 @@ function NavLink({
}

function NavItem({items, position, className, ...props}) {
const navLinkClassNames = (extraClassName, isDropdownItem) =>
classnames(extraClassName, {
'navbar__item navbar__link': !isDropdownItem,
dropdown__link: isDropdownItem,
});
const navLinkClassNames = (extraClassName, isDropdownItem = false) =>
classnames(
{
'navbar__item navbar__link': !isDropdownItem,
dropdown__link: isDropdownItem,
},
extraClassName,
);

if (!items) {
return <NavLink className={navLinkClassNames(className)} {...props} />;
Expand All @@ -74,31 +76,41 @@ function NavItem({items, position, className, ...props}) {
'dropdown--left': position === 'left',
'dropdown--right': position === 'right',
})}>
<NavLink className={navLinkClassNames(className)} {...props}>
<NavLink
className={navLinkClassNames(className)}
{...props}
onClick={(e) => e.preventDefault()}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.target.parentNode.classList.toggle('dropdown--show');
}
}}>
{props.label}
</NavLink>
<ul className="dropdown__menu">
{items.map(
({className: childItemClassName, ...linkItemInnerProps}, i) => (
<li key={i}>
<NavLink
activeClassName="dropdown__link--active"
className={navLinkClassNames(childItemClassName, true)}
{...linkItemInnerProps}
/>
</li>
),
)}
{items.map(({className: childItemClassName, ...childItemProps}, i) => (
<li key={i}>
<NavLink
activeClassName="dropdown__link--active"
className={navLinkClassNames(childItemClassName, true)}
{...childItemProps}
/>
</li>
))}
</ul>
</div>
);
}

function MobileNavItem({items, className, ...props}) {
const navLinkClassNames = (extraClassName, isSubList = false) =>
classnames('menu__link', extraClassName, {
'menu__link--sublist': isSubList,
});
classnames(
'menu__link',
{
'menu__link--sublist': isSubList,
},
extraClassName,
);

if (!items) {
return (
Expand All @@ -114,18 +126,16 @@ function MobileNavItem({items, className, ...props}) {
{props.label}
</NavLink>
<ul className="menu__list">
{items.map(
({className: childItemClassName, ...linkItemInnerProps}, i) => (
<li className="menu__list-item" key={i}>
<NavLink
activeClassName="menu__link--active"
className={navLinkClassNames(childItemClassName)}
{...linkItemInnerProps}
onClick={props.onClick}
/>
</li>
),
)}
{items.map(({className: childItemClassName, ...childItemProps}, i) => (
<li className="menu__list-item" key={i}>
<NavLink
activeClassName="menu__link--active"
className={navLinkClassNames(childItemClassName)}
{...childItemProps}
onClick={props.onClick}
/>
</li>
))}
</ul>
</li>
);
Expand Down
1 change: 1 addition & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ module.exports = {
href: 'https://github.com/facebook/docusaurus',
position: 'right',
className: 'header-github-link',
'aria-label': 'GitHub repository',
Copy link
Contributor Author

@lex111 lex111 Apr 27, 2020

Choose a reason for hiding this comment

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

Fixes a11y issue:

Links must have discernible text

image

},
],
},
Expand Down