Skip to content

fix(TabNav): update focus strategy after initial focus #2468

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

Merged
merged 5 commits into from
Oct 25, 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/witty-elephants-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Change focus strategy for TabNav after initial focus is set
29 changes: 21 additions & 8 deletions src/TabNav.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classnames from 'classnames'
import {To} from 'history'
import React, {useRef} from 'react'
import React, {useRef, useState} from 'react'
import styled from 'styled-components'
import {get} from './constants'
import {FocusKeys, useFocusZone} from './hooks/useFocusZone'
Expand Down Expand Up @@ -30,21 +30,34 @@ export type TabNavProps = ComponentProps<typeof TabNavBase>

function TabNav({children, 'aria-label': ariaLabel, ...rest}: TabNavProps) {
const customContainerRef = useRef<HTMLElement>(null)
// TODO: revert tracking when `initialFocus` is set. This is a fix when TabNav
// is nested within another focus zone. This flag is used to indicate when
// focus has been initially set, this is useful for including the
// `aria-selected="true"` tab as the first interactive item.
//
// When set to `true`, this changes the behavior in `useFocusZone` to use
// the `'previous'` strategy which allows the tab to participate in nested
// focus zones without conflict
const [initialFocus, setInitialFocus] = useState(false)
const customStrategy = React.useCallback(() => {
if (customContainerRef.current) {
const tabs = Array.from(
customContainerRef.current.querySelectorAll<HTMLElement>('[role=tab][aria-selected=true]')
)
setInitialFocus(true)
return tabs[0]
}
}, [customContainerRef])
const {containerRef: navRef} = useFocusZone({
containerRef: customContainerRef,
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
focusOutBehavior: 'wrap',
focusInStrategy: customStrategy,
focusableElementFilter: element => element.getAttribute('role') === 'tab'
})
const {containerRef: navRef} = useFocusZone(
{
containerRef: customContainerRef,
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
focusOutBehavior: 'wrap',
focusInStrategy: initialFocus ? 'previous' : customStrategy,
focusableElementFilter: element => element.getAttribute('role') === 'tab'
},
[initialFocus]
)
return (
<TabNavBase {...rest} ref={navRef as React.RefObject<HTMLDivElement>}>
<TabNavNav aria-label={ariaLabel}>
Expand Down