UNPKG

2.77 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23/**
24 * Utility to trap focus in a given root element, e.g. for modal components such
25 * as dialogs. The root should have at least one focusable child element,
26 * for setting initial focus when trapping focus.
27 * Also tracks the previously focused element, and restores focus to that
28 * element when releasing focus.
29 */
30export declare class FocusTrap {
31 private readonly root;
32 private readonly options;
33 private elFocusedBeforeTrapFocus;
34 constructor(root: HTMLElement, options?: FocusOptions);
35 /**
36 * Traps focus in `root`. Also focuses on either `initialFocusEl` if set;
37 * otherwises sets initial focus to the first focusable child element.
38 */
39 trapFocus(): void;
40 /**
41 * Releases focus from `root`. Also restores focus to the previously focused
42 * element.
43 */
44 releaseFocus(): void;
45 /**
46 * Wraps tab focus within `el` by adding two hidden sentinel divs which are
47 * used to mark the beginning and the end of the tabbable region. When
48 * focused, these sentinel elements redirect focus to the first/last
49 * children elements of the tabbable region, ensuring that focus is trapped
50 * within that region.
51 */
52 private wrapTabFocus;
53 /**
54 * Focuses on `initialFocusEl` if defined and a child of the root element.
55 * Otherwise, focuses on the first focusable child element of the root.
56 */
57 private focusInitialElement;
58 private getFocusableElements;
59 private createSentinel;
60}
61/** Customization options. */
62export interface FocusOptions {
63 initialFocusEl?: HTMLElement;
64 skipInitialFocus?: boolean;
65 skipRestoreFocus?: boolean;
66}