useFocusTrap
function useFocusTrap(
ref: RefObject<HTMLElement | null>,
active?: boolean, // default true
options?: FocusTrapOptions,
): void;
interface FocusTrapOptions {
restoreFocus?: boolean; // return focus to where it was when the trap stops, default true
}
Keeps keyboard focus inside an element while active is true, so keyboard and screen reader
users can't Tab out of an open modal into the page behind it.
- When it becomes active, it focuses the first focusable element inside, or the container itself
if there is none. If focus is already inside, for example on an
autoFocusfield, it stays. - Tab on the last element goes to the first, and Shift+Tab on the first goes to the last. Focus moving outside, for example by a click, is brought back.
- Disabled,
hiddenandinertelements are skipped, and so are elements hidden with CSS. - When it stops or unmounts, focus returns to the element that had it before, such as the
button that opened the dialog, unless
restoreFocusis false. - With a dialog open over another, only the newest trap acts; closing it hands focus back to the one below.
- It follows the ref after a render, so it traps a dialog that mounts after
activeturns true, or a new element the ref moves to.
Close the dialog on Escape with useHotkey, and pair it with
useScrollLock. A native <dialog> opened with showModal() traps focus
itself.
Live demo
File: notes.txt
Example
import { useRef } from "react";
import { useFocusTrap, useHotkey } from "react-honey-hooks";
function Modal({ onClose, children }: { onClose: () => void; children: React.ReactNode }) {
const ref = useRef<HTMLDivElement>(null);
useFocusTrap(ref);
useHotkey("Escape", onClose, { ignoreInputs: false });
return (
<div ref={ref} role="dialog" aria-modal="true">
{children}
<button onClick={onClose}>Close</button>
</div>
);
}