focus-trap 6.6.1 → 6.7.3

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 CHANGED
@@ -1,5 +1,36 @@
1
1
  # Changelog
2
2
 
3
+ ## 6.7.3
4
+
5
+ ### Patch Changes
6
+
7
+ - ab20d3d: Fix issue with focusing negative tabindex node and then tabbing away when this node is _not_ the last node in the trap's container ((#611)[https://github.com/focus-trap/focus-trap/issues/611])
8
+
9
+ ## 6.7.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 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))
14
+
15
+ ## 6.7.1
16
+
17
+ ### Patch Changes
18
+
19
+ - 28a069f: Fix bug from #504 where it's no longer possible to create a trap without any options [#525]
20
+
21
+ ## 6.7.0
22
+
23
+ ### Minor Changes
24
+
25
+ - 893dd2c: Add `document` option to support focus traps inside `<iframe>` elements (#97)
26
+ - 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)
27
+
28
+ ### Patch Changes
29
+
30
+ - 60162eb: Fix bug where `KeyboardEvent` was not being passed to `escapeDeactivates` option when it's a function (#498)
31
+ - 7b6abfa: Fix how focus-trap determines the event's target, which was preventing traps inside open shadow DOMs from working properly (#496)
32
+ - 14b0ee8: Fix `initialFocus` option not supporting function returning `false` as documented (#490)
33
+
3
34
  ## 6.6.1
4
35
 
5
36
  ### Patch Changes
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # focus-trap [![CI](https://github.com/focus-trap/focus-trap/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/focus-trap/focus-trap/actions?query=workflow:CI+branch:master) [![license](https://badgen.now.sh/badge/license/MIT)](./LICENSE)
2
2
 
3
3
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
4
- [![All Contributors](https://img.shields.io/badge/all_contributors-17-orange.svg?style=flat-square)](#contributors)
4
+ [![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#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, focus trap on deactivation will return to the element that was focused before activation. With this option you can specify another element to programmatically receive focus after deactivation. 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.
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('../../dist/focus-trap');
193
+ const { createFocusTrap } = require('../../index');
180
194
 
181
195
  const container = document.getElementById('default');
182
196
 
@@ -232,27 +246,30 @@ 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/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></td>
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>
272
+ <td align="center"><a href="https://github.com/jpveooys"><img src="https://avatars.githubusercontent.com/u/66470099?v=4?s=100" width="100px;" alt=""/><br /><sub><b>jpveooys</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Ajpveooys" title="Bug reports">🐛</a></td>
256
273
  </tr>
257
274
  </table>
258
275
 
@@ -1,22 +1,17 @@
1
1
  /*!
2
- * focus-trap 6.6.1
2
+ * focus-trap 6.7.3
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, focusable, isTabbable, isFocusable } 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
- if (enumerableOnly) {
14
- symbols = symbols.filter(function (sym) {
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] != null ? arguments[i] : {};
28
-
29
- if (i % 2) {
30
- ownKeys(Object(source), true).forEach(function (key) {
31
- _defineProperty(target, key, source[key]);
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
- var doc = document;
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,
@@ -158,7 +159,12 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
158
159
  // is active, but the trap should never get to a state where there isn't at least one group
159
160
  // with at least one tabbable node in it (that would lead to an error condition that would
160
161
  // result in an error being thrown)
161
- // @type {Array<{ container: HTMLElement, firstTabbableNode: HTMLElement|null, lastTabbableNode: HTMLElement|null }>}
162
+ // @type {Array<{
163
+ // container: HTMLElement,
164
+ // firstTabbableNode: HTMLElement|null,
165
+ // lastTabbableNode: HTMLElement|null,
166
+ // nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined
167
+ // }>}
162
168
  tabbableGroups: [],
163
169
  nodeFocusedBeforeActivation: null,
164
170
  mostRecentlyFocusedNode: null,
@@ -175,33 +181,52 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
175
181
  };
176
182
 
177
183
  var containersContain = function containersContain(element) {
178
- return state.containers.some(function (container) {
184
+ return !!(element && state.containers.some(function (container) {
179
185
  return container.contains(element);
180
- });
186
+ }));
181
187
  };
188
+ /**
189
+ * Gets the node for the given option, which is expected to be an option that
190
+ * can be either a DOM node, a string that is a selector to get a node, `false`
191
+ * (if a node is explicitly NOT given), or a function that returns any of these
192
+ * values.
193
+ * @param {string} optionName
194
+ * @returns {undefined | false | HTMLElement | SVGElement} Returns
195
+ * `undefined` if the option is not specified; `false` if the option
196
+ * resolved to `false` (node explicitly not given); otherwise, the resolved
197
+ * DOM node.
198
+ * @throws {Error} If the option is set, not `false`, and is not, or does not
199
+ * resolve to a node.
200
+ */
201
+
182
202
 
183
203
  var getNodeForOption = function getNodeForOption(optionName) {
184
204
  var optionValue = config[optionName];
185
205
 
186
- if (!optionValue) {
187
- return null;
206
+ if (typeof optionValue === 'function') {
207
+ for (var _len2 = arguments.length, params = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
208
+ params[_key2 - 1] = arguments[_key2];
209
+ }
210
+
211
+ optionValue = optionValue.apply(void 0, params);
188
212
  }
189
213
 
190
- var node = optionValue;
214
+ if (!optionValue) {
215
+ if (optionValue === undefined || optionValue === false) {
216
+ return optionValue;
217
+ } // else, empty string (invalid), null (invalid), 0 (invalid)
191
218
 
192
- if (typeof optionValue === 'string') {
193
- node = doc.querySelector(optionValue);
194
219
 
195
- if (!node) {
196
- throw new Error("`".concat(optionName, "` refers to no known node"));
197
- }
220
+ throw new Error("`".concat(optionName, "` was specified but was not a node, or did not return a node"));
198
221
  }
199
222
 
200
- if (typeof optionValue === 'function') {
201
- node = optionValue();
223
+ var node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point
224
+
225
+ if (typeof optionValue === 'string') {
226
+ node = doc.querySelector(optionValue); // resolve to node, or null if fails
202
227
 
203
228
  if (!node) {
204
- throw new Error("`".concat(optionName, "` did not return a node"));
229
+ throw new Error("`".concat(optionName, "` as selector refers to no known node"));
205
230
  }
206
231
  }
207
232
 
@@ -209,20 +234,22 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
209
234
  };
210
235
 
211
236
  var getInitialFocusNode = function getInitialFocusNode() {
212
- var node; // false indicates we want no initialFocus at all
237
+ var node = getNodeForOption('initialFocus'); // false explicitly indicates we want no initialFocus at all
213
238
 
214
- if (getOption({}, 'initialFocus') === false) {
239
+ if (node === false) {
215
240
  return false;
216
241
  }
217
242
 
218
- if (getNodeForOption('initialFocus') !== null) {
219
- node = getNodeForOption('initialFocus');
220
- } else if (containersContain(doc.activeElement)) {
221
- node = doc.activeElement;
222
- } else {
223
- var firstTabbableGroup = state.tabbableGroups[0];
224
- var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode;
225
- node = firstTabbableNode || getNodeForOption('fallbackFocus');
243
+ if (node === undefined) {
244
+ // option not specified: use fallback options
245
+ if (containersContain(doc.activeElement)) {
246
+ node = doc.activeElement;
247
+ } else {
248
+ var firstTabbableGroup = state.tabbableGroups[0];
249
+ var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode; // NOTE: `fallbackFocus` option function cannot return `false` (not supported)
250
+
251
+ node = firstTabbableNode || getNodeForOption('fallbackFocus');
252
+ }
226
253
  }
227
254
 
228
255
  if (!node) {
@@ -234,13 +261,51 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
234
261
 
235
262
  var updateTabbableNodes = function updateTabbableNodes() {
236
263
  state.tabbableGroups = state.containers.map(function (container) {
237
- var tabbableNodes = tabbable(container);
264
+ var tabbableNodes = tabbable(container); // NOTE: if we have tabbable nodes, we must have focusable nodes; focusable nodes
265
+ // are a superset of tabbable nodes
266
+
267
+ var focusableNodes = focusable(container);
238
268
 
239
269
  if (tabbableNodes.length > 0) {
240
270
  return {
241
271
  container: container,
242
272
  firstTabbableNode: tabbableNodes[0],
243
- lastTabbableNode: tabbableNodes[tabbableNodes.length - 1]
273
+ lastTabbableNode: tabbableNodes[tabbableNodes.length - 1],
274
+
275
+ /**
276
+ * Finds the __tabbable__ node that follows the given node in the specified direction,
277
+ * in this container, if any.
278
+ * @param {HTMLElement} node
279
+ * @param {boolean} [forward] True if going in forward tab order; false if going
280
+ * in reverse.
281
+ * @returns {HTMLElement|undefined} The next tabbable node, if any.
282
+ */
283
+ nextTabbableNode: function nextTabbableNode(node) {
284
+ var forward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
285
+ // NOTE: If tabindex is positive (in order to manipulate the tab order separate
286
+ // from the DOM order), this __will not work__ because the list of focusableNodes,
287
+ // while it contains tabbable nodes, does not sort its nodes in any order other
288
+ // than DOM order, because it can't: Where would you place focusable (but not
289
+ // tabbable) nodes in that order? They have no order, because they aren't tabbale...
290
+ // Support for positive tabindex is already broken and hard to manage (possibly
291
+ // not supportable, TBD), so this isn't going to make things worse than they
292
+ // already are, and at least makes things better for the majority of cases where
293
+ // tabindex is either 0/unset or negative.
294
+ // FYI, positive tabindex issue: https://github.com/focus-trap/focus-trap/issues/375
295
+ var nodeIdx = focusableNodes.findIndex(function (n) {
296
+ return n === node;
297
+ });
298
+
299
+ if (forward) {
300
+ return focusableNodes.slice(nodeIdx + 1).find(function (n) {
301
+ return isTabbable(n);
302
+ });
303
+ }
304
+
305
+ return focusableNodes.slice(0, nodeIdx).reverse().find(function (n) {
306
+ return isTabbable(n);
307
+ });
308
+ }
244
309
  };
245
310
  }
246
311
 
@@ -250,7 +315,8 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
250
315
  }); // remove groups with no tabbable nodes
251
316
  // throw if no groups have tabbable nodes and we don't have a fallback focus node either
252
317
 
253
- if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus')) {
318
+ if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus') // returning false not supported for this option
319
+ ) {
254
320
  throw new Error('Your focus-trap must have at least one container with at least one tabbable node in it at all times');
255
321
  }
256
322
  };
@@ -280,14 +346,16 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
280
346
  };
281
347
 
282
348
  var getReturnFocusNode = function getReturnFocusNode(previousActiveElement) {
283
- var node = getNodeForOption('setReturnFocus');
284
- return node ? node : previousActiveElement;
349
+ var node = getNodeForOption('setReturnFocus', previousActiveElement);
350
+ return node ? node : node === false ? false : previousActiveElement;
285
351
  }; // This needs to be done on mousedown and touchstart instead of click
286
352
  // so that it precedes the focus event.
287
353
 
288
354
 
289
355
  var checkPointerDown = function checkPointerDown(e) {
290
- if (containersContain(e.target)) {
356
+ var target = getActualTarget(e);
357
+
358
+ if (containersContain(target)) {
291
359
  // allow the click since it ocurred inside the trap
292
360
  return;
293
361
  }
@@ -306,7 +374,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
306
374
  // that was clicked, whether it's focusable or not; by setting
307
375
  // `returnFocus: true`, we'll attempt to re-focus the node originally-focused
308
376
  // on activation (or the configured `setReturnFocus` node)
309
- returnFocus: config.returnFocusOnDeactivate && !isFocusable(e.target)
377
+ returnFocus: config.returnFocusOnDeactivate && !isFocusable(target)
310
378
  });
311
379
  return;
312
380
  } // This is needed for mobile devices.
@@ -325,11 +393,12 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
325
393
 
326
394
 
327
395
  var checkFocusIn = function checkFocusIn(e) {
328
- var targetContained = containersContain(e.target); // In Firefox when you Tab out of an iframe the Document is briefly focused.
396
+ var target = getActualTarget(e);
397
+ var targetContained = containersContain(target); // In Firefox when you Tab out of an iframe the Document is briefly focused.
329
398
 
330
- if (targetContained || e.target instanceof Document) {
399
+ if (targetContained || target instanceof Document) {
331
400
  if (targetContained) {
332
- state.mostRecentlyFocusedNode = e.target;
401
+ state.mostRecentlyFocusedNode = target;
333
402
  }
334
403
  } else {
335
404
  // escaped! pull it back in to where it just left
@@ -343,17 +412,19 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
343
412
 
344
413
 
345
414
  var checkTab = function checkTab(e) {
415
+ var target = getActualTarget(e);
346
416
  updateTabbableNodes();
347
417
  var destinationNode = null;
348
418
 
349
419
  if (state.tabbableGroups.length > 0) {
350
420
  // make sure the target is actually contained in a group
351
- // NOTE: the target may also be the container itself if it's tabbable
421
+ // NOTE: the target may also be the container itself if it's focusable
352
422
  // with tabIndex='-1' and was given initial focus
353
423
  var containerIndex = findIndex(state.tabbableGroups, function (_ref) {
354
424
  var container = _ref.container;
355
- return container.contains(e.target);
425
+ return container.contains(target);
356
426
  });
427
+ var containerGroup = containerIndex >= 0 ? state.tabbableGroups[containerIndex] : undefined;
357
428
 
358
429
  if (containerIndex < 0) {
359
430
  // target not found in any group: quite possible focus has escaped the trap,
@@ -370,11 +441,14 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
370
441
  // is the target the first tabbable node in a group?
371
442
  var startOfGroupIndex = findIndex(state.tabbableGroups, function (_ref2) {
372
443
  var firstTabbableNode = _ref2.firstTabbableNode;
373
- return e.target === firstTabbableNode;
444
+ return target === firstTabbableNode;
374
445
  });
375
446
 
376
- if (startOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container === e.target) {
377
- // an exception case where the target is the container itself, in which
447
+ if (startOfGroupIndex < 0 && (containerGroup.container === target || isFocusable(target) && !isTabbable(target) && !containerGroup.nextTabbableNode(target, false))) {
448
+ // an exception case where the target is either the container itself, or
449
+ // a non-tabbable node that was given focus (i.e. tabindex is negative
450
+ // and user clicked on it or node was programmatically given focus)
451
+ // and is not followed by any other tabbable node, in which
378
452
  // case, we should handle shift+tab as if focus were on the container's
379
453
  // first tabbable node, and go to the last tabbable node of the LAST group
380
454
  startOfGroupIndex = containerIndex;
@@ -393,11 +467,14 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
393
467
  // is the target the last tabbable node in a group?
394
468
  var lastOfGroupIndex = findIndex(state.tabbableGroups, function (_ref3) {
395
469
  var lastTabbableNode = _ref3.lastTabbableNode;
396
- return e.target === lastTabbableNode;
470
+ return target === lastTabbableNode;
397
471
  });
398
472
 
399
- if (lastOfGroupIndex < 0 && state.tabbableGroups[containerIndex].container === e.target) {
400
- // an exception case where the target is the container itself, in which
473
+ if (lastOfGroupIndex < 0 && (containerGroup.container === target || isFocusable(target) && !isTabbable(target) && !containerGroup.nextTabbableNode(target))) {
474
+ // an exception case where the target is the container itself, or
475
+ // a non-tabbable node that was given focus (i.e. tabindex is negative
476
+ // and user clicked on it or node was programmatically given focus)
477
+ // and is not followed by any other tabbable node, in which
401
478
  // case, we should handle tab as if focus were on the container's
402
479
  // last tabbable node, and go to the first tabbable node of the FIRST group
403
480
  lastOfGroupIndex = containerIndex;
@@ -414,6 +491,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
414
491
  }
415
492
  }
416
493
  } else {
494
+ // NOTE: the fallbackFocus option does not support returning false to opt-out
417
495
  destinationNode = getNodeForOption('fallbackFocus');
418
496
  }
419
497
 
@@ -425,7 +503,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
425
503
  };
426
504
 
427
505
  var checkKey = function checkKey(e) {
428
- if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates) !== false) {
506
+ if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates, e) !== false) {
429
507
  e.preventDefault();
430
508
  trap.deactivate();
431
509
  return;
@@ -442,7 +520,9 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
442
520
  return;
443
521
  }
444
522
 
445
- if (containersContain(e.target)) {
523
+ var target = getActualTarget(e);
524
+
525
+ if (containersContain(target)) {
446
526
  return;
447
527
  }
448
528