Skip to content

Commit

Permalink
fix: add aria-controls to buttons controlling menus (#13823)
Browse files Browse the repository at this point in the history
* fix: add aria-controls to buttons controlling menus

* fix: only add aria-controls if menu is open

* fix(a11y): revert changes in OverflowMenuV2 & MenuButton

* fix(OverflowMenu): add aria-owns to span

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
francinelucca and kodiakhq[bot] authored May 24, 2023
1 parent 71412d8 commit 1ae9fe9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/react/src/components/OverflowMenu/OverflowMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import mergeRefs from '../../tools/mergeRefs';
import { PrefixContext } from '../../internal/usePrefix';
import deprecate from '../../prop-types/deprecate';
import { IconButton } from '../IconButton';
import setupGetInstanceId from '../../tools/setupGetInstanceId';

const getInstanceId = setupGetInstanceId();

const on = (element, ...args) => {
element.addEventListener(...args);
Expand Down Expand Up @@ -94,6 +97,7 @@ export const getMenuOffset = (menuBody, direction, trigger, flip) => {

class OverflowMenu extends Component {
state = {};
instanceId = getInstanceId();

static propTypes = {
/**
Expand Down Expand Up @@ -529,13 +533,16 @@ class OverflowMenu extends Component {
})
);

const menuBodyId = `overflow-menu-${this.instanceId}__menu-body`;

const menuBody = (
<ul
className={overflowMenuOptionsClasses}
tabIndex="-1"
role="menu"
aria-label={ariaLabel || deprecatedAriaLabel}
onKeyDown={this.handleKeyPress}>
onKeyDown={this.handleKeyPress}
id={menuBodyId}>
{childrenWithProps}
</ul>
);
Expand Down Expand Up @@ -564,12 +571,15 @@ class OverflowMenu extends Component {

return (
<ClickListener onClickOutside={this.handleClickOutside}>
<span className={`${prefix}--overflow-menu__wrapper`}>
<span
className={`${prefix}--overflow-menu__wrapper`}
aria-owns={open ? menuBodyId : null}>
<IconButton
{...other}
type="button"
aria-haspopup
aria-expanded={this.state.open}
aria-controls={open ? menuBodyId : null}
className={overflowMenuClasses}
onClick={this.handleClick}
id={id}
Expand Down

0 comments on commit 1ae9fe9

Please sign in to comment.