Skip to content

Commit

Permalink
fix: improve tab-based navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHarland committed Dec 13, 2023
1 parent 165c34a commit 011afa0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function LayoutDefault(props: LayoutDefaultProps) {
<div />
)}
<div className={classes.children}>
<div id='skip-nav' />
<div id='skip-nav' tabIndex={-1} />
{children}
</div>
<div className={classes.footer}>{footer}</div>
Expand Down
17 changes: 6 additions & 11 deletions packages/next-ui/Navigation/components/NavigationOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export const NavigationOverlay = React.memo((props: NavigationOverlayProps) => {

useEffect(() => {
animating.set(true)

if (activeAndNotClosing) {
a11yFocusRef.current?.focus()
}
}, [activeAndNotClosing, animating])

const afterClose = useEventCallback(() => {
Expand Down Expand Up @@ -127,16 +131,7 @@ export const NavigationOverlay = React.memo((props: NavigationOverlayProps) => {
},
}}
>
<MotionDiv
layout
layoutDependency={selectionValue}
sx={{ display: 'grid' }}
onLayoutAnimationComplete={() => {
if (selectedLevel === 1 && a11yFocusRef.current) {
a11yFocusRef.current.focus()
}
}}
>
<MotionDiv layout layoutDependency={selectionValue} sx={{ display: 'grid' }}>
<Box
className={classes.header}
sx={(theme) => ({
Expand All @@ -160,7 +155,6 @@ export const NavigationOverlay = React.memo((props: NavigationOverlayProps) => {
sx={{ boxShadow: 'none', my: fabMarginY }}
size='responsive'
aria-label={i18n._(/* i18n */ 'Back')}
ref={a11yFocusRef}
>
<IconSvg src={iconChevronLeft} size='large' aria-hidden />
</Fab>
Expand All @@ -173,6 +167,7 @@ export const NavigationOverlay = React.memo((props: NavigationOverlayProps) => {
sx={{ boxShadow: 'none', my: fabMarginY }}
size='responsive'
aria-label={i18n._(/* i18n */ 'Close')}
ref={a11yFocusRef}
>
<IconSvg src={iconClose} size='large' aria-hidden />
</Fab>
Expand Down
12 changes: 12 additions & 0 deletions packages/next-ui/SkipLink/SkipLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ import { Trans } from '@lingui/react'
import { Link } from '@mui/material'

export function SkipLink() {
const setFocus = (e) => {
e.preventDefault()
globalThis.document.querySelector<HTMLDivElement>('#skip-nav')?.focus()
globalThis.document.querySelector<HTMLDivElement>('#skip-nav')?.scrollIntoView()
}

return (
<Link
href='#skip-nav'
tabIndex={0}
onClick={setFocus}
onKeyDown={(e) => {
if (e.key === 'Enter') {
setFocus(e)
}
}}
sx={(theme) => ({
position: 'absolute',
top: theme.page.vertical,
Expand Down

0 comments on commit 011afa0

Please sign in to comment.