Introducing PrimeReact v11-alpha 🎉Discover Now
styledMisc

FocusTrap

Focus Trap keeps focus within a certain DOM element while tabbing.

Register
preview

Usage#

import { FocusTrap } from '@primereact/ui/focustrap';
<FocusTrap></FocusTrap>

Examples#

Basic#

Keeps keyboard focus within a defined region for accessibility.

Forgot Password
basic-demo

Trapped#

The trapped prop controls whether focus is confined within the container. Set to false to release focus and allow normal tab navigation.

disabled-demo

Initial Focus#

Use initialFocusRef to direct focus to a specific element when the trap activates. Alternatively, add data-autofocus to a focusable element to mark the initial focus target.

initial-focus-demo

On Escape#

The onEscape callback fires when the Escape key is pressed inside the trap, useful for deactivating the trap or closing overlays.

Press Escape to release focus.

on-escape-demo

API#

Sub-Components#

See Primitive API for FocusTrap component documentation.

Hooks#

See Headless API for useFocusTrap hook documentation.

Accessibility#

See FocusTrap Primitive for WAI-ARIA compliance details and keyboard support.