focus-trap-vue is a Vue component designed to enhance web accessibility by programmatically trapping keyboard focus within a specified DOM element. This is crucial for UI patterns like modals, dialogs, and sidebars, preventing users of assistive technologies from accidentally tabbing out of the active context. The current stable version is 4.1.0, last published in August 2025, with regular patch and minor releases, indicating active maintenance and a stable API. It acts as a Vue-specific wrapper around the robust `focus-trap` library, inheriting its extensive focus management capabilities. Key differentiators include its seamless integration into Vue applications, support for both Vue 2 (via `@legacy` package) and Vue 3, and flexible control mechanisms via `active` prop, `v-model:active`, or direct method calls, while enforcing best practices for accessibility by leveraging its underlying peer dependency.
npm install focus-trap-vueVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic focus trap using `v-model:active` to control visibility and setting `initialFocus` to an input field within a modal-like cookie consent dialog for improved accessibility.
Review the `CHANGELOG.md` for `focus-trap-vue` and `focus-trap` to understand necessary code adaptations, especially if migrating from pre-4.0.0 versions or if IE support is critical. Ensure `focus-trap` v7 is installed.
Ensure `npm install focus-trap vue` (for Vue 3) or `npm install focus-trap vue@^2 focus-trap-vue@legacy` (for Vue 2) is run.
Ensure the `FocusTrap` component directly contains only a single HTML element or Vue component as its immediate child.
Always provide a valid CSS selector or a function returning a focusable DOM `Element` for the `initialFocus` prop. If targeting a container, ensure it has `tabindex="-1"`.
For Vue 2 projects, install `focus-trap-vue@legacy`. For Vue 3 projects, install `focus-trap-vue` (without `@legacy`).
Verify installation with `npm install focus-trap-vue` (or `@legacy`) and ensure correct import statement: `import { FocusTrap } from 'focus-trap-vue';`.Wrap all content within the `FocusTrap` component inside a single container `<div>` or custom component.
Ensure `FocusTrap` is either imported and registered locally in a component's `components` option, or globally registered via `app.component('FocusTrap', FocusTrap)` for Vue 3.For Vue 2 projects, use `npm install focus-trap-vue@legacy`. For Vue 3 projects, ensure `focus-trap-vue` is installed without `@legacy` and that your `vue` version meets the peer dependency requirement (e.g., `^3.0.0`).