Description
focus-trap-react is a React component that traps keyboard focus inside one of its children, which is useful for an accessible modal. It is a thin wrapper around the focus-trap library and adds React-specific handling. The trap activates automatically when the component mounts, unless you change that, and it deactivates when the component unmounts. Props can activate, deactivate, pause and unpause the trap. The component expects exactly one child element, which can be an HTML element or another component that contains focusable elements, and it cannot be a Fragment, because it needs a reference to the underlying DOM element. The README requires React 18.0.0 or later. It notes that the library no longer sets propTypes or defaultProps, since React 19 removed them, and it suggests TypeScript or a runtime validation library instead. Support is aimed at desktop browsers in current versions of Chrome, Edge, Firefox, Safari and Opera, and Internet Explorer is no longer supported. It suits React developers building dialogs, drawers and other overlays that must keep keyboard users inside them.…
Highlights
- Traps focus inside a React child element
- Activates on mount and releases on unmount
- Props to pause and unpause the trap
- Requires React 18 or later
Ratings & Reviews
No ratings yet
Be the first to rate focus-trap-react.
People also visit
focus-trap-react FAQ
What does focus-trap-react do?
It keeps keyboard focus inside a React element, such as a modal, until the trap is deactivated.
What React version does it need?
React 18.0.0 or later, according to the README.
Can the child be a fragment?
No. The child must be a single element, because the component needs a reference to the underlying DOM element.
Which browsers are supported?
Desktop Chrome, Edge, Firefox, Safari and Opera, and not Internet Explorer. Mobile browsers are not officially tested.
Does it use propTypes?
No. The README says propTypes and defaultProps are no longer assigned, and suggests TypeScript or a runtime validation library.
What are the best focus-trap-react alternatives?
Popular alternatives to focus-trap-react include react-aria-modal, react-modal and react-hotkeys. See all focus-trap-react alternatives


















