use-focus-trap
Traps focus inside given node
Import
Source
Docs
Package
Usage
use-focus-trap
traps focus at the given node, for example in modal, drawer or menu.
Node must include at least one focusable element. When the node unmounts, the focus trap is automatically released.
API
The hook accepts focus trap active state as a single argument:
The hook returns ref
that should be passed to the element:
Combine with other ref based hooks
To combine use-focus-trap
with other ref based hooks, use use-merged-ref hook:
Initial focus
By default, focus trap will move focus to the first interactive element.
To specify the element that should receive initial focus, add data-autofocus
attribute: