Debian Codegraph @

node/focus-trap/index.d.ts

raw
1 import { CheckOptions as TabbableCheckOptions } from 'tabbable';
2
3 declare module 'focus-trap' {
4 export type FocusTargetValue = HTMLElement | SVGElement | string;
5 export type FocusTargetValueOrFalse = FocusTargetValue | false;
6
7 /**
8 * A DOM node, a selector string (which will be passed to
9 * `document.querySelector()` to find the DOM node), or a function that
10 * returns a DOM node.
11 */
12 export type FocusTarget = FocusTargetValue | (() => FocusTargetValue);
13
14 /**
15 * A DOM node, a selector string (which will be passed to
16 * `document.querySelector()` to find the DOM node), `false` to explicitly indicate
17 * an opt-out, or a function that returns a DOM node or `false`.
18 */
19 export type FocusTargetOrFalse =
20 | FocusTargetValueOrFalse
21 | (() => FocusTargetValueOrFalse);
22
23 type MouseEventToBoolean = (event: MouseEvent | TouchEvent) => boolean;
24 type KeyboardEventToBoolean = (event: KeyboardEvent) => boolean;
25
26 /** tabbable options supported by focus-trap. */
27 // eslint-disable-next-line @typescript-eslint/no-empty-object-type -- Reserving ability to add/remove properties in the future
28 export interface FocusTrapTabbableOptions extends TabbableCheckOptions {}
29
30 export interface Options {
31 /**
32 * A function that will be called **before** sending focus to the
33 * target element upon activation.
34 */
35 onActivate?: () => void;
36
37 /**
38 * A function that will be called **after** focus has been sent to the
39 * target element upon activation.
40 */
41 onPostActivate?: () => void;
42
43 /**
44 * A function that will be called immediately after the trap's state is updated to be paused.
45 */
46 onPause?: () => void;
47
48 /**
49 * A function that will be called after the trap has been completely paused and is no longer
50 * managing/trapping focus.
51 */
52 onPostPause?: () => void;
53
54 /**
55 * A function that will be called immediately after the trap's state is updated to be active
56 * again, but prior to updating its knowledge of what nodes are tabbable within its containers,
57 * and prior to actively managing/trapping focus.
58 */
59 onUnpause?: () => void;
60
61 /**
62 * A function that will be called after the trap has been completely unpaused and is once
63 * again managing/trapping focus.
64 */
65 onPostUnpause?: () => void;
66
67 /**
68 * A function for determining if it is safe to send focus to the focus trap
69 * or not.
70 *
71 * It should return a promise that only resolves once all the listed `containers`
72 * are able to receive focus.
73 *
74 * The purpose of this is to prevent early focus-trap activation on animated
75 * dialogs that fade in and out. When a dialog fades in, there is a brief delay
76 * between the activation of the trap and the trap element being focusable.
77 */
78 checkCanFocusTrap?: (
79 containers: Array<HTMLElement | SVGElement>
80 ) => Promise<void>;
81
82 /**
83 * A function that will be called **before** sending focus to the
84 * trigger element upon deactivation.
85 */
86 onDeactivate?: () => void;
87
88 /**
89 * A function that will be called after the trap is deactivated, after `onDeactivate`.
90 * If `returnFocus` was set, it will be called **after** focus has been sent to the trigger
91 * element upon deactivation; otherwise, it will be called after deactivation completes.
92 */
93 onPostDeactivate?: () => void;
94 /**
95 * A function for determining if it is safe to send focus back to the `trigger` element.
96 *
97 * It should return a promise that only resolves once `trigger` is focusable.
98 *
99 * The purpose of this is to prevent the focus being sent to an animated trigger element too early.
100 * If a trigger element fades in upon trap deactivation, there is a brief delay between the deactivation
101 * of the trap and when the trigger element is focusable.
102 *
103 * `trigger` will be either the node that had focus prior to the trap being activated,
104 * or the result of the `setReturnFocus` option, if configured.
105 *
106 * This handler is **not** called if the `returnFocusOnDeactivate` configuration option
107 * (or the `returnFocus` deactivation option) is falsy.
108 */
109 checkCanReturnFocus?: (trigger: HTMLElement | SVGElement) => Promise<void>;
110
111 /**
112 * By default, when a focus trap is activated the first element in the
113 * focus trap's tab order will receive focus. With this option you can
114 * specify a different element to receive that initial focus, or use `false`
115 * for no initially focused element at all.
116 *
117 * NOTE: Setting this option to `false` (or a function that returns `false`)
118 * will prevent the `fallbackFocus` option from being used.
119 *
120 * Setting this option to `undefined` (or a function that returns `undefined`)
121 * will result in the default behavior.
122 */
123 initialFocus?: FocusTargetOrFalse | undefined | (() => void);
124 /**
125 * By default, an error will be thrown if the focus trap contains no
126 * elements in its tab order. With this option you can specify a
127 * fallback element to programmatically receive focus if no other
128 * tabbable elements are found. For example, you may want a popover's
129 * `<div>` to receive focus if the popover's content includes no
130 * tabbable elements. *Make sure the fallback element has a negative
131 * `tabindex` so it can be programmatically focused.
132 *
133 * NOTE: If `initialFocus` is `false` (or a function that returns `false`),
134 * this function will not be called when the trap is activated, and no element
135 * will be initially focused. This function may still be called while the trap
136 * is active if things change such that there are no longer any tabbable nodes
137 * in the trap.
138 */
139 fallbackFocus?: FocusTarget;
140 /**
141 * Default: `true`. If `false`, when the trap is deactivated,
142 * focus will *not* return to the element that had focus before activation.
143 */
144 returnFocusOnDeactivate?: boolean;
145 /**
146 * By default, focus trap on deactivation will return to the element
147 * that was focused before activation.
148 */
149 setReturnFocus?:
150 | FocusTargetValueOrFalse
151 | ((
152 nodeFocusedBeforeActivation: HTMLElement | SVGElement
153 ) => FocusTargetValueOrFalse);
154 /**
155 * Default: `true`. If `false` or returns `false`, the `Escape` key will not trigger
156 * deactivation of the focus trap. This can be useful if you want
157 * to force the user to make a decision instead of allowing an easy
158 * way out. Note that if a function is given, it's only called if the ESC key
159 * was pressed.
160 */
161 escapeDeactivates?: boolean | KeyboardEventToBoolean;
162 /**
163 * If `true` or returns `true`, a click outside the focus trap will
164 * deactivate the focus trap and allow the click event to do its thing (i.e.
165 * to pass-through to the element that was clicked). This option **takes
166 * precedence** over `allowOutsideClick` when it's set to `true`, causing
167 * that option to be ignored. Default: `false`.
168 */
169 clickOutsideDeactivates?: boolean | MouseEventToBoolean;
170 /**
171 * If set and is or returns `true`, a click outside the focus trap will not
172 * be prevented, even when `clickOutsideDeactivates` is `false`. When
173 * `clickOutsideDeactivates` is `true`, this option is **ignored** (i.e.
174 * if it's a function, it will not be called). Use this option to control
175 * if (and even which) clicks are allowed outside the trap in conjunction
176 * with `clickOutsideDeactivates: false`. Default: `false`.
177 */
178 allowOutsideClick?: boolean | MouseEventToBoolean;
179 /**
180 * By default, focus() will scroll to the element if not in viewport.
181 * It can produce unintended effects like scrolling back to the top of a modal.
182 * If set to `true`, no scroll will happen.
183 */
184 preventScroll?: boolean;
185 /**
186 * Default: `true`. Delays the autofocus when the focus trap is activated.
187 * This prevents elements within the focusable element from capturing
188 * the event that triggered the focus trap activation.
189 */
190 delayInitialFocus?: boolean;
191 /**
192 * Default: `window.document`. Document where the focus trap will be active.
193 * This allows to use FocusTrap in an iFrame context.
194 */
195 document?: Document;
196
197 /**
198 * Specific tabbable options configurable on focus-trap.
199 */
200 tabbableOptions?: FocusTrapTabbableOptions;
201
202 /**
203 * Define the global trap stack. This makes it possible to share the same stack
204 * in multiple instances of `focus-trap` in the same page such that
205 * auto-activation/pausing of traps is properly coordinated among all instances
206 * as activating a trap when another is already active should result in the other
207 * being auto-paused. By default, each instance will have its own internal stack,
208 * leading to conflicts if they each try to trap the focus at the same time.
209 */
210 // eslint-disable-next-line no-use-before-define -- TypeScript handles circular dependencies automatically
211 trapStack?: Array<FocusTrap>;
212
213 /**
214 * Determines if the given keyboard event is a "tab forward" event that will move
215 * the focus to the next trapped element in tab order. Defaults to the `TAB` key.
216 * Use this to override the trap's behavior if you want to use arrow keys to control
217 * keyboard navigation within the trap, for example. Also see `isKeyBackward()` option.
218 */
219 isKeyForward?: KeyboardEventToBoolean;
220
221 /**
222 * Determines if the given keyboard event is a "tab backward" event that will move
223 * the focus to the previous trapped element in tab order. Defaults to the `SHIFT+TAB` key.
224 * Use this to override the trap's behavior if you want to use arrow keys to control
225 * keyboard navigation within the trap, for example. Also see `isKeyForward()` option.
226 */
227 isKeyBackward?: KeyboardEventToBoolean;
228 }
229
230 type ActivateOptions = Pick<
231 Options,
232 'onActivate' | 'onPostActivate' | 'checkCanFocusTrap'
233 >;
234 type PauseOptions = Pick<Options, 'onPause' | 'onPostPause'>;
235 type UnpauseOptions = Pick<Options, 'onUnpause' | 'onPostUnpause'>;
236
237 interface DeactivateOptions
238 extends Pick<
239 Options,
240 'onDeactivate' | 'onPostDeactivate' | 'checkCanReturnFocus'
241 > {
242 returnFocus?: boolean;
243 }
244
245 export interface FocusTrap {
246 active: boolean;
247 paused: boolean;
248 activate(activateOptions?: ActivateOptions): FocusTrap;
249 deactivate(deactivateOptions?: DeactivateOptions): FocusTrap;
250 pause(pauseOptions?: PauseOptions): FocusTrap;
251 unpause(unpauseOptions?: UnpauseOptions): FocusTrap;
252 updateContainerElements(
253 containerElements:
254 | HTMLElement
255 | SVGElement
256 | string
257 | Array<HTMLElement | SVGElement | string>
258 ): FocusTrap;
259 }
260
261 /**
262 * Returns a new focus trap on `element`.
263 *
264 * @param element
265 * The element to be the focus trap, or a selector that will be used to
266 * find the element.
267 */
268 export function createFocusTrap(
269 element:
270 | HTMLElement
271 | SVGElement
272 | string
273 | Array<HTMLElement | SVGElement | string>,
274 userOptions?: Options
275 ): FocusTrap;
276 }