focus-trap 6.6.0 → 6.7.2
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 +31 -0
- package/README.md +25 -9
- package/dist/focus-trap.esm.js +99 -65
- 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 +98 -64
- 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 +101 -67
- 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 +25 -4
- package/index.js +101 -47
- package/package.json +28 -26
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 6.7.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c932330: Fixed bug where tabbing forward from an element with negative tabindex that is last in the trap would result in focus remaining on that element ([565](https://github.com/focus-trap/focus-trap/issues/565))
|
|
8
|
+
|
|
9
|
+
## 6.7.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 28a069f: Fix bug from #504 where it's no longer possible to create a trap without any options [#525]
|
|
14
|
+
|
|
15
|
+
## 6.7.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- 893dd2c: Add `document` option to support focus traps inside `<iframe>` elements (#97)
|
|
20
|
+
- 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)
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- 60162eb: Fix bug where `KeyboardEvent` was not being passed to `escapeDeactivates` option when it's a function (#498)
|
|
25
|
+
- 7b6abfa: Fix how focus-trap determines the event's target, which was preventing traps inside open shadow DOMs from working properly (#496)
|
|
26
|
+
- 14b0ee8: Fix `initialFocus` option not supporting function returning `false` as documented (#490)
|
|
27
|
+
|
|
28
|
+
## 6.6.1
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- 24063d7: Update tabbable to v5.2.1 to get bug fix for disabled fieldsets.
|
|
33
|
+
|
|
3
34
|
## 6.6.0
|
|
4
35
|
|
|
5
36
|
### 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.
|
|
@@ -88,17 +88,31 @@ Returns a new focus trap on `element` (one or more "containers" of tabbable node
|
|
|
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
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 | 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` to prevent any initial focus at all when the trap activates.
|
|
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.
|
|
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.
|
|
93
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.
|
|
@@ -173,10 +187,10 @@ Returns the `trap`.
|
|
|
173
187
|
|
|
174
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,25 +246,27 @@ 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>
|
|
244
|
-
<td align="center"><a href="https://github.com/
|
|
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>
|
|
245
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>
|
|
246
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>
|
|
247
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>
|
|
248
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>
|
|
249
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>
|
|
250
|
-
<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>
|
|
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>
|
|
253
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>
|
|
254
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>
|
|
255
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>
|
|
256
272
|
</tr>
|
package/dist/focus-trap.esm.js
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* focus-trap 6.
|
|
2
|
+
* focus-trap 6.7.2
|
|
3
3
|
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
|
|
4
4
|
*/
|
|
5
|
-
import { tabbable, isFocusable } from 'tabbable';
|
|
5
|
+
import { tabbable, isFocusable, isTabbable } from 'tabbable';
|
|
6
6
|
|
|
7
7
|
function ownKeys(object, enumerableOnly) {
|
|
8
8
|
var keys = Object.keys(object);
|
|
9
9
|
|
|
10
10
|
if (Object.getOwnPropertySymbols) {
|
|
11
11
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
keys.push.apply(keys, symbols);
|
|
12
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
13
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
14
|
+
})), keys.push.apply(keys, symbols);
|
|
20
15
|
}
|
|
21
16
|
|
|
22
17
|
return keys;
|
|
@@ -24,19 +19,12 @@ function ownKeys(object, enumerableOnly) {
|
|
|
24
19
|
|
|
25
20
|
function _objectSpread2(target) {
|
|
26
21
|
for (var i = 1; i < arguments.length; i++) {
|
|
27
|
-
var source = arguments[i]
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
} else if (Object.getOwnPropertyDescriptors) {
|
|
34
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
35
|
-
} else {
|
|
36
|
-
ownKeys(Object(source)).forEach(function (key) {
|
|
37
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
38
|
-
});
|
|
39
|
-
}
|
|
22
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
23
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
24
|
+
_defineProperty(target, key, source[key]);
|
|
25
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
26
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
27
|
+
});
|
|
40
28
|
}
|
|
41
29
|
|
|
42
30
|
return target;
|
|
@@ -140,8 +128,21 @@ var valueOrHandler = function valueOrHandler(value) {
|
|
|
140
128
|
return typeof value === 'function' ? value.apply(void 0, params) : value;
|
|
141
129
|
};
|
|
142
130
|
|
|
131
|
+
var getActualTarget = function getActualTarget(event) {
|
|
132
|
+
// NOTE: If the trap is _inside_ a shadow DOM, event.target will always be the
|
|
133
|
+
// shadow host. However, event.target.composedPath() will be an array of
|
|
134
|
+
// nodes "clicked" from inner-most (the actual element inside the shadow) to
|
|
135
|
+
// outer-most (the host HTML document). If we have access to composedPath(),
|
|
136
|
+
// then use its first element; otherwise, fall back to event.target (and
|
|
137
|
+
// this only works for an _open_ shadow DOM; otherwise,
|
|
138
|
+
// composedPath()[0] === event.target always).
|
|
139
|
+
return event.target.shadowRoot && typeof event.composedPath === 'function' ? event.composedPath()[0] : event.target;
|
|
140
|
+
};
|
|
141
|
+
|
|
143
142
|
var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
144
|
-
|
|
143
|
+
// SSR: a live trap shouldn't be created in this type of environment so this
|
|
144
|
+
// should be safe code to execute if the `document` option isn't specified
|
|
145
|
+
var doc = (userOptions === null || userOptions === void 0 ? void 0 : userOptions.document) || document;
|
|
145
146
|
|
|
146
147
|
var config = _objectSpread2({
|
|
147
148
|
returnFocusOnDeactivate: true,
|
|
@@ -175,33 +176,52 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
175
176
|
};
|
|
176
177
|
|
|
177
178
|
var containersContain = function containersContain(element) {
|
|
178
|
-
return state.containers.some(function (container) {
|
|
179
|
+
return !!(element && state.containers.some(function (container) {
|
|
179
180
|
return container.contains(element);
|
|
180
|
-
});
|
|
181
|
+
}));
|
|
181
182
|
};
|
|
183
|
+
/**
|
|
184
|
+
* Gets the node for the given option, which is expected to be an option that
|
|
185
|
+
* can be either a DOM node, a string that is a selector to get a node, `false`
|
|
186
|
+
* (if a node is explicitly NOT given), or a function that returns any of these
|
|
187
|
+
* values.
|
|
188
|
+
* @param {string} optionName
|
|
189
|
+
* @returns {undefined | false | HTMLElement | SVGElement} Returns
|
|
190
|
+
* `undefined` if the option is not specified; `false` if the option
|
|
191
|
+
* resolved to `false` (node explicitly not given); otherwise, the resolved
|
|
192
|
+
* DOM node.
|
|
193
|
+
* @throws {Error} If the option is set, not `false`, and is not, or does not
|
|
194
|
+
* resolve to a node.
|
|
195
|
+
*/
|
|
196
|
+
|
|
182
197
|
|
|
183
198
|
var getNodeForOption = function getNodeForOption(optionName) {
|
|
184
199
|
var optionValue = config[optionName];
|
|
185
200
|
|
|
186
|
-
if (
|
|
187
|
-
|
|
201
|
+
if (typeof optionValue === 'function') {
|
|
202
|
+
for (var _len2 = arguments.length, params = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
203
|
+
params[_key2 - 1] = arguments[_key2];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
optionValue = optionValue.apply(void 0, params);
|
|
188
207
|
}
|
|
189
208
|
|
|
190
|
-
|
|
209
|
+
if (!optionValue) {
|
|
210
|
+
if (optionValue === undefined || optionValue === false) {
|
|
211
|
+
return optionValue;
|
|
212
|
+
} // else, empty string (invalid), null (invalid), 0 (invalid)
|
|
191
213
|
|
|
192
|
-
if (typeof optionValue === 'string') {
|
|
193
|
-
node = doc.querySelector(optionValue);
|
|
194
214
|
|
|
195
|
-
|
|
196
|
-
throw new Error("`".concat(optionName, "` refers to no known node"));
|
|
197
|
-
}
|
|
215
|
+
throw new Error("`".concat(optionName, "` was specified but was not a node, or did not return a node"));
|
|
198
216
|
}
|
|
199
217
|
|
|
200
|
-
|
|
201
|
-
|
|
218
|
+
var node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point
|
|
219
|
+
|
|
220
|
+
if (typeof optionValue === 'string') {
|
|
221
|
+
node = doc.querySelector(optionValue); // resolve to node, or null if fails
|
|
202
222
|
|
|
203
223
|
if (!node) {
|
|
204
|
-
throw new Error("`".concat(optionName, "`
|
|
224
|
+
throw new Error("`".concat(optionName, "` as selector refers to no known node"));
|
|
205
225
|
}
|
|
206
226
|
}
|
|
207
227
|
|
|
@@ -209,20 +229,22 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
209
229
|
};
|
|
210
230
|
|
|
211
231
|
var getInitialFocusNode = function getInitialFocusNode() {
|
|
212
|
-
var node; // false indicates we want no initialFocus at all
|
|
232
|
+
var node = getNodeForOption('initialFocus'); // false explicitly indicates we want no initialFocus at all
|
|
213
233
|
|
|
214
|
-
if (
|
|
234
|
+
if (node === false) {
|
|
215
235
|
return false;
|
|
216
236
|
}
|
|
217
237
|
|
|
218
|
-
if (
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
238
|
+
if (node === undefined) {
|
|
239
|
+
// option not specified: use fallback options
|
|
240
|
+
if (containersContain(doc.activeElement)) {
|
|
241
|
+
node = doc.activeElement;
|
|
242
|
+
} else {
|
|
243
|
+
var firstTabbableGroup = state.tabbableGroups[0];
|
|
244
|
+
var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode; // NOTE: `fallbackFocus` option function cannot return `false` (not supported)
|
|
245
|
+
|
|
246
|
+
node = firstTabbableNode || getNodeForOption('fallbackFocus');
|
|
247
|
+
}
|
|
226
248
|
}
|
|
227
249
|
|
|
228
250
|
if (!node) {
|
|
@@ -250,7 +272,8 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
250
272
|
}); // remove groups with no tabbable nodes
|
|
251
273
|
// throw if no groups have tabbable nodes and we don't have a fallback focus node either
|
|
252
274
|
|
|
253
|
-
if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus')
|
|
275
|
+
if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus') // returning false not supported for this option
|
|
276
|
+
) {
|
|
254
277
|
throw new Error('Your focus-trap must have at least one container with at least one tabbable node in it at all times');
|
|
255
278
|
}
|
|
256
279
|
};
|
|
@@ -280,14 +303,16 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
280
303
|
};
|
|
281
304
|
|
|
282
305
|
var getReturnFocusNode = function getReturnFocusNode(previousActiveElement) {
|
|
283
|
-
var node = getNodeForOption('setReturnFocus');
|
|
284
|
-
return node ? node : previousActiveElement;
|
|
306
|
+
var node = getNodeForOption('setReturnFocus', previousActiveElement);
|
|
307
|
+
return node ? node : node === false ? false : previousActiveElement;
|
|
285
308
|
}; // This needs to be done on mousedown and touchstart instead of click
|
|
286
309
|
// so that it precedes the focus event.
|
|
287
310
|
|
|
288
311
|
|
|
289
312
|
var checkPointerDown = function checkPointerDown(e) {
|
|
290
|
-
|
|
313
|
+
var target = getActualTarget(e);
|
|
314
|
+
|
|
315
|
+
if (containersContain(target)) {
|
|
291
316
|
// allow the click since it ocurred inside the trap
|
|
292
317
|
return;
|
|
293
318
|
}
|
|
@@ -306,7 +331,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
306
331
|
// that was clicked, whether it's focusable or not; by setting
|
|
307
332
|
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused
|
|
308
333
|
// on activation (or the configured `setReturnFocus` node)
|
|
309
|
-
returnFocus: config.returnFocusOnDeactivate && !isFocusable(
|
|
334
|
+
returnFocus: config.returnFocusOnDeactivate && !isFocusable(target)
|
|
310
335
|
});
|
|
311
336
|
return;
|
|
312
337
|
} // This is needed for mobile devices.
|
|
@@ -325,11 +350,12 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
325
350
|
|
|
326
351
|
|
|
327
352
|
var checkFocusIn = function checkFocusIn(e) {
|
|
328
|
-
var
|
|
353
|
+
var target = getActualTarget(e);
|
|
354
|
+
var targetContained = containersContain(target); // In Firefox when you Tab out of an iframe the Document is briefly focused.
|
|
329
355
|
|
|
330
|
-
if (targetContained ||
|
|
356
|
+
if (targetContained || target instanceof Document) {
|
|
331
357
|
if (targetContained) {
|
|
332
|
-
state.mostRecentlyFocusedNode =
|
|
358
|
+
state.mostRecentlyFocusedNode = target;
|
|
333
359
|
}
|
|
334
360
|
} else {
|
|
335
361
|
// escaped! pull it back in to where it just left
|
|
@@ -343,16 +369,17 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
343
369
|
|
|
344
370
|
|
|
345
371
|
var checkTab = function checkTab(e) {
|
|
372
|
+
var target = getActualTarget(e);
|
|
346
373
|
updateTabbableNodes();
|
|
347
374
|
var destinationNode = null;
|
|
348
375
|
|
|
349
376
|
if (state.tabbableGroups.length > 0) {
|
|
350
377
|
// make sure the target is actually contained in a group
|
|
351
|
-
// NOTE: the target may also be the container itself if it's
|
|
378
|
+
// NOTE: the target may also be the container itself if it's focusable
|
|
352
379
|
// with tabIndex='-1' and was given initial focus
|
|
353
380
|
var containerIndex = findIndex(state.tabbableGroups, function (_ref) {
|
|
354
381
|
var container = _ref.container;
|
|
355
|
-
return container.contains(
|
|
382
|
+
return container.contains(target);
|
|
356
383
|
});
|
|
357
384
|
|
|
358
385
|
if (containerIndex < 0) {
|
|
@@ -370,11 +397,13 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
370
397
|
// is the target the first tabbable node in a group?
|
|
371
398
|
var startOfGroupIndex = findIndex(state.tabbableGroups, function (_ref2) {
|
|
372
399
|
var firstTabbableNode = _ref2.firstTabbableNode;
|
|
373
|
-
return
|
|
400
|
+
return target === firstTabbableNode;
|
|
374
401
|
});
|
|
375
402
|
|
|
376
|
-
if (startOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container ===
|
|
377
|
-
// an exception case where the target is the container itself,
|
|
403
|
+
if (startOfGroupIndex < 0 && (state.tabbableGroups[containerIndex].container === target || isFocusable(target) && !isTabbable(target))) {
|
|
404
|
+
// an exception case where the target is either the container itself, or
|
|
405
|
+
// a non-tabbable node that was given focus (i.e. tabindex is negative
|
|
406
|
+
// and user clicked on it or node was programmatically given focus), in which
|
|
378
407
|
// case, we should handle shift+tab as if focus were on the container's
|
|
379
408
|
// first tabbable node, and go to the last tabbable node of the LAST group
|
|
380
409
|
startOfGroupIndex = containerIndex;
|
|
@@ -393,11 +422,13 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
393
422
|
// is the target the last tabbable node in a group?
|
|
394
423
|
var lastOfGroupIndex = findIndex(state.tabbableGroups, function (_ref3) {
|
|
395
424
|
var lastTabbableNode = _ref3.lastTabbableNode;
|
|
396
|
-
return
|
|
425
|
+
return target === lastTabbableNode;
|
|
397
426
|
});
|
|
398
427
|
|
|
399
|
-
if (lastOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container ===
|
|
400
|
-
// an exception case where the target is the container itself,
|
|
428
|
+
if (lastOfGroupIndex < 0 && (state.tabbableGroups[containerIndex].container === target || isFocusable(target) && !isTabbable(target))) {
|
|
429
|
+
// an exception case where the target is the container itself, or
|
|
430
|
+
// a non-tabbable node that was given focus (i.e. tabindex is negative
|
|
431
|
+
// and user clicked on it or node was programmatically given focus), in which
|
|
401
432
|
// case, we should handle tab as if focus were on the container's
|
|
402
433
|
// last tabbable node, and go to the first tabbable node of the FIRST group
|
|
403
434
|
lastOfGroupIndex = containerIndex;
|
|
@@ -414,6 +445,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
414
445
|
}
|
|
415
446
|
}
|
|
416
447
|
} else {
|
|
448
|
+
// NOTE: the fallbackFocus option does not support returning false to opt-out
|
|
417
449
|
destinationNode = getNodeForOption('fallbackFocus');
|
|
418
450
|
}
|
|
419
451
|
|
|
@@ -425,7 +457,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
425
457
|
};
|
|
426
458
|
|
|
427
459
|
var checkKey = function checkKey(e) {
|
|
428
|
-
if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates) !== false) {
|
|
460
|
+
if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates, e) !== false) {
|
|
429
461
|
e.preventDefault();
|
|
430
462
|
trap.deactivate();
|
|
431
463
|
return;
|
|
@@ -442,7 +474,9 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
442
474
|
return;
|
|
443
475
|
}
|
|
444
476
|
|
|
445
|
-
|
|
477
|
+
var target = getActualTarget(e);
|
|
478
|
+
|
|
479
|
+
if (containersContain(target)) {
|
|
446
480
|
return;
|
|
447
481
|
}
|
|
448
482
|
|