Skip to content

Commit

Permalink
PageHeader: A11y eng review remediations (#2900)
Browse files Browse the repository at this point in the history
* a11y remediations

* Add changeset

* Add tests for a11y remediations

* add docs for a11y remediations

* Update generated/components.json

* wrap console.log with __DEV__ and add a todo

* fix merge issue

* update docs

---------

Co-authored-by: broccolinisoup <broccolinisoup@users.noreply.github.com>
  • Loading branch information
broccolinisoup and broccolinisoup authored Feb 16, 2023
1 parent 03ebf41 commit 3768cd7
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-rivers-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

PageHeader: A11y eng review remediations
24 changes: 24 additions & 0 deletions docs/content/drafts/PageHeader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ import {PageHeader} from '@primer/react/drafts'

### With navigation slot

#### Utilizing a Navigation component

```jsx live drafts
<PageHeader>
<PageHeader.TitleArea>
Expand All @@ -145,6 +147,28 @@ import {PageHeader} from '@primer/react/drafts'
</PageHeader>
```

#### Utilizing a custom navigation

```jsx live drafts
<PageHeader>
<PageHeader.TitleArea>
<PageHeader.Title>Page Title</PageHeader.Title>
</PageHeader.TitleArea>
<PageHeader.Navigation as="nav" aria-label="Item list">
<Box as="ul" sx={{display: 'flex', gap: '8px', listStyle: 'none', paddingY: 0, paddingX: 3}} role="list">
<li>
<Link href="#" aria-current="page">
Item 1
</Link>
</li>
<li>
<Link href="#">Item 2</Link>
</li>
</Box>
</PageHeader.Navigation>
</PageHeader>
```

### With ContextArea

#### With parent link and actions (only visible on mobile)
Expand Down
34 changes: 25 additions & 9 deletions generated/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -3018,7 +3018,7 @@
]
},
{
"name": "PageHeader.ContextArea Children",
"name": "PageHeader.ParentLink",
"props": [
{
"name": "href",
Expand All @@ -3039,7 +3039,7 @@
]
},
{
"name": "PageHeader.ContextArea Children",
"name": "PageHeader.ContextBar",
"props": [
{
"name": "hidden",
Expand All @@ -3054,7 +3054,7 @@
]
},
{
"name": "PageHeader.ContextArea Children",
"name": "PageHeader.ContextAreaActions",
"props": [
{
"name": "hidden",
Expand Down Expand Up @@ -3090,7 +3090,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.LeadingAction",
"props": [
{
"name": "hidden",
Expand All @@ -3105,7 +3105,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.LeadingVisual",
"props": [
{
"name": "hidden",
Expand All @@ -3120,7 +3120,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.Title",
"props": [
{
"name": "hidden",
Expand All @@ -3140,7 +3140,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.TrailingVisual",
"props": [
{
"name": "hidden",
Expand All @@ -3155,7 +3155,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.TrailingAction",
"props": [
{
"name": "hidden",
Expand All @@ -3170,7 +3170,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.Actions",
"props": [
{
"name": "hidden",
Expand Down Expand Up @@ -3202,6 +3202,22 @@
{
"name": "PageHeader.Navigation",
"props": [
{
"name": "aria-label",
"type": "string",
"description": "The aria-label attribute for the navigaton component when it is rendered as a \"nav\" element."
},
{
"name": "aria-labelledby",
"type": "string",
"description": "The aria-labelledby attribute for the navigaton component when it is rendered as a \"nav\" element."
},
{
"name": "as",
"type": "div | nav",
"defaultValue": "\"div\"",
"description": "The HTML element used to render the navigation."
},
{
"name": "hidden",
"type": "| boolean | { narrow?: boolean regular?: boolean wide?: boolean }",
Expand Down
36 changes: 26 additions & 10 deletions src/PageHeader/PageHeader.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
]
},
{
"name": "PageHeader.ContextArea Children",
"name": "PageHeader.ParentLink",
"props": [
{
"name": "href",
Expand All @@ -65,7 +65,7 @@
]
},
{
"name": "PageHeader.ContextArea Children",
"name": "PageHeader.ContextBar",
"props": [
{
"name": "hidden",
Expand All @@ -80,7 +80,7 @@
]
},
{
"name": "PageHeader.ContextArea Children",
"name": "PageHeader.ContextAreaActions",
"props": [
{
"name": "hidden",
Expand Down Expand Up @@ -116,7 +116,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.LeadingAction",
"props": [
{
"name": "hidden",
Expand All @@ -131,7 +131,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.LeadingVisual",
"props": [
{
"name": "hidden",
Expand All @@ -146,7 +146,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.Title",
"props": [
{
"name": "hidden",
Expand All @@ -166,7 +166,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.TrailingVisual",
"props": [
{
"name": "hidden",
Expand All @@ -181,7 +181,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.TrailingAction",
"props": [
{
"name": "hidden",
Expand All @@ -196,7 +196,7 @@
]
},
{
"name": "PageHeader.TitleArea Children",
"name": "PageHeader.Actions",
"props": [
{
"name": "hidden",
Expand Down Expand Up @@ -228,6 +228,22 @@
{
"name": "PageHeader.Navigation",
"props": [
{
"name": "aria-label",
"type": "string",
"description": "The aria-label attribute for the navigaton component when it is rendered as a \"nav\" element."
},
{
"name": "aria-labelledby",
"type": "string",
"description": "The aria-labelledby attribute for the navigaton component when it is rendered as a \"nav\" element."
},
{
"name": "as",
"type": "div | nav",
"defaultValue": "\"div\"",
"description": "The HTML element used to render the navigation."
},
{
"name": "hidden",
"type": "| boolean | { narrow?: boolean regular?: boolean wide?: boolean }",
Expand All @@ -241,4 +257,4 @@
]
}
]
}
}
30 changes: 30 additions & 0 deletions src/PageHeader/PageHeader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ describe('PageHeader', () => {
expect(getByText('Trailing Action')).toHaveStyle('height: 3rem')
// add actions here
})
it('renders "aria-label" prop when Navigation is rendered as "nav" landmark', () => {
const {getByLabelText, getByText} = render(
<PageHeader>
<PageHeader.Navigation as="nav" aria-label="Custom">
Navigation
</PageHeader.Navigation>
</PageHeader>,
)
expect(getByLabelText('Custom')).toBeInTheDocument()
expect(getByText('Navigation')).toHaveAttribute('aria-label', 'Custom')
})
it('does not renders "aria-label" prop when Navigation is rendered as "div"', () => {
const {getByText} = render(
<PageHeader>
<PageHeader.Navigation aria-label="Custom">Navigation</PageHeader.Navigation>
</PageHeader>,
)
expect(getByText('Navigation')).not.toHaveAttribute('aria-label')
})
it('logs a warning when the Navigation component is rendered as "nav" but no "aria-label" or "aria-labelledby" prop is provided', () => {
const consoleSpy = jest.spyOn(global.console, 'warn').mockImplementation()
render(
<PageHeader>
<PageHeader.Navigation as="nav">Navigation</PageHeader.Navigation>
</PageHeader>,
)
expect(consoleSpy).toHaveBeenCalled()

consoleSpy.mockRestore()
})
})

checkStoriesForAxeViolations('examples', '../PageHeader/')
40 changes: 28 additions & 12 deletions src/PageHeader/PageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,7 @@ type LinkProps = Pick<
export type ParentLinkProps = React.PropsWithChildren<ChildrenPropTypes & LinkProps>

const ParentLink = React.forwardRef<HTMLAnchorElement, ParentLinkProps>(
(
{
children,
sx = {},
href,
'aria-label': ariaLabel = `Back to ${children}`,
as = 'a',
hidden = hiddenOnRegularAndWide,
},
ref,
) => {
({children, sx = {}, href, 'aria-label': ariaLabel, as = 'a', hidden = hiddenOnRegularAndWide}, ref) => {
return (
<>
<Link
Expand Down Expand Up @@ -437,10 +427,36 @@ const Description: React.FC<React.PropsWithChildren<ChildrenPropTypes>> = ({chil
)
}

export type NavigationProps = {
as?: 'nav' | 'div'
'aria-label'?: React.AriaAttributes['aria-label']
'aria-labelledby'?: React.AriaAttributes['aria-labelledby']
} & ChildrenPropTypes

// PageHeader.Navigation: The local navigation area of the header. Visible on all viewports
const Navigation: React.FC<React.PropsWithChildren<ChildrenPropTypes>> = ({children, sx = {}, hidden = false}) => {
const Navigation: React.FC<React.PropsWithChildren<NavigationProps>> = ({
children,
sx = {},
hidden = false,
as,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
}) => {
// TODO: use warning utility function when it is merged https://github.com/primer/react/pull/2901/
if (__DEV__) {
if (as === 'nav' && !ariaLabel && !ariaLabelledBy) {
// eslint-disable-next-line no-console
console.warn(
'Use `aria-label` or `aria-labelledby` prop to provide an accessible label to the `nav` landmark for assistive technology',
)
}
}
return (
<Box
as={as}
// Render `aria-label` and `aria-labelledby` only on `nav` elements
aria-label={as === 'nav' ? ariaLabel : undefined}
aria-labelledby={as === 'nav' ? ariaLabelledBy : undefined}
sx={merge<BetterSystemStyleObject>(
{
display: 'flex',
Expand Down
22 changes: 22 additions & 0 deletions src/PageHeader/features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ export const WithNavigationSlot = () => (
</Box>
)

export const WithCustomNavigation = () => (
<Box sx={{padding: 3}}>
<PageHeader>
<PageHeader.TitleArea>
<PageHeader.Title>Pull request title</PageHeader.Title>
</PageHeader.TitleArea>
<PageHeader.Navigation as="nav" aria-label="Item list">
<Box as="ul" sx={{display: 'flex', gap: '8px', listStyle: 'none', paddingY: 0, paddingX: 3}} role="list">
<li>
<Link href="#" aria-current="page">
Item 1
</Link>
</li>
<li>
<Link href="#">Item 2</Link>
</li>
</Box>
</PageHeader.Navigation>
</PageHeader>
</Box>
)

export const WithLeadingAndTrailingActions = () => (
<Box sx={{padding: 3}}>
<PageHeader>
Expand Down

0 comments on commit 3768cd7

Please sign in to comment.