Skip to content

Commit

Permalink
feat(ui-shell): switch header nav role for side nav
Browse files Browse the repository at this point in the history
This change allows users of `<HeaderMenu>` and `<HeaderMenuItem>` to
specify their ARIA roles so the a11y structure won't be broken when
they are put in side nav for narrow screen.

Fixes carbon-design-system#3572.
  • Loading branch information
asudoh committed Dec 9, 2019
1 parent 347a5e1 commit f591d1a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
10 changes: 9 additions & 1 deletion packages/react/src/components/UIShell/HeaderMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class HeaderMenu extends React.Component {
*/
focusRef: PropTypes.func,

/**
* Optionally supply a role for the underlying <a> node. Useful for resetting
* <a> semantics.
*/
linkRole: PropTypes.string,

/**
* Optionally provide a tabIndex for the underlying menu button
*/
Expand All @@ -54,6 +60,7 @@ class HeaderMenu extends React.Component {
};

static defaultProps = {
linkRole: 'menuitem',
renderMenuContent: defaultRenderMenuContent,
};

Expand Down Expand Up @@ -157,6 +164,7 @@ class HeaderMenu extends React.Component {
'aria-labelledby': ariaLabelledBy,
className: customClassName,
children,
linkRole,
renderMenuContent: MenuContent,
menuLinkName,
} = this.props;
Expand Down Expand Up @@ -185,7 +193,7 @@ class HeaderMenu extends React.Component {
href="#"
onKeyDown={this.handleOnKeyDown}
ref={this.handleMenuButtonRef}
role="menuitem"
role={linkRole}
tabIndex={0}
{...accessibilityLabel}>
{menuLinkName}
Expand Down
14 changes: 12 additions & 2 deletions packages/react/src/components/UIShell/HeaderMenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Link, { LinkPropTypes } from './Link';
const { prefix } = settings;

const HeaderMenuItem = React.forwardRef(function HeaderMenuItem(
{ className, children, role, ...rest },
{ className, children, role, linkRole, ...rest },
ref
) {
return (
Expand All @@ -22,7 +22,7 @@ const HeaderMenuItem = React.forwardRef(function HeaderMenuItem(
{...rest}
className={`${prefix}--header__menu-item`}
ref={ref}
role="menuitem"
role={linkRole}
tabIndex={0}>
<span className={`${prefix}--text-truncate--end`}>{children}</span>
</Link>
Expand Down Expand Up @@ -53,6 +53,16 @@ HeaderMenuItem.propTypes = {
* <ul> semantics for menus.
*/
role: PropTypes.string,

/**
* Optionally supply a role for the underlying <a> node. Useful for resetting
* <a> semantics.
*/
linkRole: PropTypes.string,
};

HeaderMenuItem.defaultProps = {
linkRole: 'menuitem',
};

export default HeaderMenuItem;
51 changes: 39 additions & 12 deletions packages/react/src/components/UIShell/UIShell-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,19 @@ storiesOf('UI Shell', module)
isPersistent={false}>
<SideNavItems>
<HeaderSideNavItems>
<HeaderMenuItem href="#">Link 1</HeaderMenuItem>
<HeaderMenuItem href="#">Link 2</HeaderMenuItem>
<HeaderMenuItem href="#">Link 3</HeaderMenuItem>
<HeaderMenu aria-label="Link 4" menuLinkName="Link 4">
<HeaderMenuItem linkRole="link" href="#">
Link 1
</HeaderMenuItem>
<HeaderMenuItem linkRole="link" href="#">
Link 2
</HeaderMenuItem>
<HeaderMenuItem linkRole="link" href="#">
Link 3
</HeaderMenuItem>
<HeaderMenu
linkRole="link"
aria-label="Link 4"
menuLinkName="Link 4">
<HeaderMenuItem href="#">Sub-link 1</HeaderMenuItem>
<HeaderMenuItem href="#">Sub-link 2</HeaderMenuItem>
<HeaderMenuItem href="#">Sub-link 3</HeaderMenuItem>
Expand Down Expand Up @@ -267,10 +276,19 @@ storiesOf('UI Shell', module)
isPersistent={false}>
<SideNavItems>
<HeaderSideNavItems>
<HeaderMenuItem href="#">Link 1</HeaderMenuItem>
<HeaderMenuItem href="#">Link 2</HeaderMenuItem>
<HeaderMenuItem href="#">Link 3</HeaderMenuItem>
<HeaderMenu aria-label="Link 4" menuLinkName="Link 4">
<HeaderMenuItem linkRole="link" href="#">
Link 1
</HeaderMenuItem>
<HeaderMenuItem linkRole="link" href="#">
Link 2
</HeaderMenuItem>
<HeaderMenuItem linkRole="link" href="#">
Link 3
</HeaderMenuItem>
<HeaderMenu
linkRole="link"
aria-label="Link 4"
menuLinkName="Link 4">
<HeaderMenuItem href="#">Sub-link 1</HeaderMenuItem>
<HeaderMenuItem href="#">Sub-link 2</HeaderMenuItem>
<HeaderMenuItem href="#">Sub-link 3</HeaderMenuItem>
Expand Down Expand Up @@ -332,10 +350,19 @@ storiesOf('UI Shell', module)
expanded={isSideNavExpanded}>
<SideNavItems>
<HeaderSideNavItems hasDivider={true}>
<HeaderMenuItem href="#">Link 1</HeaderMenuItem>
<HeaderMenuItem href="#">Link 2</HeaderMenuItem>
<HeaderMenuItem href="#">Link 3</HeaderMenuItem>
<HeaderMenu aria-label="Link 4" menuLinkName="Link 4">
<HeaderMenuItem linkRole="link" href="#">
Link 1
</HeaderMenuItem>
<HeaderMenuItem linkRole="link" href="#">
Link 2
</HeaderMenuItem>
<HeaderMenuItem linkRole="link" href="#">
Link 3
</HeaderMenuItem>
<HeaderMenu
linkRole="link"
aria-label="Link 4"
menuLinkName="Link 4">
<HeaderMenuItem href="#">Sub-link 1</HeaderMenuItem>
<HeaderMenuItem href="#">Sub-link 2</HeaderMenuItem>
<HeaderMenuItem href="#">Sub-link 3</HeaderMenuItem>
Expand Down

0 comments on commit f591d1a

Please sign in to comment.