import { MaybeElementRef } from "../toElement/index.mjs"; import { FOCUSABLE, FocusTrapElement, tabbable } from "./tabbable.mjs"; import { MaybeRefOrGetter, Ref } from "vue"; //#region src/composables/useFocusTrap/index.d.ts interface UseFocusTrapOptions { /** * 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. * * @remarks * This is a *source*, not the state — `isActive` is the state. An imperative * `activate()` / `deactivate()` overrides the source until its next * transition. See {@link useFocusTrap} for the full precedence rule. * * @example * ```ts * useFocusTrap(panel, { active: () => dialog.isOpen.value }) * ``` */ active?: MaybeRefOrGetter; /** * 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 * * @example * ```ts * const search = useTemplateRef('search') * * useFocusTrap(panel, { initial: search }) // autofocus a specific control * useFocusTrap(panel, { initial: false }) // no autofocus at all * ``` */ initial?: false | Exclude; /** * 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 `` still restores. * * @default true * * @example * ```ts * useFocusTrap(panel, { restore: false }) * ``` */ restore?: boolean; /** * 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 * * @example * ```ts * const trap = useFocusTrap(panel, { active: isOpen, listen: false }) * * function onPanelKeydown (event: KeyboardEvent) { * if (editorHasFocus.value && event.key === 'Tab') return * * trap.onKeydown(event) * } * ``` */ listen?: boolean; /** * 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. * * @example * ```ts * const ticket = stack.register({ onDismiss: () => (isOpen.value = false) }) * * useFocusTrap(panel, { * active: isOpen, * onEscape: event => { * if (!ticket.globalTop.value) return * event.preventDefault() * isOpen.value = false * }, * }) * ``` */ onEscape?: (event: KeyboardEvent) => void; } interface UseFocusTrapReturn { /** * Whether the trap is currently engaged */ readonly isActive: Readonly>; /** * 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. * * @remarks * The document listener binds on the next flush, so a Tab dispatched * synchronously in the same tick as this call is not yet intercepted. Awaiting * a tick — or letting Vue drive the trap through `options.active` — avoids it. */ activate: () => void; /** * Release the trap and restore focus. No-op when not engaged. */ deactivate: () => void; /** * The keydown handler. Bound on `document` in the capture phase while the * trap is engaged unless {@link UseFocusTrapOptions.listen} is `false`. * * @example * ```ts * const trap = useFocusTrap(panel, { active: isOpen, listen: false }) * * function onPanelKeydown (event: KeyboardEvent) { * trap.onKeydown(event) * } * ``` */ onKeydown: (event: KeyboardEvent) => void; } /** * Confines Tab / Shift+Tab to the subtree of `target`. * * @param target The trap root. Accepts a template ref, getter, component instance, or raw element. Give it `tabindex="-1"` if it can ever contain zero tabbable descendants. * @param options Configuration options. * @returns The trap's reactive state and controls. * * @remarks * **Why the listener is on `document`.** A root-bound listener stops firing the * moment focus leaves the subtree — a backdrop click that blurs to ``, or * a stray programmatic `focus()` — and the trap is then dead with no way back. * Binding at the document in the capture phase means an escaped focus is * recovered on the next Tab, and containment survives an inner * `stopPropagation()`. * * **Only the boundaries are intercepted.** Mid-list Tab presses pass straight * through untouched, so an inner widget keeps full control of its own Tab * handling everywhere except the first and last stop. * * **Precedence.** `options.active` is a source; `isActive` is the state. The * source's transitions call `activate()` / `deactivate()`, while imperative * calls write the state directly and never write back to the source. Calling * `deactivate()` while the `active` getter still returns `true` therefore stays * deactivated until `active` next transitions false → true. * * **Ordering.** Candidates are collected in document order. Positive `tabindex` * values are not re-sorted into their real tab position — they are an * anti-pattern the trap declines to optimize for. * * **Shadow DOM and iframes.** Containment pierces open shadow roots, but * discovery cannot (`querySelectorAll` does not cross the boundary). If the last * tab stop lives inside a descendant's shadow root, focus can still leave on * Tab. The same applies to `