UIComponentsReact
A region that keeps keyboard focus inside it while active, for a custom overlay or an editing area.
Built on the headless Focus Trap.See its API in Docs
Rendered by React 19.3.1
Installation
npx shadcn@4.23.0 add https://hamur.dev/r/react/focus-trap.jsonIt also adds utils. The app needs its setup first: Installation.
Usage
Wrap the content in FocusTrap. While active, Tab and Shift+Tab wrap at its edges, Escape asks to deactivate, and with modal (the default) the rest of the page is inert; focus returns where it was when the trap deactivates. modal={false} keeps only Tab inside. inside adds elements outside the region to the Tab cycle, and initialFocus and finalFocus pick where focus goes; they come from the headless Focus Trap. For a dialog, use the Dialog, which traps focus itself; the trap has no role and no name.
import { FocusTrap } from '@/components/ui/focus-trap'Examples
Default
A panel with a field and two buttons, trapped while open and closed with Escape.
The trap has no look of its own: the region takes focus itself only when it holds nothing tabbable, after a touch, or when the focused control goes away, so it draws no outline (WCAG 2.4.3 for the order focus moves in, 2.1.2 for the Escape way out). Its focus guards carry data-base-ui-focus-guard; an accessibility scanner that flags them as focusable but hidden can leave them out with that selector, for example axe's exclude: [['[data-base-ui-focus-guard]']]. This item takes Base UI's component ID as its slug.
API reference
The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Focus Trap's API reference.