Skip to content
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
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

- Added `id` prop to `Layout` and `Heading` for hash linking ([#4307](https://github.com/Shopify/polaris-react/pull/4307))
- Added `external` prop to `Navigation.Item` component ([#4310](https://github.com/Shopify/polaris-react/pull/4310))
- Added `ariaLabelledBy` props to `Navigation` component to allow a hidden label for accessibility ([#4343](https://github.com/Shopify/polaris-react/pull/4343))

### Bug fixes

Expand Down
5 changes: 4 additions & 1 deletion src/components/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface NavigationProps {
children?: React.ReactNode;
contextControl?: React.ReactNode;
onDismiss?(): void;
/** id of the element used as aria-labelledby */
ariaLabelledBy?: string;
}

export const Navigation: React.FunctionComponent<NavigationProps> & {
Expand All @@ -26,6 +28,7 @@ export const Navigation: React.FunctionComponent<NavigationProps> & {
contextControl,
location,
onDismiss,
ariaLabelledBy,
}: NavigationProps) {
const {logo} = useTheme();
const width = getWidth(logo, 104);
Expand Down Expand Up @@ -61,7 +64,7 @@ export const Navigation: React.FunctionComponent<NavigationProps> & {
return (
<NavigationContext.Provider value={context}>
<WithinContentContext.Provider value>
<nav className={styles.Navigation}>
<nav className={styles.Navigation} aria-labelledby={ariaLabelledBy}>
{mediaMarkup}
<Scrollable className={styles.PrimaryNavigation}>
{children}
Expand Down
32 changes: 32 additions & 0 deletions src/components/Navigation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,38 @@ This example showcases the many elements that can compose a navigation, especial
</Navigation>
```

### Navigation with aria-labelledby

This example shows how to add an aria-labelledby to add a hidden label to the `nav` element.

```jsx
<Navigation location="/" ariaLabelledBy="label-id">
<VisuallyHidden>
<p id="label-id">Hidden label</p>
</VisuallyHidden>
<Navigation.Section
items={[
{
url: '/path/to/place',
label: 'Home',
icon: HomeMajor,
},
{
url: '/path/to/place',
label: 'Orders',
icon: OrdersMajor,
badge: '15',
},
{
url: '/path/to/place',
label: 'Products',
icon: ProductsMajor,
},
]}
/>
</Navigation>
```

---

## Related components
Expand Down
10 changes: 10 additions & 0 deletions src/components/Navigation/tests/Navigation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ describe('<Navigation />', () => {
expect(navigation).toContainReactComponent(Image);
});

it('renders nav with aria-labelledby when passed as prop', () => {
const label = 'label-id';
const navigation = mountWithApp(
<Navigation location="/" ariaLabelledBy={label} />,
);
expect(navigation).toContainReactComponent('nav', {
'aria-labelledby': label,
});
});

describe('context', () => {
it('passes location context', () => {
const Child: React.SFC = (_props) => {
Expand Down