| 1 |
/*! |
| 2 |
* focus-trap 7.6.6 |
| 3 |
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE |
| 4 |
*/ |
| 5 |
(function (global, factory) { |
| 6 |
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('tabbable')) : |
| 7 |
typeof define === 'function' && define.amd ? define(['exports', 'tabbable'], factory) : |
| 8 |
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, (function () { |
| 9 |
var current = global.focusTrap; |
| 10 |
var exports = global.focusTrap = {}; |
| 11 |
factory(exports, global.tabbable); |
| 12 |
exports.noConflict = function () { global.focusTrap = current; return exports; }; |
| 13 |
})()); |
| 14 |
})(this, (function (exports, tabbable) { 'use strict'; |
| 15 |
|
| 16 |
function _arrayLikeToArray(r, a) { |
| 17 |
(null == a || a > r.length) && (a = r.length); |
| 18 |
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; |
| 19 |
return n; |
| 20 |
} |
| 21 |
function _arrayWithoutHoles(r) { |
| 22 |
if (Array.isArray(r)) return _arrayLikeToArray(r); |
| 23 |
} |
| 24 |
function _defineProperty(e, r, t) { |
| 25 |
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { |
| 26 |
value: t, |
| 27 |
enumerable: true, |
| 28 |
configurable: true, |
| 29 |
writable: true |
| 30 |
}) : e[r] = t, e; |
| 31 |
} |
| 32 |
function _iterableToArray(r) { |
| 33 |
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); |
| 34 |
} |
| 35 |
function _nonIterableSpread() { |
| 36 |
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); |
| 37 |
} |
| 38 |
function ownKeys(e, r) { |
| 39 |
var t = Object.keys(e); |
| 40 |
if (Object.getOwnPropertySymbols) { |
| 41 |
var o = Object.getOwnPropertySymbols(e); |
| 42 |
r && (o = o.filter(function (r) { |
| 43 |
return Object.getOwnPropertyDescriptor(e, r).enumerable; |
| 44 |
})), t.push.apply(t, o); |
| 45 |
} |
| 46 |
return t; |
| 47 |
} |
| 48 |
function _objectSpread2(e) { |
| 49 |
for (var r = 1; r < arguments.length; r++) { |
| 50 |
var t = null != arguments[r] ? arguments[r] : {}; |
| 51 |
r % 2 ? ownKeys(Object(t), true).forEach(function (r) { |
| 52 |
_defineProperty(e, r, t[r]); |
| 53 |
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { |
| 54 |
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); |
| 55 |
}); |
| 56 |
} |
| 57 |
return e; |
| 58 |
} |
| 59 |
function _toConsumableArray(r) { |
| 60 |
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); |
| 61 |
} |
| 62 |
function _toPrimitive(t, r) { |
| 63 |
if ("object" != typeof t || !t) return t; |
| 64 |
var e = t[Symbol.toPrimitive]; |
| 65 |
if (void 0 !== e) { |
| 66 |
var i = e.call(t, r); |
| 67 |
if ("object" != typeof i) return i; |
| 68 |
throw new TypeError("@@toPrimitive must return a primitive value."); |
| 69 |
} |
| 70 |
return ("string" === r ? String : Number)(t); |
| 71 |
} |
| 72 |
function _toPropertyKey(t) { |
| 73 |
var i = _toPrimitive(t, "string"); |
| 74 |
return "symbol" == typeof i ? i : i + ""; |
| 75 |
} |
| 76 |
function _unsupportedIterableToArray(r, a) { |
| 77 |
if (r) { |
| 78 |
if ("string" == typeof r) return _arrayLikeToArray(r, a); |
| 79 |
var t = {}.toString.call(r).slice(8, -1); |
| 80 |
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
var activeFocusTraps = { |
| 85 |
activateTrap: function activateTrap(trapStack, trap) { |
| 86 |
if (trapStack.length > 0) { |
| 87 |
var activeTrap = trapStack[trapStack.length - 1]; |
| 88 |
if (activeTrap !== trap) { |
| 89 |
activeTrap._setPausedState(true); |
| 90 |
} |
| 91 |
} |
| 92 |
var trapIndex = trapStack.indexOf(trap); |
| 93 |
if (trapIndex === -1) { |
| 94 |
trapStack.push(trap); |
| 95 |
} else { |
| 96 |
// move this existing trap to the front of the queue |
| 97 |
trapStack.splice(trapIndex, 1); |
| 98 |
trapStack.push(trap); |
| 99 |
} |
| 100 |
}, |
| 101 |
deactivateTrap: function deactivateTrap(trapStack, trap) { |
| 102 |
var trapIndex = trapStack.indexOf(trap); |
| 103 |
if (trapIndex !== -1) { |
| 104 |
trapStack.splice(trapIndex, 1); |
| 105 |
} |
| 106 |
if (trapStack.length > 0 && !trapStack[trapStack.length - 1]._isManuallyPaused()) { |
| 107 |
trapStack[trapStack.length - 1]._setPausedState(false); |
| 108 |
} |
| 109 |
} |
| 110 |
}; |
| 111 |
var isSelectableInput = function isSelectableInput(node) { |
| 112 |
return node.tagName && node.tagName.toLowerCase() === 'input' && typeof node.select === 'function'; |
| 113 |
}; |
| 114 |
var isEscapeEvent = function isEscapeEvent(e) { |
| 115 |
return (e === null || e === void 0 ? void 0 : e.key) === 'Escape' || (e === null || e === void 0 ? void 0 : e.key) === 'Esc' || (e === null || e === void 0 ? void 0 : e.keyCode) === 27; |
| 116 |
}; |
| 117 |
var isTabEvent = function isTabEvent(e) { |
| 118 |
return (e === null || e === void 0 ? void 0 : e.key) === 'Tab' || (e === null || e === void 0 ? void 0 : e.keyCode) === 9; |
| 119 |
}; |
| 120 |
|
| 121 |
// checks for TAB by default |
| 122 |
var isKeyForward = function isKeyForward(e) { |
| 123 |
return isTabEvent(e) && !e.shiftKey; |
| 124 |
}; |
| 125 |
|
| 126 |
// checks for SHIFT+TAB by default |
| 127 |
var isKeyBackward = function isKeyBackward(e) { |
| 128 |
return isTabEvent(e) && e.shiftKey; |
| 129 |
}; |
| 130 |
var delay = function delay(fn) { |
| 131 |
return setTimeout(fn, 0); |
| 132 |
}; |
| 133 |
|
| 134 |
/** |
| 135 |
* Get an option's value when it could be a plain value, or a handler that provides |
| 136 |
* the value. |
| 137 |
* @param {*} value Option's value to check. |
| 138 |
* @param {...*} [params] Any parameters to pass to the handler, if `value` is a function. |
| 139 |
* @returns {*} The `value`, or the handler's returned value. |
| 140 |
*/ |
| 141 |
var valueOrHandler = function valueOrHandler(value) { |
| 142 |
for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { |
| 143 |
params[_key - 1] = arguments[_key]; |
| 144 |
} |
| 145 |
return typeof value === 'function' ? value.apply(void 0, params) : value; |
| 146 |
}; |
| 147 |
var getActualTarget = function getActualTarget(event) { |
| 148 |
// NOTE: If the trap is _inside_ a shadow DOM, event.target will always be the |
| 149 |
// shadow host. However, event.target.composedPath() will be an array of |
| 150 |
// nodes "clicked" from inner-most (the actual element inside the shadow) to |
| 151 |
// outer-most (the host HTML document). If we have access to composedPath(), |
| 152 |
// then use its first element; otherwise, fall back to event.target (and |
| 153 |
// this only works for an _open_ shadow DOM; otherwise, |
| 154 |
// composedPath()[0] === event.target always). |
| 155 |
return event.target.shadowRoot && typeof event.composedPath === 'function' ? event.composedPath()[0] : event.target; |
| 156 |
}; |
| 157 |
|
| 158 |
// NOTE: this must be _outside_ `createFocusTrap()` to make sure all traps in this |
| 159 |
// current instance use the same stack if `userOptions.trapStack` isn't specified |
| 160 |
var internalTrapStack = []; |
| 161 |
var createFocusTrap = function createFocusTrap(elements, userOptions) { |
| 162 |
// SSR: a live trap shouldn't be created in this type of environment so this |
| 163 |
// should be safe code to execute if the `document` option isn't specified |
| 164 |
var doc = (userOptions === null || userOptions === void 0 ? void 0 : userOptions.document) || document; |
| 165 |
var trapStack = (userOptions === null || userOptions === void 0 ? void 0 : userOptions.trapStack) || internalTrapStack; |
| 166 |
var config = _objectSpread2({ |
| 167 |
returnFocusOnDeactivate: true, |
| 168 |
escapeDeactivates: true, |
| 169 |
delayInitialFocus: true, |
| 170 |
isKeyForward: isKeyForward, |
| 171 |
isKeyBackward: isKeyBackward |
| 172 |
}, userOptions); |
| 173 |
var state = { |
| 174 |
// containers given to createFocusTrap() |
| 175 |
// @type {Array<HTMLElement>} |
| 176 |
containers: [], |
| 177 |
// list of objects identifying tabbable nodes in `containers` in the trap |
| 178 |
// NOTE: it's possible that a group has no tabbable nodes if nodes get removed while the trap |
| 179 |
// is active, but the trap should never get to a state where there isn't at least one group |
| 180 |
// with at least one tabbable node in it (that would lead to an error condition that would |
| 181 |
// result in an error being thrown) |
| 182 |
// @type {Array<{ |
| 183 |
// container: HTMLElement, |
| 184 |
// tabbableNodes: Array<HTMLElement>, // empty if none |
| 185 |
// focusableNodes: Array<HTMLElement>, // empty if none |
| 186 |
// posTabIndexesFound: boolean, |
| 187 |
// firstTabbableNode: HTMLElement|undefined, |
| 188 |
// lastTabbableNode: HTMLElement|undefined, |
| 189 |
// firstDomTabbableNode: HTMLElement|undefined, |
| 190 |
// lastDomTabbableNode: HTMLElement|undefined, |
| 191 |
// nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined |
| 192 |
// }>} |
| 193 |
containerGroups: [], |
| 194 |
// same order/length as `containers` list |
| 195 |
|
| 196 |
// references to objects in `containerGroups`, but only those that actually have |
| 197 |
// tabbable nodes in them |
| 198 |
// NOTE: same order as `containers` and `containerGroups`, but __not necessarily__ |
| 199 |
// the same length |
| 200 |
tabbableGroups: [], |
| 201 |
nodeFocusedBeforeActivation: null, |
| 202 |
mostRecentlyFocusedNode: null, |
| 203 |
active: false, |
| 204 |
paused: false, |
| 205 |
manuallyPaused: false, |
| 206 |
// timer ID for when delayInitialFocus is true and initial focus in this trap |
| 207 |
// has been delayed during activation |
| 208 |
delayInitialFocusTimer: undefined, |
| 209 |
// the most recent KeyboardEvent for the configured nav key (typically [SHIFT+]TAB), if any |
| 210 |
recentNavEvent: undefined |
| 211 |
}; |
| 212 |
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 |
| 213 |
|
| 214 |
/** |
| 215 |
* Gets a configuration option value. |
| 216 |
* @param {Object|undefined} configOverrideOptions If true, and option is defined in this set, |
| 217 |
* value will be taken from this object. Otherwise, value will be taken from base configuration. |
| 218 |
* @param {string} optionName Name of the option whose value is sought. |
| 219 |
* @param {string|undefined} [configOptionName] Name of option to use __instead of__ `optionName` |
| 220 |
* IIF `configOverrideOptions` is not defined. Otherwise, `optionName` is used. |
| 221 |
*/ |
| 222 |
var getOption = function getOption(configOverrideOptions, optionName, configOptionName) { |
| 223 |
return configOverrideOptions && configOverrideOptions[optionName] !== undefined ? configOverrideOptions[optionName] : config[configOptionName || optionName]; |
| 224 |
}; |
| 225 |
|
| 226 |
/** |
| 227 |
* Finds the index of the container that contains the element. |
| 228 |
* @param {HTMLElement} element |
| 229 |
* @param {Event} [event] If available, and `element` isn't directly found in any container, |
| 230 |
* the event's composed path is used to see if includes any known trap containers in the |
| 231 |
* case where the element is inside a Shadow DOM. |
| 232 |
* @returns {number} Index of the container in either `state.containers` or |
| 233 |
* `state.containerGroups` (the order/length of these lists are the same); -1 |
| 234 |
* if the element isn't found. |
| 235 |
*/ |
| 236 |
var findContainerIndex = function findContainerIndex(element, event) { |
| 237 |
var composedPath = typeof (event === null || event === void 0 ? void 0 : event.composedPath) === 'function' ? event.composedPath() : undefined; |
| 238 |
// NOTE: search `containerGroups` because it's possible a group contains no tabbable |
| 239 |
// nodes, but still contains focusable nodes (e.g. if they all have `tabindex=-1`) |
| 240 |
// and we still need to find the element in there |
| 241 |
return state.containerGroups.findIndex(function (_ref) { |
| 242 |
var container = _ref.container, |
| 243 |
tabbableNodes = _ref.tabbableNodes; |
| 244 |
return container.contains(element) || (// fall back to explicit tabbable search which will take into consideration any |
| 245 |
// web components if the `tabbableOptions.getShadowRoot` option was used for |
| 246 |
// the trap, enabling shadow DOM support in tabbable (`Node.contains()` doesn't |
| 247 |
// look inside web components even if open) |
| 248 |
composedPath === null || composedPath === void 0 ? void 0 : composedPath.includes(container)) || tabbableNodes.find(function (node) { |
| 249 |
return node === element; |
| 250 |
}); |
| 251 |
}); |
| 252 |
}; |
| 253 |
|
| 254 |
/** |
| 255 |
* Gets the node for the given option, which is expected to be an option that |
| 256 |
* can be either a DOM node, a string that is a selector to get a node, `false` |
| 257 |
* (if a node is explicitly NOT given), or a function that returns any of these |
| 258 |
* values. |
| 259 |
* @param {string} optionName |
| 260 |
* @param {Object} options |
| 261 |
* @param {boolean} [options.hasFallback] True if the option could be a selector string |
| 262 |
* and the option allows for a fallback scenario in the case where the selector is |
| 263 |
* valid but does not match a node (i.e. the queried node doesn't exist in the DOM). |
| 264 |
* @param {Array} [options.params] Params to pass to the option if it's a function. |
| 265 |
* @returns {undefined | null | false | HTMLElement | SVGElement} Returns |
| 266 |
* `undefined` if the option is not specified; `null` if the option didn't resolve |
| 267 |
* to a node but `options.hasFallback=true`, `false` if the option resolved to `false` |
| 268 |
* (node explicitly not given); otherwise, the resolved DOM node. |
| 269 |
* @throws {Error} If the option is set, not `false`, and is not, or does not |
| 270 |
* resolve to a node, unless the option is a selector string and `options.hasFallback=true`. |
| 271 |
*/ |
| 272 |
var getNodeForOption = function getNodeForOption(optionName) { |
| 273 |
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, |
| 274 |
_ref2$hasFallback = _ref2.hasFallback, |
| 275 |
hasFallback = _ref2$hasFallback === void 0 ? false : _ref2$hasFallback, |
| 276 |
_ref2$params = _ref2.params, |
| 277 |
params = _ref2$params === void 0 ? [] : _ref2$params; |
| 278 |
var optionValue = config[optionName]; |
| 279 |
if (typeof optionValue === 'function') { |
| 280 |
optionValue = optionValue.apply(void 0, _toConsumableArray(params)); |
| 281 |
} |
| 282 |
if (optionValue === true) { |
| 283 |
optionValue = undefined; // use default value |
| 284 |
} |
| 285 |
if (!optionValue) { |
| 286 |
if (optionValue === undefined || optionValue === false) { |
| 287 |
return optionValue; |
| 288 |
} |
| 289 |
// else, empty string (invalid), null (invalid), 0 (invalid) |
| 290 |
|
| 291 |
throw new Error("`".concat(optionName, "` was specified but was not a node, or did not return a node")); |
| 292 |
} |
| 293 |
var node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point |
| 294 |
|
| 295 |
if (typeof optionValue === 'string') { |
| 296 |
try { |
| 297 |
node = doc.querySelector(optionValue); // resolve to node, or null if fails |
| 298 |
} catch (err) { |
| 299 |
throw new Error("`".concat(optionName, "` appears to be an invalid selector; error=\"").concat(err.message, "\"")); |
| 300 |
} |
| 301 |
if (!node) { |
| 302 |
if (!hasFallback) { |
| 303 |
throw new Error("`".concat(optionName, "` as selector refers to no known node")); |
| 304 |
} |
| 305 |
// else, `node` MUST be `null` because that's what `Document.querySelector()` returns |
| 306 |
// if the selector is valid but doesn't match anything |
| 307 |
} |
| 308 |
} |
| 309 |
return node; |
| 310 |
}; |
| 311 |
var getInitialFocusNode = function getInitialFocusNode() { |
| 312 |
var node = getNodeForOption('initialFocus', { |
| 313 |
hasFallback: true |
| 314 |
}); |
| 315 |
|
| 316 |
// false explicitly indicates we want no initialFocus at all |
| 317 |
if (node === false) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
if (node === undefined || node && !tabbable.isFocusable(node, config.tabbableOptions)) { |
| 321 |
// option not specified nor focusable: use fallback options |
| 322 |
if (findContainerIndex(doc.activeElement) >= 0) { |
| 323 |
node = doc.activeElement; |
| 324 |
} else { |
| 325 |
var firstTabbableGroup = state.tabbableGroups[0]; |
| 326 |
var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode; |
| 327 |
|
| 328 |
// NOTE: `fallbackFocus` option function cannot return `false` (not supported) |
| 329 |
node = firstTabbableNode || getNodeForOption('fallbackFocus'); |
| 330 |
} |
| 331 |
} else if (node === null) { |
| 332 |
// option is a VALID selector string that doesn't yield a node: use the `fallbackFocus` |
| 333 |
// option instead of the default behavior when the option isn't specified at all |
| 334 |
node = getNodeForOption('fallbackFocus'); |
| 335 |
} |
| 336 |
if (!node) { |
| 337 |
throw new Error('Your focus-trap needs to have at least one focusable element'); |
| 338 |
} |
| 339 |
return node; |
| 340 |
}; |
| 341 |
var updateTabbableNodes = function updateTabbableNodes() { |
| 342 |
state.containerGroups = state.containers.map(function (container) { |
| 343 |
var tabbableNodes = tabbable.tabbable(container, config.tabbableOptions); |
| 344 |
|
| 345 |
// NOTE: if we have tabbable nodes, we must have focusable nodes; focusable nodes |
| 346 |
// are a superset of tabbable nodes since nodes with negative `tabindex` attributes |
| 347 |
// are focusable but not tabbable |
| 348 |
var focusableNodes = tabbable.focusable(container, config.tabbableOptions); |
| 349 |
var firstTabbableNode = tabbableNodes.length > 0 ? tabbableNodes[0] : undefined; |
| 350 |
var lastTabbableNode = tabbableNodes.length > 0 ? tabbableNodes[tabbableNodes.length - 1] : undefined; |
| 351 |
var firstDomTabbableNode = focusableNodes.find(function (node) { |
| 352 |
return tabbable.isTabbable(node); |
| 353 |
}); |
| 354 |
var lastDomTabbableNode = focusableNodes.slice().reverse().find(function (node) { |
| 355 |
return tabbable.isTabbable(node); |
| 356 |
}); |
| 357 |
var posTabIndexesFound = !!tabbableNodes.find(function (node) { |
| 358 |
return tabbable.getTabIndex(node) > 0; |
| 359 |
}); |
| 360 |
return { |
| 361 |
container: container, |
| 362 |
tabbableNodes: tabbableNodes, |
| 363 |
focusableNodes: focusableNodes, |
| 364 |
/** True if at least one node with positive `tabindex` was found in this container. */ |
| 365 |
posTabIndexesFound: posTabIndexesFound, |
| 366 |
/** First tabbable node in container, __tabindex__ order; `undefined` if none. */ |
| 367 |
firstTabbableNode: firstTabbableNode, |
| 368 |
/** Last tabbable node in container, __tabindex__ order; `undefined` if none. */ |
| 369 |
lastTabbableNode: lastTabbableNode, |
| 370 |
// NOTE: DOM order is NOT NECESSARILY "document position" order, but figuring that out |
| 371 |
// would require more than just https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition |
| 372 |
// because that API doesn't work with Shadow DOM as well as it should (@see |
| 373 |
// https://github.com/whatwg/dom/issues/320) and since this first/last is only needed, so far, |
| 374 |
// to address an edge case related to positive tabindex support, this seems like a much easier, |
| 375 |
// "close enough most of the time" alternative for positive tabindexes which should generally |
| 376 |
// be avoided anyway... |
| 377 |
/** First tabbable node in container, __DOM__ order; `undefined` if none. */ |
| 378 |
firstDomTabbableNode: firstDomTabbableNode, |
| 379 |
/** Last tabbable node in container, __DOM__ order; `undefined` if none. */ |
| 380 |
lastDomTabbableNode: lastDomTabbableNode, |
| 381 |
/** |
| 382 |
* Finds the __tabbable__ node that follows the given node in the specified direction, |
| 383 |
* in this container, if any. |
| 384 |
* @param {HTMLElement} node |
| 385 |
* @param {boolean} [forward] True if going in forward tab order; false if going |
| 386 |
* in reverse. |
| 387 |
* @returns {HTMLElement|undefined} The next tabbable node, if any. |
| 388 |
*/ |
| 389 |
nextTabbableNode: function nextTabbableNode(node) { |
| 390 |
var forward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; |
| 391 |
var nodeIdx = tabbableNodes.indexOf(node); |
| 392 |
if (nodeIdx < 0) { |
| 393 |
// either not tabbable nor focusable, or was focused but not tabbable (negative tabindex): |
| 394 |
// since `node` should at least have been focusable, we assume that's the case and mimic |
| 395 |
// what browsers do, which is set focus to the next node in __document position order__, |
| 396 |
// regardless of positive tabindexes, if any -- and for reasons explained in the NOTE |
| 397 |
// above related to `firstDomTabbable` and `lastDomTabbable` properties, we fall back to |
| 398 |
// basic DOM order |
| 399 |
if (forward) { |
| 400 |
return focusableNodes.slice(focusableNodes.indexOf(node) + 1).find(function (el) { |
| 401 |
return tabbable.isTabbable(el); |
| 402 |
}); |
| 403 |
} |
| 404 |
return focusableNodes.slice(0, focusableNodes.indexOf(node)).reverse().find(function (el) { |
| 405 |
return tabbable.isTabbable(el); |
| 406 |
}); |
| 407 |
} |
| 408 |
return tabbableNodes[nodeIdx + (forward ? 1 : -1)]; |
| 409 |
} |
| 410 |
}; |
| 411 |
}); |
| 412 |
state.tabbableGroups = state.containerGroups.filter(function (group) { |
| 413 |
return group.tabbableNodes.length > 0; |
| 414 |
}); |
| 415 |
|
| 416 |
// throw if no groups have tabbable nodes and we don't have a fallback focus node either |
| 417 |
if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus') // returning false not supported for this option |
| 418 |
) { |
| 419 |
throw new Error('Your focus-trap must have at least one container with at least one tabbable node in it at all times'); |
| 420 |
} |
| 421 |
|
| 422 |
// NOTE: Positive tabindexes are only properly supported in single-container traps because |
| 423 |
// doing it across multiple containers where tabindexes could be all over the place |
| 424 |
// would require Tabbable to support multiple containers, would require additional |
| 425 |
// specialized Shadow DOM support, and would require Tabbable's multi-container support |
| 426 |
// to look at those containers in document position order rather than user-provided |
| 427 |
// order (as they are treated in Focus-trap, for legacy reasons). See discussion on |
| 428 |
// https://github.com/focus-trap/focus-trap/issues/375 for more details. |
| 429 |
if (state.containerGroups.find(function (g) { |
| 430 |
return g.posTabIndexesFound; |
| 431 |
}) && state.containerGroups.length > 1) { |
| 432 |
throw new Error("At least one node with a positive tabindex was found in one of your focus-trap's multiple containers. Positive tabindexes are only supported in single-container focus-traps."); |
| 433 |
} |
| 434 |
}; |
| 435 |
|
| 436 |
/** |
| 437 |
* Gets the current activeElement. If it's a web-component and has open shadow-root |
| 438 |
* it will recursively search inside shadow roots for the "true" activeElement. |
| 439 |
* |
| 440 |
* @param {Document | ShadowRoot} el |
| 441 |
* |
| 442 |
* @returns {HTMLElement} The element that currently has the focus |
| 443 |
**/ |
| 444 |
var _getActiveElement = function getActiveElement(el) { |
| 445 |
var activeElement = el.activeElement; |
| 446 |
if (!activeElement) { |
| 447 |
return; |
| 448 |
} |
| 449 |
if (activeElement.shadowRoot && activeElement.shadowRoot.activeElement !== null) { |
| 450 |
return _getActiveElement(activeElement.shadowRoot); |
| 451 |
} |
| 452 |
return activeElement; |
| 453 |
}; |
| 454 |
var _tryFocus = function tryFocus(node) { |
| 455 |
if (node === false) { |
| 456 |
return; |
| 457 |
} |
| 458 |
if (node === _getActiveElement(document)) { |
| 459 |
return; |
| 460 |
} |
| 461 |
if (!node || !node.focus) { |
| 462 |
_tryFocus(getInitialFocusNode()); |
| 463 |
return; |
| 464 |
} |
| 465 |
node.focus({ |
| 466 |
preventScroll: !!config.preventScroll |
| 467 |
}); |
| 468 |
// NOTE: focus() API does not trigger focusIn event so set MRU node manually |
| 469 |
state.mostRecentlyFocusedNode = node; |
| 470 |
if (isSelectableInput(node)) { |
| 471 |
node.select(); |
| 472 |
} |
| 473 |
}; |
| 474 |
var getReturnFocusNode = function getReturnFocusNode(previousActiveElement) { |
| 475 |
var node = getNodeForOption('setReturnFocus', { |
| 476 |
params: [previousActiveElement] |
| 477 |
}); |
| 478 |
return node ? node : node === false ? false : previousActiveElement; |
| 479 |
}; |
| 480 |
|
| 481 |
/** |
| 482 |
* Finds the next node (in either direction) where focus should move according to a |
| 483 |
* keyboard focus-in event. |
| 484 |
* @param {Object} params |
| 485 |
* @param {Node} [params.target] Known target __from which__ to navigate, if any. |
| 486 |
* @param {KeyboardEvent|FocusEvent} [params.event] Event to use if `target` isn't known (event |
| 487 |
* will be used to determine the `target`). Ignored if `target` is specified. |
| 488 |
* @param {boolean} [params.isBackward] True if focus should move backward. |
| 489 |
* @returns {Node|undefined} The next node, or `undefined` if a next node couldn't be |
| 490 |
* determined given the current state of the trap. |
| 491 |
*/ |
| 492 |
var findNextNavNode = function findNextNavNode(_ref3) { |
| 493 |
var target = _ref3.target, |
| 494 |
event = _ref3.event, |
| 495 |
_ref3$isBackward = _ref3.isBackward, |
| 496 |
isBackward = _ref3$isBackward === void 0 ? false : _ref3$isBackward; |
| 497 |
target = target || getActualTarget(event); |
| 498 |
updateTabbableNodes(); |
| 499 |
var destinationNode = null; |
| 500 |
if (state.tabbableGroups.length > 0) { |
| 501 |
// make sure the target is actually contained in a group |
| 502 |
// NOTE: the target may also be the container itself if it's focusable |
| 503 |
// with tabIndex='-1' and was given initial focus |
| 504 |
var containerIndex = findContainerIndex(target, event); |
| 505 |
var containerGroup = containerIndex >= 0 ? state.containerGroups[containerIndex] : undefined; |
| 506 |
if (containerIndex < 0) { |
| 507 |
// target not found in any group: quite possible focus has escaped the trap, |
| 508 |
// so bring it back into... |
| 509 |
if (isBackward) { |
| 510 |
// ...the last node in the last group |
| 511 |
destinationNode = state.tabbableGroups[state.tabbableGroups.length - 1].lastTabbableNode; |
| 512 |
} else { |
| 513 |
// ...the first node in the first group |
| 514 |
destinationNode = state.tabbableGroups[0].firstTabbableNode; |
| 515 |
} |
| 516 |
} else if (isBackward) { |
| 517 |
// REVERSE |
| 518 |
|
| 519 |
// is the target the first tabbable node in a group? |
| 520 |
var startOfGroupIndex = state.tabbableGroups.findIndex(function (_ref4) { |
| 521 |
var firstTabbableNode = _ref4.firstTabbableNode; |
| 522 |
return target === firstTabbableNode; |
| 523 |
}); |
| 524 |
if (startOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target, config.tabbableOptions) && !tabbable.isTabbable(target, config.tabbableOptions) && !containerGroup.nextTabbableNode(target, false))) { |
| 525 |
// an exception case where the target is either the container itself, or |
| 526 |
// a non-tabbable node that was given focus (i.e. tabindex is negative |
| 527 |
// and user clicked on it or node was programmatically given focus) |
| 528 |
// and is not followed by any other tabbable node, in which |
| 529 |
// case, we should handle shift+tab as if focus were on the container's |
| 530 |
// first tabbable node, and go to the last tabbable node of the LAST group |
| 531 |
startOfGroupIndex = containerIndex; |
| 532 |
} |
| 533 |
if (startOfGroupIndex >= 0) { |
| 534 |
// YES: then shift+tab should go to the last tabbable node in the |
| 535 |
// previous group (and wrap around to the last tabbable node of |
| 536 |
// the LAST group if it's the first tabbable node of the FIRST group) |
| 537 |
var destinationGroupIndex = startOfGroupIndex === 0 ? state.tabbableGroups.length - 1 : startOfGroupIndex - 1; |
| 538 |
var destinationGroup = state.tabbableGroups[destinationGroupIndex]; |
| 539 |
destinationNode = tabbable.getTabIndex(target) >= 0 ? destinationGroup.lastTabbableNode : destinationGroup.lastDomTabbableNode; |
| 540 |
} else if (!isTabEvent(event)) { |
| 541 |
// user must have customized the nav keys so we have to move focus manually _within_ |
| 542 |
// the active group: do this based on the order determined by tabbable() |
| 543 |
destinationNode = containerGroup.nextTabbableNode(target, false); |
| 544 |
} |
| 545 |
} else { |
| 546 |
// FORWARD |
| 547 |
|
| 548 |
// is the target the last tabbable node in a group? |
| 549 |
var lastOfGroupIndex = state.tabbableGroups.findIndex(function (_ref5) { |
| 550 |
var lastTabbableNode = _ref5.lastTabbableNode; |
| 551 |
return target === lastTabbableNode; |
| 552 |
}); |
| 553 |
if (lastOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target, config.tabbableOptions) && !tabbable.isTabbable(target, config.tabbableOptions) && !containerGroup.nextTabbableNode(target))) { |
| 554 |
// an exception case where the target is the container itself, or |
| 555 |
// a non-tabbable node that was given focus (i.e. tabindex is negative |
| 556 |
// and user clicked on it or node was programmatically given focus) |
| 557 |
// and is not followed by any other tabbable node, in which |
| 558 |
// case, we should handle tab as if focus were on the container's |
| 559 |
// last tabbable node, and go to the first tabbable node of the FIRST group |
| 560 |
lastOfGroupIndex = containerIndex; |
| 561 |
} |
| 562 |
if (lastOfGroupIndex >= 0) { |
| 563 |
// YES: then tab should go to the first tabbable node in the next |
| 564 |
// group (and wrap around to the first tabbable node of the FIRST |
| 565 |
// group if it's the last tabbable node of the LAST group) |
| 566 |
var _destinationGroupIndex = lastOfGroupIndex === state.tabbableGroups.length - 1 ? 0 : lastOfGroupIndex + 1; |
| 567 |
var _destinationGroup = state.tabbableGroups[_destinationGroupIndex]; |
| 568 |
destinationNode = tabbable.getTabIndex(target) >= 0 ? _destinationGroup.firstTabbableNode : _destinationGroup.firstDomTabbableNode; |
| 569 |
} else if (!isTabEvent(event)) { |
| 570 |
// user must have customized the nav keys so we have to move focus manually _within_ |
| 571 |
// the active group: do this based on the order determined by tabbable() |
| 572 |
destinationNode = containerGroup.nextTabbableNode(target); |
| 573 |
} |
| 574 |
} |
| 575 |
} else { |
| 576 |
// no groups available |
| 577 |
// NOTE: the fallbackFocus option does not support returning false to opt-out |
| 578 |
destinationNode = getNodeForOption('fallbackFocus'); |
| 579 |
} |
| 580 |
return destinationNode; |
| 581 |
}; |
| 582 |
|
| 583 |
// This needs to be done on mousedown and touchstart instead of click |
| 584 |
// so that it precedes the focus event. |
| 585 |
var checkPointerDown = function checkPointerDown(e) { |
| 586 |
var target = getActualTarget(e); |
| 587 |
if (findContainerIndex(target, e) >= 0) { |
| 588 |
// allow the click since it ocurred inside the trap |
| 589 |
return; |
| 590 |
} |
| 591 |
if (valueOrHandler(config.clickOutsideDeactivates, e)) { |
| 592 |
// immediately deactivate the trap |
| 593 |
trap.deactivate({ |
| 594 |
// NOTE: by setting `returnFocus: false`, deactivate() will do nothing, |
| 595 |
// which will result in the outside click setting focus to the node |
| 596 |
// that was clicked (and if not focusable, to "nothing"); by setting |
| 597 |
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused |
| 598 |
// on activation (or the configured `setReturnFocus` node), whether the |
| 599 |
// outside click was on a focusable node or not |
| 600 |
returnFocus: config.returnFocusOnDeactivate |
| 601 |
}); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
// This is needed for mobile devices. |
| 606 |
// (If we'll only let `click` events through, |
| 607 |
// then on mobile they will be blocked anyways if `touchstart` is blocked.) |
| 608 |
if (valueOrHandler(config.allowOutsideClick, e)) { |
| 609 |
// allow the click outside the trap to take place |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
// otherwise, prevent the click |
| 614 |
e.preventDefault(); |
| 615 |
}; |
| 616 |
|
| 617 |
// In case focus escapes the trap for some strange reason, pull it back in. |
| 618 |
// NOTE: the focusIn event is NOT cancelable, so if focus escapes, it may cause unexpected |
| 619 |
// scrolling if the node that got focused was out of view; there's nothing we can do to |
| 620 |
// prevent that from happening by the time we discover that focus escaped |
| 621 |
var checkFocusIn = function checkFocusIn(event) { |
| 622 |
var target = getActualTarget(event); |
| 623 |
var targetContained = findContainerIndex(target, event) >= 0; |
| 624 |
|
| 625 |
// In Firefox when you Tab out of an iframe the Document is briefly focused. |
| 626 |
if (targetContained || target instanceof Document) { |
| 627 |
if (targetContained) { |
| 628 |
state.mostRecentlyFocusedNode = target; |
| 629 |
} |
| 630 |
} else { |
| 631 |
// escaped! pull it back in to where it just left |
| 632 |
event.stopImmediatePropagation(); |
| 633 |
|
| 634 |
// focus will escape if the MRU node had a positive tab index and user tried to nav forward; |
| 635 |
// it will also escape if the MRU node had a 0 tab index and user tried to nav backward |
| 636 |
// toward a node with a positive tab index |
| 637 |
var nextNode; // next node to focus, if we find one |
| 638 |
var navAcrossContainers = true; |
| 639 |
if (state.mostRecentlyFocusedNode) { |
| 640 |
if (tabbable.getTabIndex(state.mostRecentlyFocusedNode) > 0) { |
| 641 |
// MRU container index must be >=0 otherwise we wouldn't have it as an MRU node... |
| 642 |
var mruContainerIdx = findContainerIndex(state.mostRecentlyFocusedNode); |
| 643 |
// there MAY not be any tabbable nodes in the container if there are at least 2 containers |
| 644 |
// and the MRU node is focusable but not tabbable (focus-trap requires at least 1 container |
| 645 |
// with at least one tabbable node in order to function, so this could be the other container |
| 646 |
// with nothing tabbable in it) |
| 647 |
var tabbableNodes = state.containerGroups[mruContainerIdx].tabbableNodes; |
| 648 |
if (tabbableNodes.length > 0) { |
| 649 |
// MRU tab index MAY not be found if the MRU node is focusable but not tabbable |
| 650 |
var mruTabIdx = tabbableNodes.findIndex(function (node) { |
| 651 |
return node === state.mostRecentlyFocusedNode; |
| 652 |
}); |
| 653 |
if (mruTabIdx >= 0) { |
| 654 |
if (config.isKeyForward(state.recentNavEvent)) { |
| 655 |
if (mruTabIdx + 1 < tabbableNodes.length) { |
| 656 |
nextNode = tabbableNodes[mruTabIdx + 1]; |
| 657 |
navAcrossContainers = false; |
| 658 |
} |
| 659 |
// else, don't wrap within the container as focus should move to next/previous |
| 660 |
// container |
| 661 |
} else { |
| 662 |
if (mruTabIdx - 1 >= 0) { |
| 663 |
nextNode = tabbableNodes[mruTabIdx - 1]; |
| 664 |
navAcrossContainers = false; |
| 665 |
} |
| 666 |
// else, don't wrap within the container as focus should move to next/previous |
| 667 |
// container |
| 668 |
} |
| 669 |
// else, don't find in container order without considering direction too |
| 670 |
} |
| 671 |
} |
| 672 |
// else, no tabbable nodes in that container (which means we must have at least one other |
| 673 |
// container with at least one tabbable node in it, otherwise focus-trap would've thrown |
| 674 |
// an error the last time updateTabbableNodes() was run): find next node among all known |
| 675 |
// containers |
| 676 |
} else { |
| 677 |
// check to see if there's at least one tabbable node with a positive tab index inside |
| 678 |
// the trap because focus seems to escape when navigating backward from a tabbable node |
| 679 |
// with tabindex=0 when this is the case (instead of wrapping to the tabbable node with |
| 680 |
// the greatest positive tab index like it should) |
| 681 |
if (!state.containerGroups.some(function (g) { |
| 682 |
return g.tabbableNodes.some(function (n) { |
| 683 |
return tabbable.getTabIndex(n) > 0; |
| 684 |
}); |
| 685 |
})) { |
| 686 |
// no containers with tabbable nodes with positive tab indexes which means the focus |
| 687 |
// escaped for some other reason and we should just execute the fallback to the |
| 688 |
// MRU node or initial focus node, if any |
| 689 |
navAcrossContainers = false; |
| 690 |
} |
| 691 |
} |
| 692 |
} else { |
| 693 |
// no MRU node means we're likely in some initial condition when the trap has just |
| 694 |
// been activated and initial focus hasn't been given yet, in which case we should |
| 695 |
// fall through to trying to focus the initial focus node, which is what should |
| 696 |
// happen below at this point in the logic |
| 697 |
navAcrossContainers = false; |
| 698 |
} |
| 699 |
if (navAcrossContainers) { |
| 700 |
nextNode = findNextNavNode({ |
| 701 |
// move FROM the MRU node, not event-related node (which will be the node that is |
| 702 |
// outside the trap causing the focus escape we're trying to fix) |
| 703 |
target: state.mostRecentlyFocusedNode, |
| 704 |
isBackward: config.isKeyBackward(state.recentNavEvent) |
| 705 |
}); |
| 706 |
} |
| 707 |
if (nextNode) { |
| 708 |
_tryFocus(nextNode); |
| 709 |
} else { |
| 710 |
_tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode()); |
| 711 |
} |
| 712 |
} |
| 713 |
state.recentNavEvent = undefined; // clear |
| 714 |
}; |
| 715 |
|
| 716 |
// Hijack key nav events on the first and last focusable nodes of the trap, |
| 717 |
// in order to prevent focus from escaping. If it escapes for even a |
| 718 |
// moment it can end up scrolling the page and causing confusion so we |
| 719 |
// kind of need to capture the action at the keydown phase. |
| 720 |
var checkKeyNav = function checkKeyNav(event) { |
| 721 |
var isBackward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; |
| 722 |
state.recentNavEvent = event; |
| 723 |
var destinationNode = findNextNavNode({ |
| 724 |
event: event, |
| 725 |
isBackward: isBackward |
| 726 |
}); |
| 727 |
if (destinationNode) { |
| 728 |
if (isTabEvent(event)) { |
| 729 |
// since tab natively moves focus, we wouldn't have a destination node unless we |
| 730 |
// were on the edge of a container and had to move to the next/previous edge, in |
| 731 |
// which case we want to prevent default to keep the browser from moving focus |
| 732 |
// to where it normally would |
| 733 |
event.preventDefault(); |
| 734 |
} |
| 735 |
_tryFocus(destinationNode); |
| 736 |
} |
| 737 |
// else, let the browser take care of [shift+]tab and move the focus |
| 738 |
}; |
| 739 |
var checkTabKey = function checkTabKey(event) { |
| 740 |
if (config.isKeyForward(event) || config.isKeyBackward(event)) { |
| 741 |
checkKeyNav(event, config.isKeyBackward(event)); |
| 742 |
} |
| 743 |
}; |
| 744 |
|
| 745 |
// we use a different event phase for the Escape key to allow canceling the event and checking for this in escapeDeactivates |
| 746 |
var checkEscapeKey = function checkEscapeKey(event) { |
| 747 |
if (isEscapeEvent(event) && valueOrHandler(config.escapeDeactivates, event) !== false) { |
| 748 |
event.preventDefault(); |
| 749 |
trap.deactivate(); |
| 750 |
} |
| 751 |
}; |
| 752 |
var checkClick = function checkClick(e) { |
| 753 |
var target = getActualTarget(e); |
| 754 |
if (findContainerIndex(target, e) >= 0) { |
| 755 |
return; |
| 756 |
} |
| 757 |
if (valueOrHandler(config.clickOutsideDeactivates, e)) { |
| 758 |
return; |
| 759 |
} |
| 760 |
if (valueOrHandler(config.allowOutsideClick, e)) { |
| 761 |
return; |
| 762 |
} |
| 763 |
e.preventDefault(); |
| 764 |
e.stopImmediatePropagation(); |
| 765 |
}; |
| 766 |
|
| 767 |
// |
| 768 |
// EVENT LISTENERS |
| 769 |
// |
| 770 |
|
| 771 |
var addListeners = function addListeners() { |
| 772 |
if (!state.active) { |
| 773 |
return; |
| 774 |
} |
| 775 |
|
| 776 |
// There can be only one listening focus trap at a time |
| 777 |
activeFocusTraps.activateTrap(trapStack, trap); |
| 778 |
|
| 779 |
// Delay ensures that the focused element doesn't capture the event |
| 780 |
// that caused the focus trap activation. |
| 781 |
state.delayInitialFocusTimer = config.delayInitialFocus ? delay(function () { |
| 782 |
_tryFocus(getInitialFocusNode()); |
| 783 |
}) : _tryFocus(getInitialFocusNode()); |
| 784 |
doc.addEventListener('focusin', checkFocusIn, true); |
| 785 |
doc.addEventListener('mousedown', checkPointerDown, { |
| 786 |
capture: true, |
| 787 |
passive: false |
| 788 |
}); |
| 789 |
doc.addEventListener('touchstart', checkPointerDown, { |
| 790 |
capture: true, |
| 791 |
passive: false |
| 792 |
}); |
| 793 |
doc.addEventListener('click', checkClick, { |
| 794 |
capture: true, |
| 795 |
passive: false |
| 796 |
}); |
| 797 |
doc.addEventListener('keydown', checkTabKey, { |
| 798 |
capture: true, |
| 799 |
passive: false |
| 800 |
}); |
| 801 |
doc.addEventListener('keydown', checkEscapeKey); |
| 802 |
return trap; |
| 803 |
}; |
| 804 |
var removeListeners = function removeListeners() { |
| 805 |
if (!state.active) { |
| 806 |
return; |
| 807 |
} |
| 808 |
doc.removeEventListener('focusin', checkFocusIn, true); |
| 809 |
doc.removeEventListener('mousedown', checkPointerDown, true); |
| 810 |
doc.removeEventListener('touchstart', checkPointerDown, true); |
| 811 |
doc.removeEventListener('click', checkClick, true); |
| 812 |
doc.removeEventListener('keydown', checkTabKey, true); |
| 813 |
doc.removeEventListener('keydown', checkEscapeKey); |
| 814 |
return trap; |
| 815 |
}; |
| 816 |
|
| 817 |
// |
| 818 |
// MUTATION OBSERVER |
| 819 |
// |
| 820 |
|
| 821 |
var checkDomRemoval = function checkDomRemoval(mutations) { |
| 822 |
var isFocusedNodeRemoved = mutations.some(function (mutation) { |
| 823 |
var removedNodes = Array.from(mutation.removedNodes); |
| 824 |
return removedNodes.some(function (node) { |
| 825 |
return node === state.mostRecentlyFocusedNode; |
| 826 |
}); |
| 827 |
}); |
| 828 |
|
| 829 |
// If the currently focused is removed then browsers will move focus to the |
| 830 |
// <body> element. If this happens, try to move focus back into the trap. |
| 831 |
if (isFocusedNodeRemoved) { |
| 832 |
_tryFocus(getInitialFocusNode()); |
| 833 |
} |
| 834 |
}; |
| 835 |
|
| 836 |
// Use MutationObserver - if supported - to detect if focused node is removed |
| 837 |
// from the DOM. |
| 838 |
var mutationObserver = typeof window !== 'undefined' && 'MutationObserver' in window ? new MutationObserver(checkDomRemoval) : undefined; |
| 839 |
var updateObservedNodes = function updateObservedNodes() { |
| 840 |
if (!mutationObserver) { |
| 841 |
return; |
| 842 |
} |
| 843 |
mutationObserver.disconnect(); |
| 844 |
if (state.active && !state.paused) { |
| 845 |
state.containers.map(function (container) { |
| 846 |
mutationObserver.observe(container, { |
| 847 |
subtree: true, |
| 848 |
childList: true |
| 849 |
}); |
| 850 |
}); |
| 851 |
} |
| 852 |
}; |
| 853 |
|
| 854 |
// |
| 855 |
// TRAP DEFINITION |
| 856 |
// |
| 857 |
|
| 858 |
trap = { |
| 859 |
get active() { |
| 860 |
return state.active; |
| 861 |
}, |
| 862 |
get paused() { |
| 863 |
return state.paused; |
| 864 |
}, |
| 865 |
activate: function activate(activateOptions) { |
| 866 |
if (state.active) { |
| 867 |
return this; |
| 868 |
} |
| 869 |
var onActivate = getOption(activateOptions, 'onActivate'); |
| 870 |
var onPostActivate = getOption(activateOptions, 'onPostActivate'); |
| 871 |
var checkCanFocusTrap = getOption(activateOptions, 'checkCanFocusTrap'); |
| 872 |
if (!checkCanFocusTrap) { |
| 873 |
updateTabbableNodes(); |
| 874 |
} |
| 875 |
state.active = true; |
| 876 |
state.paused = false; |
| 877 |
state.nodeFocusedBeforeActivation = _getActiveElement(doc); |
| 878 |
onActivate === null || onActivate === void 0 || onActivate(); |
| 879 |
var finishActivation = function finishActivation() { |
| 880 |
if (checkCanFocusTrap) { |
| 881 |
updateTabbableNodes(); |
| 882 |
} |
| 883 |
addListeners(); |
| 884 |
updateObservedNodes(); |
| 885 |
onPostActivate === null || onPostActivate === void 0 || onPostActivate(); |
| 886 |
}; |
| 887 |
if (checkCanFocusTrap) { |
| 888 |
checkCanFocusTrap(state.containers.concat()).then(finishActivation, finishActivation); |
| 889 |
return this; |
| 890 |
} |
| 891 |
finishActivation(); |
| 892 |
return this; |
| 893 |
}, |
| 894 |
deactivate: function deactivate(deactivateOptions) { |
| 895 |
if (!state.active) { |
| 896 |
return this; |
| 897 |
} |
| 898 |
var options = _objectSpread2({ |
| 899 |
onDeactivate: config.onDeactivate, |
| 900 |
onPostDeactivate: config.onPostDeactivate, |
| 901 |
checkCanReturnFocus: config.checkCanReturnFocus |
| 902 |
}, deactivateOptions); |
| 903 |
clearTimeout(state.delayInitialFocusTimer); // noop if undefined |
| 904 |
state.delayInitialFocusTimer = undefined; |
| 905 |
removeListeners(); |
| 906 |
state.active = false; |
| 907 |
state.paused = false; |
| 908 |
updateObservedNodes(); |
| 909 |
activeFocusTraps.deactivateTrap(trapStack, trap); |
| 910 |
var onDeactivate = getOption(options, 'onDeactivate'); |
| 911 |
var onPostDeactivate = getOption(options, 'onPostDeactivate'); |
| 912 |
var checkCanReturnFocus = getOption(options, 'checkCanReturnFocus'); |
| 913 |
var returnFocus = getOption(options, 'returnFocus', 'returnFocusOnDeactivate'); |
| 914 |
onDeactivate === null || onDeactivate === void 0 || onDeactivate(); |
| 915 |
var finishDeactivation = function finishDeactivation() { |
| 916 |
delay(function () { |
| 917 |
if (returnFocus) { |
| 918 |
_tryFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation)); |
| 919 |
} |
| 920 |
onPostDeactivate === null || onPostDeactivate === void 0 || onPostDeactivate(); |
| 921 |
}); |
| 922 |
}; |
| 923 |
if (returnFocus && checkCanReturnFocus) { |
| 924 |
checkCanReturnFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation)).then(finishDeactivation, finishDeactivation); |
| 925 |
return this; |
| 926 |
} |
| 927 |
finishDeactivation(); |
| 928 |
return this; |
| 929 |
}, |
| 930 |
pause: function pause(pauseOptions) { |
| 931 |
if (!state.active) { |
| 932 |
return this; |
| 933 |
} |
| 934 |
state.manuallyPaused = true; |
| 935 |
return this._setPausedState(true, pauseOptions); |
| 936 |
}, |
| 937 |
unpause: function unpause(unpauseOptions) { |
| 938 |
if (!state.active) { |
| 939 |
return this; |
| 940 |
} |
| 941 |
state.manuallyPaused = false; |
| 942 |
if (trapStack[trapStack.length - 1] !== this) { |
| 943 |
return this; |
| 944 |
} |
| 945 |
return this._setPausedState(false, unpauseOptions); |
| 946 |
}, |
| 947 |
updateContainerElements: function updateContainerElements(containerElements) { |
| 948 |
var elementsAsArray = [].concat(containerElements).filter(Boolean); |
| 949 |
state.containers = elementsAsArray.map(function (element) { |
| 950 |
return typeof element === 'string' ? doc.querySelector(element) : element; |
| 951 |
}); |
| 952 |
if (state.active) { |
| 953 |
updateTabbableNodes(); |
| 954 |
} |
| 955 |
updateObservedNodes(); |
| 956 |
return this; |
| 957 |
} |
| 958 |
}; |
| 959 |
Object.defineProperties(trap, { |
| 960 |
_isManuallyPaused: { |
| 961 |
value: function value() { |
| 962 |
return state.manuallyPaused; |
| 963 |
} |
| 964 |
}, |
| 965 |
_setPausedState: { |
| 966 |
value: function value(paused, options) { |
| 967 |
if (state.paused === paused) { |
| 968 |
return this; |
| 969 |
} |
| 970 |
state.paused = paused; |
| 971 |
if (paused) { |
| 972 |
var onPause = getOption(options, 'onPause'); |
| 973 |
var onPostPause = getOption(options, 'onPostPause'); |
| 974 |
onPause === null || onPause === void 0 || onPause(); |
| 975 |
removeListeners(); |
| 976 |
updateObservedNodes(); |
| 977 |
onPostPause === null || onPostPause === void 0 || onPostPause(); |
| 978 |
} else { |
| 979 |
var onUnpause = getOption(options, 'onUnpause'); |
| 980 |
var onPostUnpause = getOption(options, 'onPostUnpause'); |
| 981 |
onUnpause === null || onUnpause === void 0 || onUnpause(); |
| 982 |
updateTabbableNodes(); |
| 983 |
addListeners(); |
| 984 |
updateObservedNodes(); |
| 985 |
onPostUnpause === null || onPostUnpause === void 0 || onPostUnpause(); |
| 986 |
} |
| 987 |
return this; |
| 988 |
} |
| 989 |
} |
| 990 |
}); |
| 991 |
|
| 992 |
// initialize container elements |
| 993 |
trap.updateContainerElements(elements); |
| 994 |
return trap; |
| 995 |
}; |
| 996 |
|
| 997 |
exports.createFocusTrap = createFocusTrap; |
| 998 |
|
| 999 |
})); |
| 1000 |
//# sourceMappingURL=focus-trap.umd.js.map |