Skip to content

Commit

Permalink
Selected tab doesn't need to be <a> tag (#2158)
Browse files Browse the repository at this point in the history
* Selected tab doesn't need to be a tag

* Updates tests for new code and updates snapshot
  • Loading branch information
owenniblock committed Jun 30, 2022
1 parent c7bbd06 commit ac92de4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-pets-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Fixes issue when tabs are not links
7 changes: 5 additions & 2 deletions src/TabNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function TabNav({children, 'aria-label': ariaLabel, ...rest}: TabNavProps) {
const customContainerRef = useRef<HTMLElement>(null)
const customStrategy = React.useCallback(() => {
if (customContainerRef.current) {
const tabs = Array.from(customContainerRef.current.querySelectorAll<HTMLElement>('a[aria-selected=true]'))
const tabs = Array.from(
customContainerRef.current.querySelectorAll<HTMLElement>('[role=tab][aria-selected=true]')
)
return tabs[0]
}
}, [customContainerRef])
Expand Down Expand Up @@ -60,7 +62,8 @@ const TabNavLink = styled.a.attrs<StyledTabNavLinkProps>(props => ({
activeClassName: typeof props.to === 'string' ? 'selected' : '',
className: classnames(ITEM_CLASS, props.selected && SELECTED_CLASS, props.className),
role: 'tab',
'aria-selected': !!props.selected
'aria-selected': !!props.selected,
tabIndex: -1
}))<StyledTabNavLinkProps>`
padding: 8px 12px;
font-size: ${get('fontSizes.1')};
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/TabNav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('TabNav', () => {
const tabNavMarkup = (
<Box>
<TabNav>
<TabNav.Link id="first" href="#">
<TabNav.Link id="first" href="#" as="div">
First
</TabNav.Link>
<TabNav.Link id="middle" href="#" selected>
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/TabNav.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports[`TabNav TabNav.Link renders consistently 1`] = `
aria-selected={false}
className="c0 TabNav-item"
role="tab"
tabIndex={-1}
/>
`;

Expand Down

0 comments on commit ac92de4

Please sign in to comment.