focus-trap 6.5.0 → 6.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -0
- package/README.md +31 -13
- package/dist/focus-trap.esm.js +102 -51
- package/dist/focus-trap.esm.js.map +1 -1
- package/dist/focus-trap.esm.min.js +2 -2
- package/dist/focus-trap.esm.min.js.map +1 -1
- package/dist/focus-trap.js +102 -51
- package/dist/focus-trap.js.map +1 -1
- package/dist/focus-trap.min.js +2 -2
- package/dist/focus-trap.min.js.map +1 -1
- package/dist/focus-trap.umd.js +105 -54
- package/dist/focus-trap.umd.js.map +1 -1
- package/dist/focus-trap.umd.min.js +2 -2
- package/dist/focus-trap.umd.min.js.map +1 -1
- package/index.d.ts +36 -11
- package/index.js +114 -52
- package/package.json +30 -28
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,61 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 6.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 893dd2c: Add `document` option to support focus traps inside `<iframe>` elements (#97)
|
|
8
|
+
- 244f0c1: Extend the `setReturnFocus` option to receive a reference to the element that had focus prior to the trap being activated when a function is specified. Additionally, the function can now return `false` to leave focus where it is at the time of deactivation. (#485)
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- 60162eb: Fix bug where `KeyboardEvent` was not being passed to `escapeDeactivates` option when it's a function (#498)
|
|
13
|
+
- 7b6abfa: Fix how focus-trap determines the event's target, which was preventing traps inside open shadow DOMs from working properly (#496)
|
|
14
|
+
- 14b0ee8: Fix `initialFocus` option not supporting function returning `false` as documented (#490)
|
|
15
|
+
|
|
16
|
+
## 6.6.1
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- 24063d7: Update tabbable to v5.2.1 to get bug fix for disabled fieldsets.
|
|
21
|
+
|
|
22
|
+
## 6.6.0
|
|
23
|
+
|
|
24
|
+
### Minor Changes
|
|
25
|
+
|
|
26
|
+
- 281e66c: Add option to allow no initial focus when trap activates via `initialFocus: false`
|
|
27
|
+
|
|
28
|
+
There may be cases where we don't want to focus the first tabbable element when a focus trap activates.
|
|
29
|
+
|
|
30
|
+
Examples use-cases:
|
|
31
|
+
|
|
32
|
+
- Modals/dialogs
|
|
33
|
+
- On mobile devices where "tabbing" doesn't make sense without a connected Bluetooth keyboard
|
|
34
|
+
|
|
35
|
+
In addition, this change ensures that any element inside the trap manually focused outside of `focus-trap` code will be brought back in focus if focus is somehow found outside of the trap.
|
|
36
|
+
|
|
37
|
+
Example usage:
|
|
38
|
+
|
|
39
|
+
When the trap activates, there will be no initially focused element inside the new trap.
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
const focusTrap = createFocusTrap('#some-container', {
|
|
43
|
+
initialFocus: false,
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- 75be463: `escapeDeactivates` can now be either a boolean (as before) or a function that takes an event and returns a boolean.
|
|
48
|
+
|
|
49
|
+
### Patch Changes
|
|
50
|
+
|
|
51
|
+
- e2294f0: Fix race condition when activating a second trap where initial focus in the second trap may be thwarted because pausing of first trap clears the `delayInitialFocus` timer created for the second trap before during its activation sequence.
|
|
52
|
+
|
|
53
|
+
## 6.5.1
|
|
54
|
+
|
|
55
|
+
### Patch Changes
|
|
56
|
+
|
|
57
|
+
- c38bf3f: onPostDeactivate should always be called even if returnFocus/OnDeactivate is disabled.
|
|
58
|
+
|
|
3
59
|
## 6.5.0
|
|
4
60
|
|
|
5
61
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# focus-trap [](https://github.com/focus-trap/focus-trap/actions?query=workflow:CI+branch:master) [](./LICENSE)
|
|
2
2
|
|
|
3
3
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
4
|
-
[](#contributors)
|
|
5
5
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
6
6
|
|
|
7
7
|
Trap focus within a DOM node.
|
|
@@ -28,7 +28,7 @@ When the focus trap is deactivated, this is what should happen:
|
|
|
28
28
|
- Focus is passed to *whichever element had focus when the trap was activated* (e.g. the button that opened the modal or menu).
|
|
29
29
|
- Tabbing and clicking behave normally everywhere.
|
|
30
30
|
|
|
31
|
-
[Check out the demos.](http://focus-trap.github.io/focus-trap/
|
|
31
|
+
[Check out the demos.](http://focus-trap.github.io/focus-trap/)
|
|
32
32
|
|
|
33
33
|
For more advanced usage (e.g. focus traps within focus traps), you can also pause a focus trap's behavior without deactivating it entirely, then unpause at will.
|
|
34
34
|
|
|
@@ -86,19 +86,33 @@ Returns a new focus trap on `element` (one or more "containers" of tabbable node
|
|
|
86
86
|
- **onPostActivate** `{() => void}`: A function that will be called **after** sending focus to the target element upon activation.
|
|
87
87
|
- **checkCanFocusTrap** `{(containers: Array<HTMLElement | SVGElement>) => Promise<void>}`: Animated dialogs have a small delay between when `onActivate` is called and when the focus trap is focusable. `checkCanFocusTrap` expects a promise to be returned. When that promise settles (resolves or rejects), focus will be sent to the first tabbable node (in tab order) in the focus trap (or the node configured in the `initialFocus` option). Due to the lack of Promise support, `checkCanFocusTrap` is not supported in IE unless you provide a Promise polyfill.
|
|
88
88
|
- **onDeactivate** `{() => void}`: A function that will be called **before** returning focus to the node that had focus prior to activation (or configured with the `setReturnFocus` option) upon deactivation.
|
|
89
|
-
- **onPostDeactivate** `{() => void}`: A function that will be called **after** returning focus to the node that had focus prior to activation (or configured with the `setReturnFocus` option) upon deactivation.
|
|
89
|
+
- **onPostDeactivate** `{() => void}`: A function that will be called after the trap is deactivated, after `onDeactivate`. If the `returnFocus` deactivation option was set, it will be called **after** returning focus to the node that had focus prior to activation (or configured with the `setReturnFocus` option) upon deactivation; otherwise, it will be called after deactivation completes.
|
|
90
90
|
- **checkCanReturnFocus** `{(trigger: HTMLElement | SVGElement) => Promise<void>}`: An animated trigger button will have a small delay between when `onDeactivate` is called and when the focus is able to be sent back to the trigger. `checkCanReturnFocus` expects a promise to be returned. When that promise settles (resolves or rejects), focus will be sent to to the node that had focus prior to the activation of the trap (or the node configured in the `setReturnFocus` option). Due to the lack of Promise support, `checkCanReturnFocus` is not supported in IE unless you provide a Promise polyfill.
|
|
91
|
-
- **initialFocus** `{HTMLElement | SVGElement | string | () => HTMLElement | SVGElement}`: By default, when a focus trap is activated the first element in the focus trap's tab order will receive focus. With this option you can specify a different element to receive that initial focus. Can be a DOM node, or a selector string (which will be passed to `document.querySelector()` to find the DOM node), or a function that returns a DOM node.
|
|
91
|
+
- **initialFocus** `{HTMLElement | SVGElement | string | false | (() => HTMLElement | SVGElement | false)}`: By default, when a focus trap is activated the first element in the focus trap's tab order will receive focus. With this option you can specify a different element to receive that initial focus. Can be a DOM node, or a selector string (which will be passed to `document.querySelector()` to find the DOM node), or a function that returns a DOM node. You can also set this option to `false` (or to a function that returns `false`) to prevent any initial focus at all when the trap activates.
|
|
92
|
+
- 💬 Setting this option to `false` (or a function that returns `false`) will prevent the `fallbackFocus` option from being used.
|
|
93
|
+
- ⚠️ See warning below about **Shadow DOM** and selector strings.
|
|
92
94
|
- **fallbackFocus** `{HTMLElement | SVGElement | string | () => HTMLElement | SVGElement}`: By default, an error will be thrown if the focus trap contains no elements in its tab order. With this option you can specify a fallback element to programmatically receive focus if no other tabbable elements are found. For example, you may want a popover's `<div>` to receive focus if the popover's content includes no tabbable elements. *Make sure the fallback element has a negative `tabindex` so it can be programmatically focused.* The option value can be a DOM node, a selector string (which will be passed to `document.querySelector()` to find the DOM node), or a function that returns a DOM node.
|
|
93
|
-
-
|
|
95
|
+
- 💬 If `initialFocus` is `false` (or a function that returns `false`), this function will not be called when the trap is activated, and no element will be initially focused. This function may still be called while the trap is active if things change such that there are no longer any tabbable nodes in the trap.
|
|
96
|
+
- ⚠️ See warning below about **Shadow DOM** and selector strings.
|
|
97
|
+
- **escapeDeactivates** `{boolean} | (e: KeyboardEvent) => boolean)`: Default: `true`. If `false` or returns `false`, the `Escape` key will not trigger deactivation of the focus trap. This can be useful if you want to force the user to make a decision instead of allowing an easy way out. Note that if a function is given, it's only called if the ESC key was pressed.
|
|
94
98
|
- **clickOutsideDeactivates** `{boolean | (e: MouseEvent | TouchEvent) => boolean}`: If `true` or returns `true`, a click outside the focus trap will deactivate the focus trap and allow the click event to do its thing (i.e. to pass-through to the element that was clicked). This option **takes precedence** over `allowOutsideClick` when it's set to `true`. Default: `false`.
|
|
95
99
|
- ⚠️ If you're using a password manager such as 1Password, where the app adds a clickable icon to all fillable fields, you should avoid using this option, and instead use the `allowOutsideClick` option to better control exactly when the focus trap can be deactivated. The clickable icons are usually positioned absolutely, floating on top of the fields, and therefore _not_ part of the container the trap is managing. When using the `clickOutsideDeactivates` option, clicking on a field's 1Password icon will likely cause the trap to be unintentionally deactivated.
|
|
96
100
|
- **allowOutsideClick** `{boolean | (e: MouseEvent | TouchEvent) => boolean}`: If set and is or returns `true`, a click outside the focus trap will not be prevented, even when `clickOutsideDeactivates` is `false`. When `clickOutsideDeactivates` is `true`, this option is **ignored** (i.e. if it's a function, it will not be called). Use this option to control if (and even which) clicks are allowed outside the trap in conjunction with `clickOutsideDeactivates: false`. Default: `false`.
|
|
97
101
|
- ⚠️ If this is a function, it will be called **twice** on every click: First on `mousedown` (or `touchstart` on mobile), and then on the actual `click` if the function returned `true` on the first event. Be sure to check the event type if the double call is an issue in your code.
|
|
98
102
|
- **returnFocusOnDeactivate** `{boolean}`: Default: `true`. If `false`, when the trap is deactivated, focus will *not* return to the element that had focus before activation.
|
|
99
|
-
- **setReturnFocus** `{HTMLElement | SVGElement | string | () => HTMLElement | SVGElement}`: By default,
|
|
103
|
+
- **setReturnFocus** `{HTMLElement | SVGElement | string | (previousActiveElement: HTMLElement | SVGElement) => HTMLElement | SVGElement | false}`: By default, on **deactivation**, if `returnFocusOnDeactivate=true` (or if `returnFocus=true` in the [deactivation options](#trapdeactivatedeactivateoptions)), focus will be returned to the element that was focused just before activation. With this option, you can specify another element to programmatically receive focus after deactivation. It can be a DOM node, a selector string (which will be passed to `document.querySelector()` to find the DOM node **upon deactivation**), or a function that returns a DOM node to call **upon deactivation** (i.e. the selector and function options are only executed at the time the trap is deactivated), or `false` to leave focus where it is at the time of deactivation.
|
|
104
|
+
- 💬 Using the selector or function options is a good way to return focus to a DOM node that may not even exist at the time the trap is activated.
|
|
105
|
+
- ⚠️ See warning below about **Shadow DOM** and selector strings.
|
|
100
106
|
- **preventScroll** `{boolean}`: By default, focus() will scroll to the element if not in viewport. It can produce unintended effects like scrolling back to the top of a modal. If set to `true`, no scroll will happen.
|
|
101
107
|
- **delayInitialFocus** `{boolean}`: Default: `true`. Delays the autofocus to the next execution frame when the focus trap is activated. This prevents elements within the focusable element from capturing the event that triggered the focus trap activation.
|
|
108
|
+
- **document** {Document}: Default: `window.document`. Document where the focus trap will be active. This allows to use FocusTrap in an iFrame context.
|
|
109
|
+
|
|
110
|
+
#### Shadow DOM and selector strings
|
|
111
|
+
|
|
112
|
+
⚠️ Beware that putting a focus-trap **inside** an open Shadow DOM means you must either:
|
|
113
|
+
|
|
114
|
+
- **Not use selector strings** for options that support these (because nodes inside Shadow DOMs, even open shadows, are not visible via `document.querySelector()`); OR
|
|
115
|
+
- You must **use the `document` option** to configure the focus trap to use your *shadow host* element as its document. The downside of this option is that, while selector queries on nodes inside your trap will now work, the trap will not prevent focus from being set on nodes outside your Shadow DOM, which is the same drawback as putting a focus trap <a href="https://focus-trap.github.io/focus-trap/#demo-in-iframe">inside an iframe</a>.
|
|
102
116
|
|
|
103
117
|
### trap.activate([activateOptions])
|
|
104
118
|
|
|
@@ -132,7 +146,7 @@ Returns the `trap`.
|
|
|
132
146
|
|
|
133
147
|
These options are used to override the focus trap's default behavior for this particular deactivation.
|
|
134
148
|
|
|
135
|
-
- **returnFocus** `{boolean}`: Default: whatever you chose for `createOptions.returnFocusOnDeactivate`.
|
|
149
|
+
- **returnFocus** `{boolean}`: Default: whatever you chose for `createOptions.returnFocusOnDeactivate`. If `true`, then the `setReturnFocus` option (specified when the trap was created) is used to determine where focus will be returned.
|
|
136
150
|
- **onDeactivate** `{() => void}`: Default: whatever you chose for `createOptions.onDeactivate`. `null` or `false` are the equivalent of a `noop`.
|
|
137
151
|
- **onPostDeactivate** `{() => void}`: Default: whatever you chose for `createOptions.onPostDeactivate`. `null` or `false` are the equivalent of a `noop`.
|
|
138
152
|
- **checkCanReturnFocus** `{(trigger: HTMLElement | SVGElement) => Promise<void>}`: Default: whatever you chose for `createOptions.checkCanReturnFocus`. Not called if the `returnFocus` option is falsy. `trigger` is either the originally focused node prior to activation, or the result of the `setReturnFocus` configuration option.
|
|
@@ -171,12 +185,12 @@ Returns the `trap`.
|
|
|
171
185
|
|
|
172
186
|
## Examples
|
|
173
187
|
|
|
174
|
-
Read code in `
|
|
188
|
+
Read code in `docs/` and [see how it works](http://focus-trap.github.io/focus-trap/).
|
|
175
189
|
|
|
176
|
-
Here's what happens in `default.js` (the "default behavior" demo):
|
|
190
|
+
Here's generally what happens in `default.js` (the "default behavior" demo):
|
|
177
191
|
|
|
178
192
|
```js
|
|
179
|
-
const { createFocusTrap } = require('../../
|
|
193
|
+
const { createFocusTrap } = require('../../index');
|
|
180
194
|
|
|
181
195
|
const container = document.getElementById('default');
|
|
182
196
|
|
|
@@ -232,24 +246,28 @@ In alphabetical order:
|
|
|
232
246
|
<!-- markdownlint-disable -->
|
|
233
247
|
<table>
|
|
234
248
|
<tr>
|
|
249
|
+
<td align="center"><a href="https://github.com/andersthorsen"><img src="https://avatars.githubusercontent.com/u/190081?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anders Thorsen</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Aandersthorsen" title="Bug reports">🐛</a></td>
|
|
235
250
|
<td align="center"><a href="https://github.com/bparish628"><img src="https://avatars1.githubusercontent.com/u/8492971?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Benjamin Parish</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Abparish628" title="Bug reports">🐛</a></td>
|
|
236
251
|
<td align="center"><a href="https://clintgoodman.com"><img src="https://avatars3.githubusercontent.com/u/5473697?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Clint Goodman</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=cgood92" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=cgood92" title="Documentation">📖</a> <a href="#example-cgood92" title="Examples">💡</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=cgood92" title="Tests">⚠️</a></td>
|
|
237
252
|
<td align="center"><a href="https://github.com/Dan503"><img src="https://avatars.githubusercontent.com/u/10610368?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Daniel Tonon</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=Dan503" title="Documentation">📖</a> <a href="#tool-Dan503" title="Tools">🔧</a> <a href="#a11y-Dan503" title="Accessibility">️️️️♿️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=Dan503" title="Code">💻</a></td>
|
|
238
253
|
<td align="center"><a href="http://davidtheclark.com/"><img src="https://avatars2.githubusercontent.com/u/628431?v=4?s=100" width="100px;" alt=""/><br /><sub><b>David Clark</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=davidtheclark" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Adavidtheclark" title="Bug reports">🐛</a> <a href="#infra-davidtheclark" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=davidtheclark" title="Tests">⚠️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=davidtheclark" title="Documentation">📖</a> <a href="#maintenance-davidtheclark" title="Maintenance">🚧</a></td>
|
|
239
254
|
<td align="center"><a href="https://github.com/features/security"><img src="https://avatars1.githubusercontent.com/u/27347476?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dependabot</b></sub></a><br /><a href="#maintenance-dependabot" title="Maintenance">🚧</a></td>
|
|
240
255
|
<td align="center"><a href="https://github.com/michael-ar"><img src="https://avatars3.githubusercontent.com/u/18557997?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Reynolds</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Amichael-ar" title="Bug reports">🐛</a></td>
|
|
241
|
-
<td align="center"><a href="https://github.com/liunate"><img src="https://avatars2.githubusercontent.com/u/38996291?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nate Liu</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=liunate" title="Tests">⚠️</a></td>
|
|
242
256
|
</tr>
|
|
243
257
|
<tr>
|
|
258
|
+
<td align="center"><a href="https://github.com/liunate"><img src="https://avatars2.githubusercontent.com/u/38996291?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nate Liu</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=liunate" title="Tests">⚠️</a></td>
|
|
259
|
+
<td align="center"><a href="https://github.com/far-fetched"><img src="https://avatars.githubusercontent.com/u/11621383?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Piotr Panek</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Afar-fetched" title="Bug reports">🐛</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=far-fetched" title="Documentation">📖</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=far-fetched" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=far-fetched" title="Tests">⚠️</a></td>
|
|
244
260
|
<td align="center"><a href="https://github.com/randypuro"><img src="https://avatars2.githubusercontent.com/u/2579?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Randy Puro</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Arandypuro" title="Bug reports">🐛</a></td>
|
|
245
261
|
<td align="center"><a href="https://github.com/sadick254"><img src="https://avatars2.githubusercontent.com/u/5238135?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sadick</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=sadick254" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=sadick254" title="Tests">⚠️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=sadick254" title="Documentation">📖</a></td>
|
|
246
262
|
<td align="center"><a href="https://scottblinch.me/"><img src="https://avatars2.githubusercontent.com/u/4682114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Scott Blinch</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=scottblinch" title="Documentation">📖</a></td>
|
|
247
263
|
<td align="center"><a href="https://seanmcp.com/"><img src="https://avatars1.githubusercontent.com/u/6360367?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sean McPherson</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=SeanMcP" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=SeanMcP" title="Documentation">📖</a></td>
|
|
248
264
|
<td align="center"><a href="https://recollectr.io"><img src="https://avatars2.githubusercontent.com/u/6835891?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Slapbox</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3ASlapbox" title="Bug reports">🐛</a></td>
|
|
249
|
-
<td align="center"><a href="https://stefancameron.com/"><img src="https://avatars3.githubusercontent.com/u/2855350?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stefan Cameron</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=stefcameron" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Astefcameron" title="Bug reports">🐛</a> <a href="#infra-stefcameron" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=stefcameron" title="Tests">⚠️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=stefcameron" title="Documentation">📖</a> <a href="#maintenance-stefcameron" title="Maintenance">🚧</a></td>
|
|
250
|
-
<td align="center"><a href="http://tylerhawkins.info/201R/"><img src="https://avatars0.githubusercontent.com/u/13806458?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tyler Hawkins</b></sub></a><br /><a href="#tool-thawkin3" title="Tools">🔧</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=thawkin3" title="Tests">⚠️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=thawkin3" title="Documentation">📖</a></td>
|
|
251
265
|
</tr>
|
|
252
266
|
<tr>
|
|
267
|
+
<td align="center"><a href="https://stefancameron.com/"><img src="https://avatars3.githubusercontent.com/u/2855350?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stefan Cameron</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=stefcameron" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Astefcameron" title="Bug reports">🐛</a> <a href="#infra-stefcameron" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=stefcameron" title="Tests">⚠️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=stefcameron" title="Documentation">📖</a> <a href="#maintenance-stefcameron" title="Maintenance">🚧</a></td>
|
|
268
|
+
<td align="center"><a href="http://tylerhawkins.info/201R/"><img src="https://avatars0.githubusercontent.com/u/13806458?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tyler Hawkins</b></sub></a><br /><a href="#tool-thawkin3" title="Tools">🔧</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=thawkin3" title="Tests">⚠️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=thawkin3" title="Documentation">📖</a></td>
|
|
269
|
+
<td align="center"><a href="https://github.com/wandroll"><img src="https://avatars.githubusercontent.com/u/4492317?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Wandrille Verlut</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=wandroll" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=wandroll" title="Tests">⚠️</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=wandroll" title="Documentation">📖</a> <a href="#tool-wandroll" title="Tools">🔧</a></td>
|
|
270
|
+
<td align="center"><a href="http://willmruzek.com/"><img src="https://avatars.githubusercontent.com/u/108522?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Will Mruzek</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=mruzekw" title="Code">💻</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=mruzekw" title="Documentation">📖</a> <a href="#example-mruzekw" title="Examples">💡</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=mruzekw" title="Tests">⚠️</a> <a href="#question-mruzekw" title="Answering Questions">💬</a></td>
|
|
253
271
|
<td align="center"><a href="https://github.com/zioth"><img src="https://avatars3.githubusercontent.com/u/945603?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Zioth</b></sub></a><br /><a href="#ideas-zioth" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Azioth" title="Bug reports">🐛</a></td>
|
|
254
272
|
</tr>
|
|
255
273
|
</table>
|
package/dist/focus-trap.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* focus-trap 6.
|
|
2
|
+
* focus-trap 6.7.0
|
|
3
3
|
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
|
|
4
4
|
*/
|
|
5
5
|
import { tabbable, isFocusable } from 'tabbable';
|
|
@@ -57,8 +57,6 @@ function _defineProperty(obj, key, value) {
|
|
|
57
57
|
return obj;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
var activeFocusDelay;
|
|
61
|
-
|
|
62
60
|
var activeFocusTraps = function () {
|
|
63
61
|
var trapQueue = [];
|
|
64
62
|
return {
|
|
@@ -142,8 +140,19 @@ var valueOrHandler = function valueOrHandler(value) {
|
|
|
142
140
|
return typeof value === 'function' ? value.apply(void 0, params) : value;
|
|
143
141
|
};
|
|
144
142
|
|
|
143
|
+
var getActualTarget = function getActualTarget(event) {
|
|
144
|
+
// NOTE: If the trap is _inside_ a shadow DOM, event.target will always be the
|
|
145
|
+
// shadow host. However, event.target.composedPath() will be an array of
|
|
146
|
+
// nodes "clicked" from inner-most (the actual element inside the shadow) to
|
|
147
|
+
// outer-most (the host HTML document). If we have access to composedPath(),
|
|
148
|
+
// then use its first element; otherwise, fall back to event.target (and
|
|
149
|
+
// this only works for an _open_ shadow DOM; otherwise,
|
|
150
|
+
// composedPath()[0] === event.target always).
|
|
151
|
+
return event.target.shadowRoot && typeof event.composedPath === 'function' ? event.composedPath()[0] : event.target;
|
|
152
|
+
};
|
|
153
|
+
|
|
145
154
|
var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
146
|
-
var doc = document;
|
|
155
|
+
var doc = userOptions.document || document;
|
|
147
156
|
|
|
148
157
|
var config = _objectSpread2({
|
|
149
158
|
returnFocusOnDeactivate: true,
|
|
@@ -165,7 +174,10 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
165
174
|
nodeFocusedBeforeActivation: null,
|
|
166
175
|
mostRecentlyFocusedNode: null,
|
|
167
176
|
active: false,
|
|
168
|
-
paused: false
|
|
177
|
+
paused: false,
|
|
178
|
+
// timer ID for when delayInitialFocus is true and initial focus in this trap
|
|
179
|
+
// has been delayed during activation
|
|
180
|
+
delayInitialFocusTimer: undefined
|
|
169
181
|
};
|
|
170
182
|
var trap; // eslint-disable-line prefer-const -- some private functions reference it, and its methods reference private functions, so we must declare here and define later
|
|
171
183
|
|
|
@@ -174,33 +186,52 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
174
186
|
};
|
|
175
187
|
|
|
176
188
|
var containersContain = function containersContain(element) {
|
|
177
|
-
return state.containers.some(function (container) {
|
|
189
|
+
return !!(element && state.containers.some(function (container) {
|
|
178
190
|
return container.contains(element);
|
|
179
|
-
});
|
|
191
|
+
}));
|
|
180
192
|
};
|
|
193
|
+
/**
|
|
194
|
+
* Gets the node for the given option, which is expected to be an option that
|
|
195
|
+
* can be either a DOM node, a string that is a selector to get a node, `false`
|
|
196
|
+
* (if a node is explicitly NOT given), or a function that returns any of these
|
|
197
|
+
* values.
|
|
198
|
+
* @param {string} optionName
|
|
199
|
+
* @returns {undefined | false | HTMLElement | SVGElement} Returns
|
|
200
|
+
* `undefined` if the option is not specified; `false` if the option
|
|
201
|
+
* resolved to `false` (node explicitly not given); otherwise, the resolved
|
|
202
|
+
* DOM node.
|
|
203
|
+
* @throws {Error} If the option is set, not `false`, and is not, or does not
|
|
204
|
+
* resolve to a node.
|
|
205
|
+
*/
|
|
206
|
+
|
|
181
207
|
|
|
182
208
|
var getNodeForOption = function getNodeForOption(optionName) {
|
|
183
209
|
var optionValue = config[optionName];
|
|
184
210
|
|
|
185
|
-
if (
|
|
186
|
-
|
|
211
|
+
if (typeof optionValue === 'function') {
|
|
212
|
+
for (var _len2 = arguments.length, params = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
213
|
+
params[_key2 - 1] = arguments[_key2];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
optionValue = optionValue.apply(void 0, params);
|
|
187
217
|
}
|
|
188
218
|
|
|
189
|
-
|
|
219
|
+
if (!optionValue) {
|
|
220
|
+
if (optionValue === undefined || optionValue === false) {
|
|
221
|
+
return optionValue;
|
|
222
|
+
} // else, empty string (invalid), null (invalid), 0 (invalid)
|
|
190
223
|
|
|
191
|
-
if (typeof optionValue === 'string') {
|
|
192
|
-
node = doc.querySelector(optionValue);
|
|
193
224
|
|
|
194
|
-
|
|
195
|
-
throw new Error("`".concat(optionName, "` refers to no known node"));
|
|
196
|
-
}
|
|
225
|
+
throw new Error("`".concat(optionName, "` was specified but was not a node, or did not return a node"));
|
|
197
226
|
}
|
|
198
227
|
|
|
199
|
-
|
|
200
|
-
|
|
228
|
+
var node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point
|
|
229
|
+
|
|
230
|
+
if (typeof optionValue === 'string') {
|
|
231
|
+
node = doc.querySelector(optionValue); // resolve to node, or null if fails
|
|
201
232
|
|
|
202
233
|
if (!node) {
|
|
203
|
-
throw new Error("`".concat(optionName, "`
|
|
234
|
+
throw new Error("`".concat(optionName, "` as selector refers to no known node"));
|
|
204
235
|
}
|
|
205
236
|
}
|
|
206
237
|
|
|
@@ -208,16 +239,22 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
208
239
|
};
|
|
209
240
|
|
|
210
241
|
var getInitialFocusNode = function getInitialFocusNode() {
|
|
211
|
-
var node;
|
|
242
|
+
var node = getNodeForOption('initialFocus'); // false explicitly indicates we want no initialFocus at all
|
|
212
243
|
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
244
|
+
if (node === false) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (node === undefined) {
|
|
249
|
+
// option not specified: use fallback options
|
|
250
|
+
if (containersContain(doc.activeElement)) {
|
|
251
|
+
node = doc.activeElement;
|
|
252
|
+
} else {
|
|
253
|
+
var firstTabbableGroup = state.tabbableGroups[0];
|
|
254
|
+
var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode; // NOTE: `fallbackFocus` option function cannot return `false` (not supported)
|
|
255
|
+
|
|
256
|
+
node = firstTabbableNode || getNodeForOption('fallbackFocus');
|
|
257
|
+
}
|
|
221
258
|
}
|
|
222
259
|
|
|
223
260
|
if (!node) {
|
|
@@ -245,12 +282,17 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
245
282
|
}); // remove groups with no tabbable nodes
|
|
246
283
|
// throw if no groups have tabbable nodes and we don't have a fallback focus node either
|
|
247
284
|
|
|
248
|
-
if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus')
|
|
285
|
+
if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus') // returning false not supported for this option
|
|
286
|
+
) {
|
|
249
287
|
throw new Error('Your focus-trap must have at least one container with at least one tabbable node in it at all times');
|
|
250
288
|
}
|
|
251
289
|
};
|
|
252
290
|
|
|
253
291
|
var tryFocus = function tryFocus(node) {
|
|
292
|
+
if (node === false) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
254
296
|
if (node === doc.activeElement) {
|
|
255
297
|
return;
|
|
256
298
|
}
|
|
@@ -271,14 +313,16 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
271
313
|
};
|
|
272
314
|
|
|
273
315
|
var getReturnFocusNode = function getReturnFocusNode(previousActiveElement) {
|
|
274
|
-
var node = getNodeForOption('setReturnFocus');
|
|
275
|
-
return node ? node : previousActiveElement;
|
|
316
|
+
var node = getNodeForOption('setReturnFocus', previousActiveElement);
|
|
317
|
+
return node ? node : node === false ? false : previousActiveElement;
|
|
276
318
|
}; // This needs to be done on mousedown and touchstart instead of click
|
|
277
319
|
// so that it precedes the focus event.
|
|
278
320
|
|
|
279
321
|
|
|
280
322
|
var checkPointerDown = function checkPointerDown(e) {
|
|
281
|
-
|
|
323
|
+
var target = getActualTarget(e);
|
|
324
|
+
|
|
325
|
+
if (containersContain(target)) {
|
|
282
326
|
// allow the click since it ocurred inside the trap
|
|
283
327
|
return;
|
|
284
328
|
}
|
|
@@ -297,7 +341,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
297
341
|
// that was clicked, whether it's focusable or not; by setting
|
|
298
342
|
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused
|
|
299
343
|
// on activation (or the configured `setReturnFocus` node)
|
|
300
|
-
returnFocus: config.returnFocusOnDeactivate && !isFocusable(
|
|
344
|
+
returnFocus: config.returnFocusOnDeactivate && !isFocusable(target)
|
|
301
345
|
});
|
|
302
346
|
return;
|
|
303
347
|
} // This is needed for mobile devices.
|
|
@@ -316,11 +360,12 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
316
360
|
|
|
317
361
|
|
|
318
362
|
var checkFocusIn = function checkFocusIn(e) {
|
|
319
|
-
var
|
|
363
|
+
var target = getActualTarget(e);
|
|
364
|
+
var targetContained = containersContain(target); // In Firefox when you Tab out of an iframe the Document is briefly focused.
|
|
320
365
|
|
|
321
|
-
if (targetContained ||
|
|
366
|
+
if (targetContained || target instanceof Document) {
|
|
322
367
|
if (targetContained) {
|
|
323
|
-
state.mostRecentlyFocusedNode =
|
|
368
|
+
state.mostRecentlyFocusedNode = target;
|
|
324
369
|
}
|
|
325
370
|
} else {
|
|
326
371
|
// escaped! pull it back in to where it just left
|
|
@@ -334,6 +379,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
334
379
|
|
|
335
380
|
|
|
336
381
|
var checkTab = function checkTab(e) {
|
|
382
|
+
var target = getActualTarget(e);
|
|
337
383
|
updateTabbableNodes();
|
|
338
384
|
var destinationNode = null;
|
|
339
385
|
|
|
@@ -343,7 +389,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
343
389
|
// with tabIndex='-1' and was given initial focus
|
|
344
390
|
var containerIndex = findIndex(state.tabbableGroups, function (_ref) {
|
|
345
391
|
var container = _ref.container;
|
|
346
|
-
return container.contains(
|
|
392
|
+
return container.contains(target);
|
|
347
393
|
});
|
|
348
394
|
|
|
349
395
|
if (containerIndex < 0) {
|
|
@@ -361,10 +407,10 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
361
407
|
// is the target the first tabbable node in a group?
|
|
362
408
|
var startOfGroupIndex = findIndex(state.tabbableGroups, function (_ref2) {
|
|
363
409
|
var firstTabbableNode = _ref2.firstTabbableNode;
|
|
364
|
-
return
|
|
410
|
+
return target === firstTabbableNode;
|
|
365
411
|
});
|
|
366
412
|
|
|
367
|
-
if (startOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container ===
|
|
413
|
+
if (startOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container === target) {
|
|
368
414
|
// an exception case where the target is the container itself, in which
|
|
369
415
|
// case, we should handle shift+tab as if focus were on the container's
|
|
370
416
|
// first tabbable node, and go to the last tabbable node of the LAST group
|
|
@@ -384,10 +430,10 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
384
430
|
// is the target the last tabbable node in a group?
|
|
385
431
|
var lastOfGroupIndex = findIndex(state.tabbableGroups, function (_ref3) {
|
|
386
432
|
var lastTabbableNode = _ref3.lastTabbableNode;
|
|
387
|
-
return
|
|
433
|
+
return target === lastTabbableNode;
|
|
388
434
|
});
|
|
389
435
|
|
|
390
|
-
if (lastOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container ===
|
|
436
|
+
if (lastOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container === target) {
|
|
391
437
|
// an exception case where the target is the container itself, in which
|
|
392
438
|
// case, we should handle tab as if focus were on the container's
|
|
393
439
|
// last tabbable node, and go to the first tabbable node of the FIRST group
|
|
@@ -405,6 +451,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
405
451
|
}
|
|
406
452
|
}
|
|
407
453
|
} else {
|
|
454
|
+
// NOTE: the fallbackFocus option does not support returning false to opt-out
|
|
408
455
|
destinationNode = getNodeForOption('fallbackFocus');
|
|
409
456
|
}
|
|
410
457
|
|
|
@@ -416,7 +463,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
416
463
|
};
|
|
417
464
|
|
|
418
465
|
var checkKey = function checkKey(e) {
|
|
419
|
-
if (config.escapeDeactivates !== false
|
|
466
|
+
if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates, e) !== false) {
|
|
420
467
|
e.preventDefault();
|
|
421
468
|
trap.deactivate();
|
|
422
469
|
return;
|
|
@@ -433,7 +480,9 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
433
480
|
return;
|
|
434
481
|
}
|
|
435
482
|
|
|
436
|
-
|
|
483
|
+
var target = getActualTarget(e);
|
|
484
|
+
|
|
485
|
+
if (containersContain(target)) {
|
|
437
486
|
return;
|
|
438
487
|
}
|
|
439
488
|
|
|
@@ -457,7 +506,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
457
506
|
activeFocusTraps.activateTrap(trap); // Delay ensures that the focused element doesn't capture the event
|
|
458
507
|
// that caused the focus trap activation.
|
|
459
508
|
|
|
460
|
-
|
|
509
|
+
state.delayInitialFocusTimer = config.delayInitialFocus ? delay(function () {
|
|
461
510
|
tryFocus(getInitialFocusNode());
|
|
462
511
|
}) : tryFocus(getInitialFocusNode());
|
|
463
512
|
doc.addEventListener('focusin', checkFocusIn, true);
|
|
@@ -543,7 +592,9 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
543
592
|
return this;
|
|
544
593
|
}
|
|
545
594
|
|
|
546
|
-
clearTimeout(
|
|
595
|
+
clearTimeout(state.delayInitialFocusTimer); // noop if undefined
|
|
596
|
+
|
|
597
|
+
state.delayInitialFocusTimer = undefined;
|
|
547
598
|
removeListeners();
|
|
548
599
|
state.active = false;
|
|
549
600
|
state.paused = false;
|
|
@@ -559,15 +610,15 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
559
610
|
var returnFocus = getOption(deactivateOptions, 'returnFocus', 'returnFocusOnDeactivate');
|
|
560
611
|
|
|
561
612
|
var finishDeactivation = function finishDeactivation() {
|
|
562
|
-
|
|
563
|
-
|
|
613
|
+
delay(function () {
|
|
614
|
+
if (returnFocus) {
|
|
564
615
|
tryFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation));
|
|
616
|
+
}
|
|
565
617
|
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
}
|
|
618
|
+
if (onPostDeactivate) {
|
|
619
|
+
onPostDeactivate();
|
|
620
|
+
}
|
|
621
|
+
});
|
|
571
622
|
};
|
|
572
623
|
|
|
573
624
|
if (returnFocus && checkCanReturnFocus) {
|