Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce correct semantics for TabNav #2125

Merged
merged 6 commits into from
Jun 21, 2022
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
5 changes: 5 additions & 0 deletions .changeset/poor-wombats-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Adds roles of tablist and tab to the TabNav component, required rearranging the HTML elements to be semantically correct
16 changes: 11 additions & 5 deletions src/TabNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ const ITEM_CLASS = 'TabNav-item'
const SELECTED_CLASS = 'selected'

const TabNavBase = styled.div<SxProp>`
margin-top: 0;
border-bottom: 1px solid ${get('colors.border.default')};
${sx}
`

const TabNavBody = styled.nav`
const TabNavTabList = styled.div`
display: flex;
margin-bottom: -1px;
overflow: auto;
`

const TabNavNav = styled.nav`
margin-top: 0;
border-bottom: 1px solid ${get('colors.border.default')};
`

export type TabNavProps = ComponentProps<typeof TabNavBase>

function TabNav({children, 'aria-label': ariaLabel, ...rest}: TabNavProps) {
return (
<TabNavBase {...rest}>
<TabNavBody aria-label={ariaLabel}>{children}</TabNavBody>
<TabNavNav aria-label={ariaLabel}>
<TabNavTabList role="tablist">{children}</TabNavTabList>
</TabNavNav>
</TabNavBase>
)
}
Expand All @@ -39,7 +44,8 @@ type StyledTabNavLinkProps = {

const TabNavLink = styled.a.attrs<StyledTabNavLinkProps>(props => ({
activeClassName: typeof props.to === 'string' ? 'selected' : '',
className: classnames(ITEM_CLASS, props.selected && SELECTED_CLASS, props.className)
className: classnames(ITEM_CLASS, props.selected && SELECTED_CLASS, props.className),
role: 'tab'
}))<StyledTabNavLinkProps>`
padding: 8px 12px;
font-size: ${get('fontSizes.1')};
Expand Down
22 changes: 14 additions & 8 deletions src/__tests__/__snapshots__/TabNav.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,11 @@ exports[`TabNav TabNav.Link renders consistently 1`] = `

<a
className="c0 TabNav-item"
role="tab"
/>
`;

exports[`TabNav renders consistently 1`] = `
.c0 {
margin-top: 0;
border-bottom: 1px solid #d0d7de;
}

.c1 {
display: -webkit-box;
display: -webkit-flex;
Expand All @@ -64,11 +60,21 @@ exports[`TabNav renders consistently 1`] = `
overflow: auto;
}

.c0 {
margin-top: 0;
border-bottom: 1px solid #d0d7de;
}

<div
className="c0"
className=""
>
<nav
className="c1"
/>
className="c0"
>
<div
className="c1"
role="tablist"
/>
</nav>
</div>
`;