useFocusTrap API
Related
Explore these related pages for additional context and usage patterns.
| Page | Category |
|---|---|
| useFocusTrap | System |
| Dialog | Disclosure |
| useClickOutside | System |
| useEventListener | System |
| useRovingFocus | System |
| useStack | Plugins |
Functions
useFocusTrap
(target: MaybeElementRef, options?: UseFocusTrapOptions) => UseFocusTrapReturnConfines Tab / Shift+Tab to the subtree of target.
tabbable
typeof tabbableOptions
active
MaybeRefOrGetter<boolean> | undefinedReactive activation source. When it flips true the trap engages and focuses into the root; when it flips false the trap releases and restores focus. Omit it entirely to drive the trap imperatively.
initial
false | { readonly value: HTMLElement | Element | SVGElement | import("/home/runner/work/0/0/node_modules/.pnpm/vue@3.5.42_typescript@6.0.3/node_modules/vue/dist/vue").ComponentPublicInstance | null | undefined; } | (() => HTMLElement | Element | SVGElement | import("/home/runner/work/0/0/node_modules/.pnpm/vue@3.5.42_typescript@6.0.3/node_modules/vue/dist/vue").ComponentPublicInstance | null | undefined) | HTMLElement | Element | SVGElement | import("/home/runner/work/0/0/node_modules/.pnpm/vue@3.5.42_typescript@6.0.3/node_modules/vue/dist/vue").ComponentPublicInstance | undefinedWhere focus lands when the trap engages.
- omitted — the first tabbable descendant, falling back to the root
- false — nothing is focused; the first Tab pulls focus in instead
- an element / ref / getter — that node is focused directly. If it cannot
take focus, the first tabbable descendant (or the root) is used instead
restore
boolean | undefinedReturn focus to the element that was focused before activation.
Skipped when focus already sits on an outside control by the time the trap
releases — closing a dialog must not steal focus back from whatever the
user just clicked. A blur to <body> still restores.
Default: true
listen
boolean | undefinedBind the capture-phase keydown listener on document.
Set false when a consumer-owned listener will call {@link UseFocusTrapReturn.onKeydown}
itself — otherwise the document listener has already wrapped by the time a
root handler runs. A listen: false trap is not pushed onto the nest stack.
Default: true
onEscape
((event: KeyboardEvent) => void) | undefinedInvoked on Escape while this trap is the last connected trap that has
onEscape and owns focus.
Opt-in and side-effect free: the trap never calls preventDefault() and
never deactivates itself. An inner Tab-only trap (no onEscape) does not
eat Escape — it falls through to the next connected trap that has a
handler. Gate on a useStack ticket when dismissal policy is overlay-stack,
not trap-stack.
Properties
Methods
activate
() => voidEngage the trap: capture the focused element for a later restore, bind containment, and focus into the root once it exists. No-op when already engaged, so it never overwrites the captured restore target.
onKeydown
(event: KeyboardEvent) => voidThe keydown handler. Bound on document in the capture phase while the
trap is engaged unless {@link UseFocusTrapOptions.listen} is false.