Skip to content

Commit ac92de4

Browse files
authored
Selected tab doesn't need to be <a> tag (#2158)
* Selected tab doesn't need to be a tag * Updates tests for new code and updates snapshot
1 parent c7bbd06 commit ac92de4

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

.changeset/thick-pets-shop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Fixes issue when tabs are not links

src/TabNav.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ function TabNav({children, 'aria-label': ariaLabel, ...rest}: TabNavProps) {
3232
const customContainerRef = useRef<HTMLElement>(null)
3333
const customStrategy = React.useCallback(() => {
3434
if (customContainerRef.current) {
35-
const tabs = Array.from(customContainerRef.current.querySelectorAll<HTMLElement>('a[aria-selected=true]'))
35+
const tabs = Array.from(
36+
customContainerRef.current.querySelectorAll<HTMLElement>('[role=tab][aria-selected=true]')
37+
)
3638
return tabs[0]
3739
}
3840
}, [customContainerRef])
@@ -60,7 +62,8 @@ const TabNavLink = styled.a.attrs<StyledTabNavLinkProps>(props => ({
6062
activeClassName: typeof props.to === 'string' ? 'selected' : '',
6163
className: classnames(ITEM_CLASS, props.selected && SELECTED_CLASS, props.className),
6264
role: 'tab',
63-
'aria-selected': !!props.selected
65+
'aria-selected': !!props.selected,
66+
tabIndex: -1
6467
}))<StyledTabNavLinkProps>`
6568
padding: 8px 12px;
6669
font-size: ${get('fontSizes.1')};

src/__tests__/TabNav.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('TabNav', () => {
1313
const tabNavMarkup = (
1414
<Box>
1515
<TabNav>
16-
<TabNav.Link id="first" href="#">
16+
<TabNav.Link id="first" href="#" as="div">
1717
First
1818
</TabNav.Link>
1919
<TabNav.Link id="middle" href="#" selected>

src/__tests__/__snapshots__/TabNav.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ exports[`TabNav TabNav.Link renders consistently 1`] = `
4848
aria-selected={false}
4949
className="c0 TabNav-item"
5050
role="tab"
51+
tabIndex={-1}
5152
/>
5253
`;
5354

0 commit comments

Comments
 (0)