Traps focus within an element for accessibility
Prevents focus from leaving the specified element when Tab is pressed. Essential for modal dialogs and other components that require focus containment per WCAG 2.1.
The element to trap focus within
Cleanup function to remove event listeners
const cleanup = trapFocus({ element: modalElement, action: (event) => { // Close modal when Escape is pressed modal.close(); }, shadowDomContext: shadowRoot});// Later, cleanup when modal closescleanup(); Copy
const cleanup = trapFocus({ element: modalElement, action: (event) => { // Close modal when Escape is pressed modal.close(); }, shadowDomContext: shadowRoot});// Later, cleanup when modal closescleanup();
Traps focus within an element for accessibility
Prevents focus from leaving the specified element when Tab is pressed. Essential for modal dialogs and other components that require focus containment per WCAG 2.1.