Skip to content

Commit

Permalink
focus trap and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
broccolinisoup committed Sep 4, 2022
1 parent a899582 commit fcf6cf5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/UnderlineNav2/UnderlineNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {UnderlineNavContext} from './UnderlineNavContext'
import {ActionMenu} from '../ActionMenu'
import {ActionList} from '../ActionList'
import {useResizeObserver, ResizeObserverEntry} from '../hooks/useResizeObserver'
import {useFocusZone} from '../hooks/useFocusZone'
import {FocusKeys} from '@primer/behaviors'

type Overflow = 'auto' | 'menu' | 'scroll'
type ChildWidthArray = Array<{width: number}>
Expand Down Expand Up @@ -84,6 +86,16 @@ export const UnderlineNav = forwardRef(
) => {
const backupRef = useRef<HTMLElement>(null)
const newRef = (forwardedRef ?? backupRef) as MutableRefObject<HTMLElement>

// This might change if we decide tab through the navigation items rather than navigationg with the arrow keys.
// TBD. In the meantime keeping it as a menu with the focus trap.
// ref: https://www.w3.org/WAI/ARIA/apg/example-index/menubar/menubar-navigation.html (Keyboard Support)
useFocusZone({
containerRef: backupRef,
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
focusOutBehavior: 'wrap'
})

const styles = {
display: 'flex',
justifyContent: align === 'right' ? 'flex-end' : 'space-between',
Expand Down Expand Up @@ -144,7 +156,7 @@ export const UnderlineNav = forwardRef(
<UnderlineNavContext.Provider
value={{setChildrenWidth, selectedLink, setSelectedLink, afterSelect: afterSelectHandler, variant}}
>
<Box as={as} sx={merge(styles, sxProp)} aria-label={label} ref={newRef}>
<Box tabIndex={0} as={as} sx={merge(styles, sxProp)} aria-label={label} ref={newRef}>
<Box as="ul" sx={merge<BetterSystemStyleObject>(overflowStyles, ulStyles)}>
{responsiveProps.items}
</Box>
Expand Down
7 changes: 4 additions & 3 deletions src/UnderlineNav2/UnderlineNavItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {IconProps} from '@primer/octicons-react'
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import {UnderlineNavContext} from './UnderlineNavContext'
import CounterLabel from '../CounterLabel'
import {get} from '../constants'

// adopted from React.AnchorHTMLAttributes
type LinkProps = {
Expand Down Expand Up @@ -109,15 +110,15 @@ export const UnderlineNavItem = forwardRef(
'&:focus': {
outline: 0,
'& > div[data-component="wrapper"]': {
boxShadow: `inset 0 0 0 2px #0969da`
boxShadow: `inset 0 0 0 2px ${get('colors.accent.fg')}`
},
// where focus-visible is supported, remove the focus box-shadow
'&:not(:focus-visible) > div[data-component="wrapper"]': {
boxShadow: 'none'
}
},
'&:focus-visible > div[data-component="wrapper"]': {
boxShadow: `inset 0 0 0 2px #0969da`
boxShadow: `inset 0 0 0 2px ${get('colors.accent.fg')}`
},
// renders a visibly hidden "copy" of the label in bold, reserving box space for when label becomes bold on selected
'& span[data-content]::before': {
Expand All @@ -131,7 +132,7 @@ export const UnderlineNavItem = forwardRef(
'&::after': {
position: 'absolute',
right: '50%',
bottom: 'calc(50% - 23px)',
bottom: 0,
width: `calc(100% - 8px)`,
height: 2,
content: '""',
Expand Down

0 comments on commit fcf6cf5

Please sign in to comment.