diff --git a/.changeset/weak-ears-search.md b/.changeset/weak-ears-search.md new file mode 100644 index 00000000000..870a79d439e --- /dev/null +++ b/.changeset/weak-ears-search.md @@ -0,0 +1,5 @@ +--- +"@primer/react": minor +--- + +Remove non-tab elements from `TabNav` focus zone diff --git a/src/TabNav.tsx b/src/TabNav.tsx index edcc02d459d..451f9ed0a4d 100644 --- a/src/TabNav.tsx +++ b/src/TabNav.tsx @@ -42,7 +42,8 @@ function TabNav({children, 'aria-label': ariaLabel, ...rest}: TabNavProps) { containerRef: customContainerRef, bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd, focusOutBehavior: 'wrap', - focusInStrategy: customStrategy + focusInStrategy: customStrategy, + focusableElementFilter: element => element.getAttribute('role') === 'tab' }) return ( }> diff --git a/src/__tests__/TabNav.test.tsx b/src/__tests__/TabNav.test.tsx index dbed266c8e9..9421df6aa59 100644 --- a/src/__tests__/TabNav.test.tsx +++ b/src/__tests__/TabNav.test.tsx @@ -17,6 +17,7 @@ describe('TabNav', () => { Middle + Focuseable Link Last @@ -102,12 +103,16 @@ describe('TabNav', () => { const user = userEvent.setup() const {getByText, getByRole} = HTMLRender(tabNavMarkup) const middleTab = getByText('Middle') + const link = getByText('Focuseable Link') const button = getByRole('button') await user.click(middleTab) expect(middleTab).toHaveFocus() await user.tab() + expect(link).toHaveFocus() + await user.tab() + expect(button).toHaveFocus() })