File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ 'use client'
2+
13import Feed from '@/components/feed/Feed'
24import Footer from '@/components/footer/Footer'
35import Navigation from '@/components/navigation/Navigation'
6+ import { useFocusTrap } from '@/hooks/useFocusTrap'
47
58export default function Home ( ) {
9+ useFocusTrap ( )
10+
611 return (
712 < div className = "min-h-svh flex flex-col items-center justify-start bg-gradient-to-b from-stone-700 to-stone-800" >
813 < Navigation />
Original file line number Diff line number Diff line change 1+ 'use client'
2+
3+ import { useEffect } from 'react'
4+
5+ export const useFocusTrap = ( ) => {
6+ useEffect ( ( ) => {
7+ const focusTrap = ( e : KeyboardEvent ) => {
8+ if ( e . key !== 'Tab' ) {
9+ return
10+ }
11+
12+ const focusableElements = (
13+ Array . from (
14+ document . querySelectorAll ( 'a, button' )
15+ ) as HTMLButtonElement [ ]
16+ ) . filter ( ( item ) => ! item . disabled )
17+
18+ const firstFocusableElement = focusableElements [ 0 ]
19+ const lastFocusableElement = focusableElements . at ( - 1 )
20+
21+ if ( e . shiftKey ) {
22+ if ( document . activeElement === firstFocusableElement ) {
23+ e . preventDefault ( )
24+ lastFocusableElement ?. focus ( )
25+ }
26+ } else {
27+ if ( document . activeElement === lastFocusableElement ) {
28+ e . preventDefault ( )
29+ firstFocusableElement ?. focus ( )
30+ }
31+ }
32+ }
33+
34+ document . addEventListener ( 'keydown' , focusTrap )
35+
36+ return ( ) => {
37+ document . removeEventListener ( 'keydown' , focusTrap )
38+ }
39+ } )
40+ }
You can’t perform that action at this time.
0 commit comments