Utilities

Focus Trap

The Focus Trap utility is a directive that traps focus within a specified element. This is useful for modals, dropdowns, and other components that require focus to be contained within a specific area.

Import

Import the FocusTrap primitives from ng-primitives/focus-trap.

import { NgpFocusTrap } from 'ng-primitives/focus-trap';

Usage

Assemble the focus-trap directives in your template.

<div ngpFocusTrap></div>

API Reference

The following directives are available to import from the ng-primitives/focus-trap package:

NgpFocusTrap

Data Attributes

Attribute Description
data-focus-trap Applied when focus is trapped.

Accessibility

Adheres to the WAI-ARIA design pattern.

Add autofocus to one or more usable elements to focus the first one when the focus trap activates; otherwise the first tabbable element is focused, or the focus trap container itself if there is none. If focus is already inside the focus trap when it activates, it stays there and autofocus is ignored. Turning ngpFocusTrapDisabled back to false does not move focus: move it into the trap yourself if it is outside.

Copyright © 2026 Angular Primitives

This site is powered by Netlify