Skip to content

Commit a057797

Browse files
fix(ui-shell): set aria-current when current class styling added (#19348)
* fix(ui-shell): set aria-current as true when current class styling added * fix(ui-shell): add test for aria-current state * fix(ui-shell): update storybook docs --------- Co-authored-by: Nikhil Tomar <[email protected]>
1 parent 3a3a4fd commit a057797

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

packages/react/src/components/UIShell/HeaderMenuItem.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,19 @@ const HeaderMenuItem = forwardRef(function HeaderMenuItem<
6060
if (isCurrentPage) {
6161
isActive = isCurrentPage;
6262
}
63+
// We set the current class only if `isActive` is passed in and we do
64+
// not have an `aria-current="page"` set for the breadcrumb item. When this
65+
// class is added we also set `aria-current` as `true`
66+
const hasCurrentClass = isActive && ariaCurrent !== 'page';
6367
const linkClassName = cx({
6468
[`${prefix}--header__menu-item`]: true,
65-
// We set the current class only if `isActive` is passed in and we do
66-
// not have an `aria-current="page"` set for the breadcrumb item
67-
[`${prefix}--header__menu-item--current`]:
68-
isActive && ariaCurrent !== 'page',
69+
[`${prefix}--header__menu-item--current`]: hasCurrentClass,
6970
});
7071
return (
7172
<li className={className} role={role}>
7273
<Link
7374
{...(rest as LinkProps<E>)}
74-
aria-current={ariaCurrent}
75+
aria-current={hasCurrentClass ? true : ariaCurrent}
7576
className={linkClassName}
7677
ref={ref}
7778
tabIndex={tabIndex}>
@@ -101,7 +102,7 @@ HeaderMenuItem.propTypes = {
101102
className: PropTypes.string,
102103

103104
/**
104-
* Applies selected styles to the item if a user sets this to true and `aria-current !== 'page'`.
105+
* If `true` and `aria-current !== 'page'`, applies selected styles to the item and sets `aria-current="true"`.
105106
*/
106107
isActive: PropTypes.bool,
107108

packages/react/src/components/UIShell/__tests__/HeaderMenuItem-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import React from 'react';
1010
import { HeaderMenuItem } from '../';
1111

1212
describe('HeaderMenuItem', () => {
13-
it('should set the current class based on isActive', () => {
13+
it('should set the current class and aria-current based on isActive', () => {
1414
render(
1515
<HeaderMenuItem data-testid="test" isActive>
1616
test
@@ -19,6 +19,7 @@ describe('HeaderMenuItem', () => {
1919
expect(screen.getByTestId('test')).toHaveClass(
2020
'cds--header__menu-item--current'
2121
);
22+
expect(screen.getByTestId('test')).toHaveAttribute('aria-current', 'true');
2223
});
2324

2425
it('should support a custom `className` prop on the outermost element', () => {

0 commit comments

Comments
 (0)