Skip to main content
Vuetify0 Logo
Theme
Mode
Palettes
Accessibility
Vuetify One
Sign in to Vuetify One

Access premium tools across the Vuetify ecosystem — Bin, Play, Studio, and more.

Not a subscriber? See what's included

useFocusTrap API

Confines Tab and Shift+Tab to a root element, focuses into it on activate, and restores the previously focused element on deactivate.

Explore these related pages for additional context and usage patterns.

PageCategory
useFocusTrapSystem
DialogDisclosure
useClickOutsideSystem
useEventListenerSystem
useRovingFocusSystem
useStackPlugins

Functions

useFocusTrap

(target: MaybeElementRef, options?: UseFocusTrapOptions) => UseFocusTrapReturn

Confines Tab / Shift+Tab to the subtree of target.

tabbable

typeof tabbable

Options

active

MaybeRefOrGetter<boolean> | undefined

Reactive 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 | undefined

Where 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 | undefined

Return 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 | undefined

Bind 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) | undefined

Invoked 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

isActive

Readonly<Ref<boolean, boolean>>

Whether the trap is currently engaged

Methods

activate

() => void

Engage 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.

deactivate

() => void

Release the trap and restore focus. No-op when not engaged.

onKeydown

(event: KeyboardEvent) => void

The keydown handler. Bound on document in the capture phase while the trap is engaged unless {@link UseFocusTrapOptions.listen} is false.

Was this page helpful?

© 2016-1970 Vuetify, LLC
Services
Ctrl+/