use-focus-trap
Traps focus inside given element
Source
Docs
PackageIcon
Usage
The use-focus-trap hook traps focus at the given node, for example in a modal, drawer, or menu.
The 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 the use-merged-ref hook:
Initial focus
By default, the focus trap will move focus to the first interactive element.
To specify the element that should receive initial focus, add the data-autofocus attribute: