-
Notifications
You must be signed in to change notification settings - Fork 0
focusTrap
Mike Byrne edited this page Jan 25, 2022
·
3 revisions
Traps keyboard tabbing focus within a node; so if you open a modal and you want the users tab focus to stay within that modal. Helps accessibility as users won't be able to tab focus outside of the active element.
- setFocusOnTarget (handled automatically)
- none
- nothing
Setup:
import { focusTrap } from './functions'
document.addEventListener('DOMContentLoaded', function(){
// listen for focus trap requests
focusTrap();
});To trap focus, where element is the node you want to trap focus within:
triggerCustomEvent(document, 'focus:trap', {
element: modalNode
});To un-trap focus:
triggerCustomEvent(document, 'focus:untrap');