focus-trap.umd.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*!
  2. * focus-trap 7.0.0
  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. function ownKeys(object, enumerableOnly) {
  16. var keys = Object.keys(object);
  17. if (Object.getOwnPropertySymbols) {
  18. var symbols = Object.getOwnPropertySymbols(object);
  19. enumerableOnly && (symbols = symbols.filter(function (sym) {
  20. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  21. })), keys.push.apply(keys, symbols);
  22. }
  23. return keys;
  24. }
  25. function _objectSpread2(target) {
  26. for (var i = 1; i < arguments.length; i++) {
  27. var source = null != arguments[i] ? arguments[i] : {};
  28. i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
  29. _defineProperty(target, key, source[key]);
  30. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
  31. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  32. });
  33. }
  34. return target;
  35. }
  36. function _defineProperty(obj, key, value) {
  37. if (key in obj) {
  38. Object.defineProperty(obj, key, {
  39. value: value,
  40. enumerable: true,
  41. configurable: true,
  42. writable: true
  43. });
  44. } else {
  45. obj[key] = value;
  46. }
  47. return obj;
  48. }
  49. var activeFocusTraps = function () {
  50. var trapQueue = [];
  51. return {
  52. activateTrap: function activateTrap(trap) {
  53. if (trapQueue.length > 0) {
  54. var activeTrap = trapQueue[trapQueue.length - 1];
  55. if (activeTrap !== trap) {
  56. activeTrap.pause();
  57. }
  58. }
  59. var trapIndex = trapQueue.indexOf(trap);
  60. if (trapIndex === -1) {
  61. trapQueue.push(trap);
  62. } else {
  63. // move this existing trap to the front of the queue
  64. trapQueue.splice(trapIndex, 1);
  65. trapQueue.push(trap);
  66. }
  67. },
  68. deactivateTrap: function deactivateTrap(trap) {
  69. var trapIndex = trapQueue.indexOf(trap);
  70. if (trapIndex !== -1) {
  71. trapQueue.splice(trapIndex, 1);
  72. }
  73. if (trapQueue.length > 0) {
  74. trapQueue[trapQueue.length - 1].unpause();
  75. }
  76. }
  77. };
  78. }();
  79. var isSelectableInput = function isSelectableInput(node) {
  80. return node.tagName && node.tagName.toLowerCase() === 'input' && typeof node.select === 'function';
  81. };
  82. var isEscapeEvent = function isEscapeEvent(e) {
  83. return e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27;
  84. };
  85. var isTabEvent = function isTabEvent(e) {
  86. return e.key === 'Tab' || e.keyCode === 9;
  87. };
  88. var delay = function delay(fn) {
  89. return setTimeout(fn, 0);
  90. }; // Array.find/findIndex() are not supported on IE; this replicates enough
  91. // of Array.findIndex() for our needs
  92. var findIndex = function findIndex(arr, fn) {
  93. var idx = -1;
  94. arr.every(function (value, i) {
  95. if (fn(value)) {
  96. idx = i;
  97. return false; // break
  98. }
  99. return true; // next
  100. });
  101. return idx;
  102. };
  103. /**
  104. * Get an option's value when it could be a plain value, or a handler that provides
  105. * the value.
  106. * @param {*} value Option's value to check.
  107. * @param {...*} [params] Any parameters to pass to the handler, if `value` is a function.
  108. * @returns {*} The `value`, or the handler's returned value.
  109. */
  110. var valueOrHandler = function valueOrHandler(value) {
  111. for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  112. params[_key - 1] = arguments[_key];
  113. }
  114. return typeof value === 'function' ? value.apply(void 0, params) : value;
  115. };
  116. var getActualTarget = function getActualTarget(event) {
  117. // NOTE: If the trap is _inside_ a shadow DOM, event.target will always be the
  118. // shadow host. However, event.target.composedPath() will be an array of
  119. // nodes "clicked" from inner-most (the actual element inside the shadow) to
  120. // outer-most (the host HTML document). If we have access to composedPath(),
  121. // then use its first element; otherwise, fall back to event.target (and
  122. // this only works for an _open_ shadow DOM; otherwise,
  123. // composedPath()[0] === event.target always).
  124. return event.target.shadowRoot && typeof event.composedPath === 'function' ? event.composedPath()[0] : event.target;
  125. };
  126. var createFocusTrap = function createFocusTrap(elements, userOptions) {
  127. // SSR: a live trap shouldn't be created in this type of environment so this
  128. // should be safe code to execute if the `document` option isn't specified
  129. var doc = (userOptions === null || userOptions === void 0 ? void 0 : userOptions.document) || document;
  130. var config = _objectSpread2({
  131. returnFocusOnDeactivate: true,
  132. escapeDeactivates: true,
  133. delayInitialFocus: true
  134. }, userOptions);
  135. var state = {
  136. // containers given to createFocusTrap()
  137. // @type {Array<HTMLElement>}
  138. containers: [],
  139. // list of objects identifying tabbable nodes in `containers` in the trap
  140. // NOTE: it's possible that a group has no tabbable nodes if nodes get removed while the trap
  141. // is active, but the trap should never get to a state where there isn't at least one group
  142. // with at least one tabbable node in it (that would lead to an error condition that would
  143. // result in an error being thrown)
  144. // @type {Array<{
  145. // container: HTMLElement,
  146. // tabbableNodes: Array<HTMLElement>, // empty if none
  147. // focusableNodes: Array<HTMLElement>, // empty if none
  148. // firstTabbableNode: HTMLElement|null,
  149. // lastTabbableNode: HTMLElement|null,
  150. // nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined
  151. // }>}
  152. containerGroups: [],
  153. // same order/length as `containers` list
  154. // references to objects in `containerGroups`, but only those that actually have
  155. // tabbable nodes in them
  156. // NOTE: same order as `containers` and `containerGroups`, but __not necessarily__
  157. // the same length
  158. tabbableGroups: [],
  159. nodeFocusedBeforeActivation: null,
  160. mostRecentlyFocusedNode: null,
  161. active: false,
  162. paused: false,
  163. // timer ID for when delayInitialFocus is true and initial focus in this trap
  164. // has been delayed during activation
  165. delayInitialFocusTimer: undefined
  166. };
  167. 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
  168. /**
  169. * Gets a configuration option value.
  170. * @param {Object|undefined} configOverrideOptions If true, and option is defined in this set,
  171. * value will be taken from this object. Otherwise, value will be taken from base configuration.
  172. * @param {string} optionName Name of the option whose value is sought.
  173. * @param {string|undefined} [configOptionName] Name of option to use __instead of__ `optionName`
  174. * IIF `configOverrideOptions` is not defined. Otherwise, `optionName` is used.
  175. */
  176. var getOption = function getOption(configOverrideOptions, optionName, configOptionName) {
  177. return configOverrideOptions && configOverrideOptions[optionName] !== undefined ? configOverrideOptions[optionName] : config[configOptionName || optionName];
  178. };
  179. /**
  180. * Finds the index of the container that contains the element.
  181. * @param {HTMLElement} element
  182. * @returns {number} Index of the container in either `state.containers` or
  183. * `state.containerGroups` (the order/length of these lists are the same); -1
  184. * if the element isn't found.
  185. */
  186. var findContainerIndex = function findContainerIndex(element) {
  187. // NOTE: search `containerGroups` because it's possible a group contains no tabbable
  188. // nodes, but still contains focusable nodes (e.g. if they all have `tabindex=-1`)
  189. // and we still need to find the element in there
  190. return state.containerGroups.findIndex(function (_ref) {
  191. var container = _ref.container,
  192. tabbableNodes = _ref.tabbableNodes;
  193. return container.contains(element) || // fall back to explicit tabbable search which will take into consideration any
  194. // web components if the `tabbableOptions.getShadowRoot` option was used for
  195. // the trap, enabling shadow DOM support in tabbable (`Node.contains()` doesn't
  196. // look inside web components even if open)
  197. tabbableNodes.find(function (node) {
  198. return node === element;
  199. });
  200. });
  201. };
  202. /**
  203. * Gets the node for the given option, which is expected to be an option that
  204. * can be either a DOM node, a string that is a selector to get a node, `false`
  205. * (if a node is explicitly NOT given), or a function that returns any of these
  206. * values.
  207. * @param {string} optionName
  208. * @returns {undefined | false | HTMLElement | SVGElement} Returns
  209. * `undefined` if the option is not specified; `false` if the option
  210. * resolved to `false` (node explicitly not given); otherwise, the resolved
  211. * DOM node.
  212. * @throws {Error} If the option is set, not `false`, and is not, or does not
  213. * resolve to a node.
  214. */
  215. var getNodeForOption = function getNodeForOption(optionName) {
  216. var optionValue = config[optionName];
  217. if (typeof optionValue === 'function') {
  218. for (var _len2 = arguments.length, params = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  219. params[_key2 - 1] = arguments[_key2];
  220. }
  221. optionValue = optionValue.apply(void 0, params);
  222. }
  223. if (optionValue === true) {
  224. optionValue = undefined; // use default value
  225. }
  226. if (!optionValue) {
  227. if (optionValue === undefined || optionValue === false) {
  228. return optionValue;
  229. } // else, empty string (invalid), null (invalid), 0 (invalid)
  230. throw new Error("`".concat(optionName, "` was specified but was not a node, or did not return a node"));
  231. }
  232. var node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point
  233. if (typeof optionValue === 'string') {
  234. node = doc.querySelector(optionValue); // resolve to node, or null if fails
  235. if (!node) {
  236. throw new Error("`".concat(optionName, "` as selector refers to no known node"));
  237. }
  238. }
  239. return node;
  240. };
  241. var getInitialFocusNode = function getInitialFocusNode() {
  242. var node = getNodeForOption('initialFocus'); // false explicitly indicates we want no initialFocus at all
  243. if (node === false) {
  244. return false;
  245. }
  246. if (node === undefined) {
  247. // option not specified: use fallback options
  248. if (findContainerIndex(doc.activeElement) >= 0) {
  249. node = doc.activeElement;
  250. } else {
  251. var firstTabbableGroup = state.tabbableGroups[0];
  252. var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode; // NOTE: `fallbackFocus` option function cannot return `false` (not supported)
  253. node = firstTabbableNode || getNodeForOption('fallbackFocus');
  254. }
  255. }
  256. if (!node) {
  257. throw new Error('Your focus-trap needs to have at least one focusable element');
  258. }
  259. return node;
  260. };
  261. var updateTabbableNodes = function updateTabbableNodes() {
  262. state.containerGroups = state.containers.map(function (container) {
  263. var tabbableNodes = tabbable.tabbable(container, config.tabbableOptions); // NOTE: if we have tabbable nodes, we must have focusable nodes; focusable nodes
  264. // are a superset of tabbable nodes
  265. var focusableNodes = tabbable.focusable(container, config.tabbableOptions);
  266. return {
  267. container: container,
  268. tabbableNodes: tabbableNodes,
  269. focusableNodes: focusableNodes,
  270. firstTabbableNode: tabbableNodes.length > 0 ? tabbableNodes[0] : null,
  271. lastTabbableNode: tabbableNodes.length > 0 ? tabbableNodes[tabbableNodes.length - 1] : null,
  272. /**
  273. * Finds the __tabbable__ node that follows the given node in the specified direction,
  274. * in this container, if any.
  275. * @param {HTMLElement} node
  276. * @param {boolean} [forward] True if going in forward tab order; false if going
  277. * in reverse.
  278. * @returns {HTMLElement|undefined} The next tabbable node, if any.
  279. */
  280. nextTabbableNode: function nextTabbableNode(node) {
  281. var forward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  282. // NOTE: If tabindex is positive (in order to manipulate the tab order separate
  283. // from the DOM order), this __will not work__ because the list of focusableNodes,
  284. // while it contains tabbable nodes, does not sort its nodes in any order other
  285. // than DOM order, because it can't: Where would you place focusable (but not
  286. // tabbable) nodes in that order? They have no order, because they aren't tabbale...
  287. // Support for positive tabindex is already broken and hard to manage (possibly
  288. // not supportable, TBD), so this isn't going to make things worse than they
  289. // already are, and at least makes things better for the majority of cases where
  290. // tabindex is either 0/unset or negative.
  291. // FYI, positive tabindex issue: https://github.com/focus-trap/focus-trap/issues/375
  292. var nodeIdx = focusableNodes.findIndex(function (n) {
  293. return n === node;
  294. });
  295. if (nodeIdx < 0) {
  296. return undefined;
  297. }
  298. if (forward) {
  299. return focusableNodes.slice(nodeIdx + 1).find(function (n) {
  300. return tabbable.isTabbable(n, config.tabbableOptions);
  301. });
  302. }
  303. return focusableNodes.slice(0, nodeIdx).reverse().find(function (n) {
  304. return tabbable.isTabbable(n, config.tabbableOptions);
  305. });
  306. }
  307. };
  308. });
  309. state.tabbableGroups = state.containerGroups.filter(function (group) {
  310. return group.tabbableNodes.length > 0;
  311. }); // throw if no groups have tabbable nodes and we don't have a fallback focus node either
  312. if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus') // returning false not supported for this option
  313. ) {
  314. throw new Error('Your focus-trap must have at least one container with at least one tabbable node in it at all times');
  315. }
  316. };
  317. var tryFocus = function tryFocus(node) {
  318. if (node === false) {
  319. return;
  320. }
  321. if (node === doc.activeElement) {
  322. return;
  323. }
  324. if (!node || !node.focus) {
  325. tryFocus(getInitialFocusNode());
  326. return;
  327. }
  328. node.focus({
  329. preventScroll: !!config.preventScroll
  330. });
  331. state.mostRecentlyFocusedNode = node;
  332. if (isSelectableInput(node)) {
  333. node.select();
  334. }
  335. };
  336. var getReturnFocusNode = function getReturnFocusNode(previousActiveElement) {
  337. var node = getNodeForOption('setReturnFocus', previousActiveElement);
  338. return node ? node : node === false ? false : previousActiveElement;
  339. }; // This needs to be done on mousedown and touchstart instead of click
  340. // so that it precedes the focus event.
  341. var checkPointerDown = function checkPointerDown(e) {
  342. var target = getActualTarget(e);
  343. if (findContainerIndex(target) >= 0) {
  344. // allow the click since it ocurred inside the trap
  345. return;
  346. }
  347. if (valueOrHandler(config.clickOutsideDeactivates, e)) {
  348. // immediately deactivate the trap
  349. trap.deactivate({
  350. // if, on deactivation, we should return focus to the node originally-focused
  351. // when the trap was activated (or the configured `setReturnFocus` node),
  352. // then assume it's also OK to return focus to the outside node that was
  353. // just clicked, causing deactivation, as long as that node is focusable;
  354. // if it isn't focusable, then return focus to the original node focused
  355. // on activation (or the configured `setReturnFocus` node)
  356. // NOTE: by setting `returnFocus: false`, deactivate() will do nothing,
  357. // which will result in the outside click setting focus to the node
  358. // that was clicked, whether it's focusable or not; by setting
  359. // `returnFocus: true`, we'll attempt to re-focus the node originally-focused
  360. // on activation (or the configured `setReturnFocus` node)
  361. returnFocus: config.returnFocusOnDeactivate && !tabbable.isFocusable(target, config.tabbableOptions)
  362. });
  363. return;
  364. } // This is needed for mobile devices.
  365. // (If we'll only let `click` events through,
  366. // then on mobile they will be blocked anyways if `touchstart` is blocked.)
  367. if (valueOrHandler(config.allowOutsideClick, e)) {
  368. // allow the click outside the trap to take place
  369. return;
  370. } // otherwise, prevent the click
  371. e.preventDefault();
  372. }; // In case focus escapes the trap for some strange reason, pull it back in.
  373. var checkFocusIn = function checkFocusIn(e) {
  374. var target = getActualTarget(e);
  375. var targetContained = findContainerIndex(target) >= 0; // In Firefox when you Tab out of an iframe the Document is briefly focused.
  376. if (targetContained || target instanceof Document) {
  377. if (targetContained) {
  378. state.mostRecentlyFocusedNode = target;
  379. }
  380. } else {
  381. // escaped! pull it back in to where it just left
  382. e.stopImmediatePropagation();
  383. tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());
  384. }
  385. }; // Hijack Tab events on the first and last focusable nodes of the trap,
  386. // in order to prevent focus from escaping. If it escapes for even a
  387. // moment it can end up scrolling the page and causing confusion so we
  388. // kind of need to capture the action at the keydown phase.
  389. var checkTab = function checkTab(e) {
  390. var target = getActualTarget(e);
  391. updateTabbableNodes();
  392. var destinationNode = null;
  393. if (state.tabbableGroups.length > 0) {
  394. // make sure the target is actually contained in a group
  395. // NOTE: the target may also be the container itself if it's focusable
  396. // with tabIndex='-1' and was given initial focus
  397. var containerIndex = findContainerIndex(target);
  398. var containerGroup = containerIndex >= 0 ? state.containerGroups[containerIndex] : undefined;
  399. if (containerIndex < 0) {
  400. // target not found in any group: quite possible focus has escaped the trap,
  401. // so bring it back in to...
  402. if (e.shiftKey) {
  403. // ...the last node in the last group
  404. destinationNode = state.tabbableGroups[state.tabbableGroups.length - 1].lastTabbableNode;
  405. } else {
  406. // ...the first node in the first group
  407. destinationNode = state.tabbableGroups[0].firstTabbableNode;
  408. }
  409. } else if (e.shiftKey) {
  410. // REVERSE
  411. // is the target the first tabbable node in a group?
  412. var startOfGroupIndex = findIndex(state.tabbableGroups, function (_ref2) {
  413. var firstTabbableNode = _ref2.firstTabbableNode;
  414. return target === firstTabbableNode;
  415. });
  416. if (startOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target, config.tabbableOptions) && !tabbable.isTabbable(target, config.tabbableOptions) && !containerGroup.nextTabbableNode(target, false))) {
  417. // an exception case where the target is either the container itself, or
  418. // a non-tabbable node that was given focus (i.e. tabindex is negative
  419. // and user clicked on it or node was programmatically given focus)
  420. // and is not followed by any other tabbable node, in which
  421. // case, we should handle shift+tab as if focus were on the container's
  422. // first tabbable node, and go to the last tabbable node of the LAST group
  423. startOfGroupIndex = containerIndex;
  424. }
  425. if (startOfGroupIndex >= 0) {
  426. // YES: then shift+tab should go to the last tabbable node in the
  427. // previous group (and wrap around to the last tabbable node of
  428. // the LAST group if it's the first tabbable node of the FIRST group)
  429. var destinationGroupIndex = startOfGroupIndex === 0 ? state.tabbableGroups.length - 1 : startOfGroupIndex - 1;
  430. var destinationGroup = state.tabbableGroups[destinationGroupIndex];
  431. destinationNode = destinationGroup.lastTabbableNode;
  432. }
  433. } else {
  434. // FORWARD
  435. // is the target the last tabbable node in a group?
  436. var lastOfGroupIndex = findIndex(state.tabbableGroups, function (_ref3) {
  437. var lastTabbableNode = _ref3.lastTabbableNode;
  438. return target === lastTabbableNode;
  439. });
  440. if (lastOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target, config.tabbableOptions) && !tabbable.isTabbable(target, config.tabbableOptions) && !containerGroup.nextTabbableNode(target))) {
  441. // an exception case where the target is the container itself, or
  442. // a non-tabbable node that was given focus (i.e. tabindex is negative
  443. // and user clicked on it or node was programmatically given focus)
  444. // and is not followed by any other tabbable node, in which
  445. // case, we should handle tab as if focus were on the container's
  446. // last tabbable node, and go to the first tabbable node of the FIRST group
  447. lastOfGroupIndex = containerIndex;
  448. }
  449. if (lastOfGroupIndex >= 0) {
  450. // YES: then tab should go to the first tabbable node in the next
  451. // group (and wrap around to the first tabbable node of the FIRST
  452. // group if it's the last tabbable node of the LAST group)
  453. var _destinationGroupIndex = lastOfGroupIndex === state.tabbableGroups.length - 1 ? 0 : lastOfGroupIndex + 1;
  454. var _destinationGroup = state.tabbableGroups[_destinationGroupIndex];
  455. destinationNode = _destinationGroup.firstTabbableNode;
  456. }
  457. }
  458. } else {
  459. // NOTE: the fallbackFocus option does not support returning false to opt-out
  460. destinationNode = getNodeForOption('fallbackFocus');
  461. }
  462. if (destinationNode) {
  463. e.preventDefault();
  464. tryFocus(destinationNode);
  465. } // else, let the browser take care of [shift+]tab and move the focus
  466. };
  467. var checkKey = function checkKey(e) {
  468. if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates, e) !== false) {
  469. e.preventDefault();
  470. trap.deactivate();
  471. return;
  472. }
  473. if (isTabEvent(e)) {
  474. checkTab(e);
  475. return;
  476. }
  477. };
  478. var checkClick = function checkClick(e) {
  479. var target = getActualTarget(e);
  480. if (findContainerIndex(target) >= 0) {
  481. return;
  482. }
  483. if (valueOrHandler(config.clickOutsideDeactivates, e)) {
  484. return;
  485. }
  486. if (valueOrHandler(config.allowOutsideClick, e)) {
  487. return;
  488. }
  489. e.preventDefault();
  490. e.stopImmediatePropagation();
  491. }; //
  492. // EVENT LISTENERS
  493. //
  494. var addListeners = function addListeners() {
  495. if (!state.active) {
  496. return;
  497. } // There can be only one listening focus trap at a time
  498. activeFocusTraps.activateTrap(trap); // Delay ensures that the focused element doesn't capture the event
  499. // that caused the focus trap activation.
  500. state.delayInitialFocusTimer = config.delayInitialFocus ? delay(function () {
  501. tryFocus(getInitialFocusNode());
  502. }) : tryFocus(getInitialFocusNode());
  503. doc.addEventListener('focusin', checkFocusIn, true);
  504. doc.addEventListener('mousedown', checkPointerDown, {
  505. capture: true,
  506. passive: false
  507. });
  508. doc.addEventListener('touchstart', checkPointerDown, {
  509. capture: true,
  510. passive: false
  511. });
  512. doc.addEventListener('click', checkClick, {
  513. capture: true,
  514. passive: false
  515. });
  516. doc.addEventListener('keydown', checkKey, {
  517. capture: true,
  518. passive: false
  519. });
  520. return trap;
  521. };
  522. var removeListeners = function removeListeners() {
  523. if (!state.active) {
  524. return;
  525. }
  526. doc.removeEventListener('focusin', checkFocusIn, true);
  527. doc.removeEventListener('mousedown', checkPointerDown, true);
  528. doc.removeEventListener('touchstart', checkPointerDown, true);
  529. doc.removeEventListener('click', checkClick, true);
  530. doc.removeEventListener('keydown', checkKey, true);
  531. return trap;
  532. }; //
  533. // TRAP DEFINITION
  534. //
  535. trap = {
  536. get active() {
  537. return state.active;
  538. },
  539. get paused() {
  540. return state.paused;
  541. },
  542. activate: function activate(activateOptions) {
  543. if (state.active) {
  544. return this;
  545. }
  546. var onActivate = getOption(activateOptions, 'onActivate');
  547. var onPostActivate = getOption(activateOptions, 'onPostActivate');
  548. var checkCanFocusTrap = getOption(activateOptions, 'checkCanFocusTrap');
  549. if (!checkCanFocusTrap) {
  550. updateTabbableNodes();
  551. }
  552. state.active = true;
  553. state.paused = false;
  554. state.nodeFocusedBeforeActivation = doc.activeElement;
  555. if (onActivate) {
  556. onActivate();
  557. }
  558. var finishActivation = function finishActivation() {
  559. if (checkCanFocusTrap) {
  560. updateTabbableNodes();
  561. }
  562. addListeners();
  563. if (onPostActivate) {
  564. onPostActivate();
  565. }
  566. };
  567. if (checkCanFocusTrap) {
  568. checkCanFocusTrap(state.containers.concat()).then(finishActivation, finishActivation);
  569. return this;
  570. }
  571. finishActivation();
  572. return this;
  573. },
  574. deactivate: function deactivate(deactivateOptions) {
  575. if (!state.active) {
  576. return this;
  577. }
  578. var options = _objectSpread2({
  579. onDeactivate: config.onDeactivate,
  580. onPostDeactivate: config.onPostDeactivate,
  581. checkCanReturnFocus: config.checkCanReturnFocus
  582. }, deactivateOptions);
  583. clearTimeout(state.delayInitialFocusTimer); // noop if undefined
  584. state.delayInitialFocusTimer = undefined;
  585. removeListeners();
  586. state.active = false;
  587. state.paused = false;
  588. activeFocusTraps.deactivateTrap(trap);
  589. var onDeactivate = getOption(options, 'onDeactivate');
  590. var onPostDeactivate = getOption(options, 'onPostDeactivate');
  591. var checkCanReturnFocus = getOption(options, 'checkCanReturnFocus');
  592. var returnFocus = getOption(options, 'returnFocus', 'returnFocusOnDeactivate');
  593. if (onDeactivate) {
  594. onDeactivate();
  595. }
  596. var finishDeactivation = function finishDeactivation() {
  597. delay(function () {
  598. if (returnFocus) {
  599. tryFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation));
  600. }
  601. if (onPostDeactivate) {
  602. onPostDeactivate();
  603. }
  604. });
  605. };
  606. if (returnFocus && checkCanReturnFocus) {
  607. checkCanReturnFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation)).then(finishDeactivation, finishDeactivation);
  608. return this;
  609. }
  610. finishDeactivation();
  611. return this;
  612. },
  613. pause: function pause() {
  614. if (state.paused || !state.active) {
  615. return this;
  616. }
  617. state.paused = true;
  618. removeListeners();
  619. return this;
  620. },
  621. unpause: function unpause() {
  622. if (!state.paused || !state.active) {
  623. return this;
  624. }
  625. state.paused = false;
  626. updateTabbableNodes();
  627. addListeners();
  628. return this;
  629. },
  630. updateContainerElements: function updateContainerElements(containerElements) {
  631. var elementsAsArray = [].concat(containerElements).filter(Boolean);
  632. state.containers = elementsAsArray.map(function (element) {
  633. return typeof element === 'string' ? doc.querySelector(element) : element;
  634. });
  635. if (state.active) {
  636. updateTabbableNodes();
  637. }
  638. return this;
  639. }
  640. }; // initialize container elements
  641. trap.updateContainerElements(elements);
  642. return trap;
  643. };
  644. exports.createFocusTrap = createFocusTrap;
  645. Object.defineProperty(exports, '__esModule', { value: true });
  646. }));
  647. //# sourceMappingURL=focus-trap.umd.js.map