use-focus-return
Captures last focused element on the page and returns focus to it once the condition is met
Import
Source
Docs
Package
Usage
use-focus-return
automatically returns focus to the last focused element when a given condition is met.
For example, it is used in Modal component to restore focus after the modal was closed.
Close the modal with the Escape
key and see how focus returns to the button after the modal closes:
In most cases, you should use this hook with use-focus-trap.
If shouldReturnFocus
option is set to false
you can call returned function to focus last active element: