use-focus-return automatically manages focus returning to last focused element when given condition is met. For example it is used in Modal component to restore focus after modal was closed.
Try closing modal with escape key and see how focus returns to button after modal transition finishes:
In most cases you would want to use this hook with use-focus-trap.
To use hook provide
shouldReturnFocus option is set to
false you can call returned function to focus last active element: