|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef, useCallback } from "react"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Hook for trapping focus within a container |
| 7 | + * MUST: Manage focus (trap, move, and return) per APG patterns |
| 8 | + */ |
| 9 | +export function useFocusTrap<T extends HTMLElement>(active: boolean = true) { |
| 10 | + const containerRef = useRef<T>(null); |
| 11 | + const previousActiveElement = useRef<HTMLElement | null>(null); |
| 12 | + |
| 13 | + // Get all focusable elements within the container |
| 14 | + const getFocusableElements = useCallback(() => { |
| 15 | + if (!containerRef.current) return []; |
| 16 | + |
| 17 | + const selector = [ |
| 18 | + 'a[href]', |
| 19 | + 'button:not([disabled])', |
| 20 | + 'input:not([disabled])', |
| 21 | + 'select:not([disabled])', |
| 22 | + 'textarea:not([disabled])', |
| 23 | + '[tabindex]:not([tabindex="-1"])', |
| 24 | + ].join(','); |
| 25 | + |
| 26 | + return Array.from( |
| 27 | + containerRef.current.querySelectorAll<HTMLElement>(selector) |
| 28 | + ).filter((el) => { |
| 29 | + // Filter out elements that are not visible |
| 30 | + return el.offsetParent !== null; |
| 31 | + }); |
| 32 | + }, []); |
| 33 | + |
| 34 | + // Handle Tab key to trap focus |
| 35 | + const handleKeyDown = useCallback( |
| 36 | + (event: KeyboardEvent) => { |
| 37 | + if (!active || event.key !== 'Tab') return; |
| 38 | + |
| 39 | + const focusableElements = getFocusableElements(); |
| 40 | + if (focusableElements.length === 0) return; |
| 41 | + |
| 42 | + const firstElement = focusableElements[0]; |
| 43 | + const lastElement = focusableElements[focusableElements.length - 1]; |
| 44 | + |
| 45 | + if (event.shiftKey) { |
| 46 | + // Shift + Tab: go to last element if on first |
| 47 | + if (document.activeElement === firstElement) { |
| 48 | + event.preventDefault(); |
| 49 | + lastElement.focus(); |
| 50 | + } |
| 51 | + } else { |
| 52 | + // Tab: go to first element if on last |
| 53 | + if (document.activeElement === lastElement) { |
| 54 | + event.preventDefault(); |
| 55 | + firstElement.focus(); |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | + [active, getFocusableElements] |
| 60 | + ); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (!active) return; |
| 64 | + |
| 65 | + // Store the currently focused element |
| 66 | + previousActiveElement.current = document.activeElement as HTMLElement; |
| 67 | + |
| 68 | + // Focus the first focusable element in the container |
| 69 | + const focusableElements = getFocusableElements(); |
| 70 | + if (focusableElements.length > 0) { |
| 71 | + focusableElements[0].focus(); |
| 72 | + } |
| 73 | + |
| 74 | + // Add event listener for Tab key |
| 75 | + document.addEventListener('keydown', handleKeyDown); |
| 76 | + |
| 77 | + return () => { |
| 78 | + document.removeEventListener('keydown', handleKeyDown); |
| 79 | + |
| 80 | + // Return focus to the previously focused element |
| 81 | + if (previousActiveElement.current) { |
| 82 | + previousActiveElement.current.focus(); |
| 83 | + } |
| 84 | + }; |
| 85 | + }, [active, getFocusableElements, handleKeyDown]); |
| 86 | + |
| 87 | + return containerRef; |
| 88 | +} |
0 commit comments