vitepress___@vueuse_integrations_useFocusTrap.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. import { tryOnScopeDispose, unrefElement } from './chunk-GIB4L7EF.js';
  2. import './chunk-VBCBHYGZ.js';
  3. import { ref, watch } from './chunk-EW3MCGF2.js';
  4. import './chunk-UXIASGQL.js';
  5. // node_modules/.pnpm/tabbable@6.2.0/node_modules/tabbable/dist/index.esm.js
  6. var candidateSelectors = [
  7. 'input:not([inert])',
  8. 'select:not([inert])',
  9. 'textarea:not([inert])',
  10. 'a[href]:not([inert])',
  11. 'button:not([inert])',
  12. '[tabindex]:not(slot):not([inert])',
  13. 'audio[controls]:not([inert])',
  14. 'video[controls]:not([inert])',
  15. '[contenteditable]:not([contenteditable="false"]):not([inert])',
  16. 'details>summary:first-of-type:not([inert])',
  17. 'details:not([inert])'
  18. ];
  19. var candidateSelector = candidateSelectors.join(',');
  20. var NoElement = typeof Element === 'undefined';
  21. var matches = NoElement
  22. ? function () {}
  23. : Element.prototype.matches ||
  24. Element.prototype.msMatchesSelector ||
  25. Element.prototype.webkitMatchesSelector;
  26. var getRootNode =
  27. !NoElement && Element.prototype.getRootNode
  28. ? function (element) {
  29. var _element$getRootNode;
  30. return element === null || element === void 0
  31. ? void 0
  32. : (_element$getRootNode = element.getRootNode) === null ||
  33. _element$getRootNode === void 0
  34. ? void 0
  35. : _element$getRootNode.call(element);
  36. }
  37. : function (element) {
  38. return element === null || element === void 0 ? void 0 : element.ownerDocument;
  39. };
  40. var isInert = function isInert2(node, lookUp) {
  41. var _node$getAttribute;
  42. if (lookUp === void 0) {
  43. lookUp = true;
  44. }
  45. var inertAtt =
  46. node === null || node === void 0
  47. ? void 0
  48. : (_node$getAttribute = node.getAttribute) === null || _node$getAttribute === void 0
  49. ? void 0
  50. : _node$getAttribute.call(node, 'inert');
  51. var inert = inertAtt === '' || inertAtt === 'true';
  52. var result = inert || (lookUp && node && isInert2(node.parentNode));
  53. return result;
  54. };
  55. var isContentEditable = function isContentEditable2(node) {
  56. var _node$getAttribute2;
  57. var attValue =
  58. node === null || node === void 0
  59. ? void 0
  60. : (_node$getAttribute2 = node.getAttribute) === null || _node$getAttribute2 === void 0
  61. ? void 0
  62. : _node$getAttribute2.call(node, 'contenteditable');
  63. return attValue === '' || attValue === 'true';
  64. };
  65. var getCandidates = function getCandidates2(el, includeContainer, filter) {
  66. if (isInert(el)) {
  67. return [];
  68. }
  69. var candidates = Array.prototype.slice.apply(el.querySelectorAll(candidateSelector));
  70. if (includeContainer && matches.call(el, candidateSelector)) {
  71. candidates.unshift(el);
  72. }
  73. candidates = candidates.filter(filter);
  74. return candidates;
  75. };
  76. var getCandidatesIteratively = function getCandidatesIteratively2(
  77. elements,
  78. includeContainer,
  79. options
  80. ) {
  81. var candidates = [];
  82. var elementsToCheck = Array.from(elements);
  83. while (elementsToCheck.length) {
  84. var element = elementsToCheck.shift();
  85. if (isInert(element, false)) {
  86. continue;
  87. }
  88. if (element.tagName === 'SLOT') {
  89. var assigned = element.assignedElements();
  90. var content = assigned.length ? assigned : element.children;
  91. var nestedCandidates = getCandidatesIteratively2(content, true, options);
  92. if (options.flatten) {
  93. candidates.push.apply(candidates, nestedCandidates);
  94. } else {
  95. candidates.push({
  96. scopeParent: element,
  97. candidates: nestedCandidates
  98. });
  99. }
  100. } else {
  101. var validCandidate = matches.call(element, candidateSelector);
  102. if (
  103. validCandidate &&
  104. options.filter(element) &&
  105. (includeContainer || !elements.includes(element))
  106. ) {
  107. candidates.push(element);
  108. }
  109. var shadowRoot =
  110. element.shadowRoot || // check for an undisclosed shadow
  111. (typeof options.getShadowRoot === 'function' && options.getShadowRoot(element));
  112. var validShadowRoot =
  113. !isInert(shadowRoot, false) &&
  114. (!options.shadowRootFilter || options.shadowRootFilter(element));
  115. if (shadowRoot && validShadowRoot) {
  116. var _nestedCandidates = getCandidatesIteratively2(
  117. shadowRoot === true ? element.children : shadowRoot.children,
  118. true,
  119. options
  120. );
  121. if (options.flatten) {
  122. candidates.push.apply(candidates, _nestedCandidates);
  123. } else {
  124. candidates.push({
  125. scopeParent: element,
  126. candidates: _nestedCandidates
  127. });
  128. }
  129. } else {
  130. elementsToCheck.unshift.apply(elementsToCheck, element.children);
  131. }
  132. }
  133. }
  134. return candidates;
  135. };
  136. var hasTabIndex = function hasTabIndex2(node) {
  137. return !isNaN(parseInt(node.getAttribute('tabindex'), 10));
  138. };
  139. var getTabIndex = function getTabIndex2(node) {
  140. if (!node) {
  141. throw new Error('No node provided');
  142. }
  143. if (node.tabIndex < 0) {
  144. if (
  145. (/^(AUDIO|VIDEO|DETAILS)$/.test(node.tagName) || isContentEditable(node)) &&
  146. !hasTabIndex(node)
  147. ) {
  148. return 0;
  149. }
  150. }
  151. return node.tabIndex;
  152. };
  153. var getSortOrderTabIndex = function getSortOrderTabIndex2(node, isScope) {
  154. var tabIndex = getTabIndex(node);
  155. if (tabIndex < 0 && isScope && !hasTabIndex(node)) {
  156. return 0;
  157. }
  158. return tabIndex;
  159. };
  160. var sortOrderedTabbables = function sortOrderedTabbables2(a, b) {
  161. return a.tabIndex === b.tabIndex ? a.documentOrder - b.documentOrder : a.tabIndex - b.tabIndex;
  162. };
  163. var isInput = function isInput2(node) {
  164. return node.tagName === 'INPUT';
  165. };
  166. var isHiddenInput = function isHiddenInput2(node) {
  167. return isInput(node) && node.type === 'hidden';
  168. };
  169. var isDetailsWithSummary = function isDetailsWithSummary2(node) {
  170. var r =
  171. node.tagName === 'DETAILS' &&
  172. Array.prototype.slice.apply(node.children).some(function (child) {
  173. return child.tagName === 'SUMMARY';
  174. });
  175. return r;
  176. };
  177. var getCheckedRadio = function getCheckedRadio2(nodes, form) {
  178. for (var i = 0; i < nodes.length; i++) {
  179. if (nodes[i].checked && nodes[i].form === form) {
  180. return nodes[i];
  181. }
  182. }
  183. };
  184. var isTabbableRadio = function isTabbableRadio2(node) {
  185. if (!node.name) {
  186. return true;
  187. }
  188. var radioScope = node.form || getRootNode(node);
  189. var queryRadios = function queryRadios2(name) {
  190. return radioScope.querySelectorAll('input[type="radio"][name="' + name + '"]');
  191. };
  192. var radioSet;
  193. if (
  194. typeof window !== 'undefined' &&
  195. typeof window.CSS !== 'undefined' &&
  196. typeof window.CSS.escape === 'function'
  197. ) {
  198. radioSet = queryRadios(window.CSS.escape(node.name));
  199. } else {
  200. try {
  201. radioSet = queryRadios(node.name);
  202. } catch (err) {
  203. console.error(
  204. 'Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s',
  205. err.message
  206. );
  207. return false;
  208. }
  209. }
  210. var checked = getCheckedRadio(radioSet, node.form);
  211. return !checked || checked === node;
  212. };
  213. var isRadio = function isRadio2(node) {
  214. return isInput(node) && node.type === 'radio';
  215. };
  216. var isNonTabbableRadio = function isNonTabbableRadio2(node) {
  217. return isRadio(node) && !isTabbableRadio(node);
  218. };
  219. var isNodeAttached = function isNodeAttached2(node) {
  220. var _nodeRoot;
  221. var nodeRoot = node && getRootNode(node);
  222. var nodeRootHost =
  223. (_nodeRoot = nodeRoot) === null || _nodeRoot === void 0 ? void 0 : _nodeRoot.host;
  224. var attached = false;
  225. if (nodeRoot && nodeRoot !== node) {
  226. var _nodeRootHost, _nodeRootHost$ownerDo, _node$ownerDocument;
  227. attached = !!(
  228. ((_nodeRootHost = nodeRootHost) !== null &&
  229. _nodeRootHost !== void 0 &&
  230. (_nodeRootHost$ownerDo = _nodeRootHost.ownerDocument) !== null &&
  231. _nodeRootHost$ownerDo !== void 0 &&
  232. _nodeRootHost$ownerDo.contains(nodeRootHost)) ||
  233. (node !== null &&
  234. node !== void 0 &&
  235. (_node$ownerDocument = node.ownerDocument) !== null &&
  236. _node$ownerDocument !== void 0 &&
  237. _node$ownerDocument.contains(node))
  238. );
  239. while (!attached && nodeRootHost) {
  240. var _nodeRoot2, _nodeRootHost2, _nodeRootHost2$ownerD;
  241. nodeRoot = getRootNode(nodeRootHost);
  242. nodeRootHost =
  243. (_nodeRoot2 = nodeRoot) === null || _nodeRoot2 === void 0
  244. ? void 0
  245. : _nodeRoot2.host;
  246. attached = !!(
  247. (_nodeRootHost2 = nodeRootHost) !== null &&
  248. _nodeRootHost2 !== void 0 &&
  249. (_nodeRootHost2$ownerD = _nodeRootHost2.ownerDocument) !== null &&
  250. _nodeRootHost2$ownerD !== void 0 &&
  251. _nodeRootHost2$ownerD.contains(nodeRootHost)
  252. );
  253. }
  254. }
  255. return attached;
  256. };
  257. var isZeroArea = function isZeroArea2(node) {
  258. var _node$getBoundingClie = node.getBoundingClientRect(),
  259. width = _node$getBoundingClie.width,
  260. height = _node$getBoundingClie.height;
  261. return width === 0 && height === 0;
  262. };
  263. var isHidden = function isHidden2(node, _ref) {
  264. var displayCheck = _ref.displayCheck,
  265. getShadowRoot = _ref.getShadowRoot;
  266. if (getComputedStyle(node).visibility === 'hidden') {
  267. return true;
  268. }
  269. var isDirectSummary = matches.call(node, 'details>summary:first-of-type');
  270. var nodeUnderDetails = isDirectSummary ? node.parentElement : node;
  271. if (matches.call(nodeUnderDetails, 'details:not([open]) *')) {
  272. return true;
  273. }
  274. if (!displayCheck || displayCheck === 'full' || displayCheck === 'legacy-full') {
  275. if (typeof getShadowRoot === 'function') {
  276. var originalNode = node;
  277. while (node) {
  278. var parentElement = node.parentElement;
  279. var rootNode = getRootNode(node);
  280. if (
  281. parentElement &&
  282. !parentElement.shadowRoot &&
  283. getShadowRoot(parentElement) === true
  284. ) {
  285. return isZeroArea(node);
  286. } else if (node.assignedSlot) {
  287. node = node.assignedSlot;
  288. } else if (!parentElement && rootNode !== node.ownerDocument) {
  289. node = rootNode.host;
  290. } else {
  291. node = parentElement;
  292. }
  293. }
  294. node = originalNode;
  295. }
  296. if (isNodeAttached(node)) {
  297. return !node.getClientRects().length;
  298. }
  299. if (displayCheck !== 'legacy-full') {
  300. return true;
  301. }
  302. } else if (displayCheck === 'non-zero-area') {
  303. return isZeroArea(node);
  304. }
  305. return false;
  306. };
  307. var isDisabledFromFieldset = function isDisabledFromFieldset2(node) {
  308. if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) {
  309. var parentNode = node.parentElement;
  310. while (parentNode) {
  311. if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) {
  312. for (var i = 0; i < parentNode.children.length; i++) {
  313. var child = parentNode.children.item(i);
  314. if (child.tagName === 'LEGEND') {
  315. return matches.call(parentNode, 'fieldset[disabled] *')
  316. ? true
  317. : !child.contains(node);
  318. }
  319. }
  320. return true;
  321. }
  322. parentNode = parentNode.parentElement;
  323. }
  324. }
  325. return false;
  326. };
  327. var isNodeMatchingSelectorFocusable = function isNodeMatchingSelectorFocusable2(options, node) {
  328. if (
  329. node.disabled || // we must do an inert look up to filter out any elements inside an inert ancestor
  330. // because we're limited in the type of selectors we can use in JSDom (see related
  331. // note related to `candidateSelectors`)
  332. isInert(node) ||
  333. isHiddenInput(node) ||
  334. isHidden(node, options) || // For a details element with a summary, the summary element gets the focus
  335. isDetailsWithSummary(node) ||
  336. isDisabledFromFieldset(node)
  337. ) {
  338. return false;
  339. }
  340. return true;
  341. };
  342. var isNodeMatchingSelectorTabbable = function isNodeMatchingSelectorTabbable2(options, node) {
  343. if (
  344. isNonTabbableRadio(node) ||
  345. getTabIndex(node) < 0 ||
  346. !isNodeMatchingSelectorFocusable(options, node)
  347. ) {
  348. return false;
  349. }
  350. return true;
  351. };
  352. var isValidShadowRootTabbable = function isValidShadowRootTabbable2(shadowHostNode) {
  353. var tabIndex = parseInt(shadowHostNode.getAttribute('tabindex'), 10);
  354. if (isNaN(tabIndex) || tabIndex >= 0) {
  355. return true;
  356. }
  357. return false;
  358. };
  359. var sortByOrder = function sortByOrder2(candidates) {
  360. var regularTabbables = [];
  361. var orderedTabbables = [];
  362. candidates.forEach(function (item, i) {
  363. var isScope = !!item.scopeParent;
  364. var element = isScope ? item.scopeParent : item;
  365. var candidateTabindex = getSortOrderTabIndex(element, isScope);
  366. var elements = isScope ? sortByOrder2(item.candidates) : element;
  367. if (candidateTabindex === 0) {
  368. isScope
  369. ? regularTabbables.push.apply(regularTabbables, elements)
  370. : regularTabbables.push(element);
  371. } else {
  372. orderedTabbables.push({
  373. documentOrder: i,
  374. tabIndex: candidateTabindex,
  375. item,
  376. isScope,
  377. content: elements
  378. });
  379. }
  380. });
  381. return orderedTabbables
  382. .sort(sortOrderedTabbables)
  383. .reduce(function (acc, sortable) {
  384. sortable.isScope ? acc.push.apply(acc, sortable.content) : acc.push(sortable.content);
  385. return acc;
  386. }, [])
  387. .concat(regularTabbables);
  388. };
  389. var tabbable = function tabbable2(container, options) {
  390. options = options || {};
  391. var candidates;
  392. if (options.getShadowRoot) {
  393. candidates = getCandidatesIteratively([container], options.includeContainer, {
  394. filter: isNodeMatchingSelectorTabbable.bind(null, options),
  395. flatten: false,
  396. getShadowRoot: options.getShadowRoot,
  397. shadowRootFilter: isValidShadowRootTabbable
  398. });
  399. } else {
  400. candidates = getCandidates(
  401. container,
  402. options.includeContainer,
  403. isNodeMatchingSelectorTabbable.bind(null, options)
  404. );
  405. }
  406. return sortByOrder(candidates);
  407. };
  408. var focusable = function focusable2(container, options) {
  409. options = options || {};
  410. var candidates;
  411. if (options.getShadowRoot) {
  412. candidates = getCandidatesIteratively([container], options.includeContainer, {
  413. filter: isNodeMatchingSelectorFocusable.bind(null, options),
  414. flatten: true,
  415. getShadowRoot: options.getShadowRoot
  416. });
  417. } else {
  418. candidates = getCandidates(
  419. container,
  420. options.includeContainer,
  421. isNodeMatchingSelectorFocusable.bind(null, options)
  422. );
  423. }
  424. return candidates;
  425. };
  426. var isTabbable = function isTabbable2(node, options) {
  427. options = options || {};
  428. if (!node) {
  429. throw new Error('No node provided');
  430. }
  431. if (matches.call(node, candidateSelector) === false) {
  432. return false;
  433. }
  434. return isNodeMatchingSelectorTabbable(options, node);
  435. };
  436. var focusableCandidateSelector = candidateSelectors.concat('iframe').join(',');
  437. var isFocusable = function isFocusable2(node, options) {
  438. options = options || {};
  439. if (!node) {
  440. throw new Error('No node provided');
  441. }
  442. if (matches.call(node, focusableCandidateSelector) === false) {
  443. return false;
  444. }
  445. return isNodeMatchingSelectorFocusable(options, node);
  446. };
  447. // node_modules/.pnpm/focus-trap@7.5.4/node_modules/focus-trap/dist/focus-trap.esm.js
  448. function ownKeys(e, r) {
  449. var t = Object.keys(e);
  450. if (Object.getOwnPropertySymbols) {
  451. var o = Object.getOwnPropertySymbols(e);
  452. r &&
  453. (o = o.filter(function (r2) {
  454. return Object.getOwnPropertyDescriptor(e, r2).enumerable;
  455. })),
  456. t.push.apply(t, o);
  457. }
  458. return t;
  459. }
  460. function _objectSpread2(e) {
  461. for (var r = 1; r < arguments.length; r++) {
  462. var t = null != arguments[r] ? arguments[r] : {};
  463. r % 2
  464. ? ownKeys(Object(t), true).forEach(function (r2) {
  465. _defineProperty(e, r2, t[r2]);
  466. })
  467. : Object.getOwnPropertyDescriptors
  468. ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t))
  469. : ownKeys(Object(t)).forEach(function (r2) {
  470. Object.defineProperty(e, r2, Object.getOwnPropertyDescriptor(t, r2));
  471. });
  472. }
  473. return e;
  474. }
  475. function _defineProperty(obj, key, value) {
  476. key = _toPropertyKey(key);
  477. if (key in obj) {
  478. Object.defineProperty(obj, key, {
  479. value,
  480. enumerable: true,
  481. configurable: true,
  482. writable: true
  483. });
  484. } else {
  485. obj[key] = value;
  486. }
  487. return obj;
  488. }
  489. function _toPrimitive(input, hint) {
  490. if (typeof input !== 'object' || input === null) return input;
  491. var prim = input[Symbol.toPrimitive];
  492. if (prim !== void 0) {
  493. var res = prim.call(input, hint || 'default');
  494. if (typeof res !== 'object') return res;
  495. throw new TypeError('@@toPrimitive must return a primitive value.');
  496. }
  497. return (hint === 'string' ? String : Number)(input);
  498. }
  499. function _toPropertyKey(arg) {
  500. var key = _toPrimitive(arg, 'string');
  501. return typeof key === 'symbol' ? key : String(key);
  502. }
  503. var activeFocusTraps = {
  504. activateTrap: function activateTrap(trapStack, trap) {
  505. if (trapStack.length > 0) {
  506. var activeTrap = trapStack[trapStack.length - 1];
  507. if (activeTrap !== trap) {
  508. activeTrap.pause();
  509. }
  510. }
  511. var trapIndex = trapStack.indexOf(trap);
  512. if (trapIndex === -1) {
  513. trapStack.push(trap);
  514. } else {
  515. trapStack.splice(trapIndex, 1);
  516. trapStack.push(trap);
  517. }
  518. },
  519. deactivateTrap: function deactivateTrap(trapStack, trap) {
  520. var trapIndex = trapStack.indexOf(trap);
  521. if (trapIndex !== -1) {
  522. trapStack.splice(trapIndex, 1);
  523. }
  524. if (trapStack.length > 0) {
  525. trapStack[trapStack.length - 1].unpause();
  526. }
  527. }
  528. };
  529. var isSelectableInput = function isSelectableInput2(node) {
  530. return (
  531. node.tagName && node.tagName.toLowerCase() === 'input' && typeof node.select === 'function'
  532. );
  533. };
  534. var isEscapeEvent = function isEscapeEvent2(e) {
  535. return (
  536. (e === null || e === void 0 ? void 0 : e.key) === 'Escape' ||
  537. (e === null || e === void 0 ? void 0 : e.key) === 'Esc' ||
  538. (e === null || e === void 0 ? void 0 : e.keyCode) === 27
  539. );
  540. };
  541. var isTabEvent = function isTabEvent2(e) {
  542. return (
  543. (e === null || e === void 0 ? void 0 : e.key) === 'Tab' ||
  544. (e === null || e === void 0 ? void 0 : e.keyCode) === 9
  545. );
  546. };
  547. var isKeyForward = function isKeyForward2(e) {
  548. return isTabEvent(e) && !e.shiftKey;
  549. };
  550. var isKeyBackward = function isKeyBackward2(e) {
  551. return isTabEvent(e) && e.shiftKey;
  552. };
  553. var delay = function delay2(fn) {
  554. return setTimeout(fn, 0);
  555. };
  556. var findIndex = function findIndex2(arr, fn) {
  557. var idx = -1;
  558. arr.every(function (value, i) {
  559. if (fn(value)) {
  560. idx = i;
  561. return false;
  562. }
  563. return true;
  564. });
  565. return idx;
  566. };
  567. var valueOrHandler = function valueOrHandler2(value) {
  568. for (
  569. var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1;
  570. _key < _len;
  571. _key++
  572. ) {
  573. params[_key - 1] = arguments[_key];
  574. }
  575. return typeof value === 'function' ? value.apply(void 0, params) : value;
  576. };
  577. var getActualTarget = function getActualTarget2(event) {
  578. return event.target.shadowRoot && typeof event.composedPath === 'function'
  579. ? event.composedPath()[0]
  580. : event.target;
  581. };
  582. var internalTrapStack = [];
  583. var createFocusTrap = function createFocusTrap2(elements, userOptions) {
  584. var doc =
  585. (userOptions === null || userOptions === void 0 ? void 0 : userOptions.document) ||
  586. document;
  587. var trapStack =
  588. (userOptions === null || userOptions === void 0 ? void 0 : userOptions.trapStack) ||
  589. internalTrapStack;
  590. var config = _objectSpread2(
  591. {
  592. returnFocusOnDeactivate: true,
  593. escapeDeactivates: true,
  594. delayInitialFocus: true,
  595. isKeyForward,
  596. isKeyBackward
  597. },
  598. userOptions
  599. );
  600. var state = {
  601. // containers given to createFocusTrap()
  602. // @type {Array<HTMLElement>}
  603. containers: [],
  604. // list of objects identifying tabbable nodes in `containers` in the trap
  605. // NOTE: it's possible that a group has no tabbable nodes if nodes get removed while the trap
  606. // is active, but the trap should never get to a state where there isn't at least one group
  607. // with at least one tabbable node in it (that would lead to an error condition that would
  608. // result in an error being thrown)
  609. // @type {Array<{
  610. // container: HTMLElement,
  611. // tabbableNodes: Array<HTMLElement>, // empty if none
  612. // focusableNodes: Array<HTMLElement>, // empty if none
  613. // posTabIndexesFound: boolean,
  614. // firstTabbableNode: HTMLElement|undefined,
  615. // lastTabbableNode: HTMLElement|undefined,
  616. // firstDomTabbableNode: HTMLElement|undefined,
  617. // lastDomTabbableNode: HTMLElement|undefined,
  618. // nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined
  619. // }>}
  620. containerGroups: [],
  621. // same order/length as `containers` list
  622. // references to objects in `containerGroups`, but only those that actually have
  623. // tabbable nodes in them
  624. // NOTE: same order as `containers` and `containerGroups`, but __not necessarily__
  625. // the same length
  626. tabbableGroups: [],
  627. nodeFocusedBeforeActivation: null,
  628. mostRecentlyFocusedNode: null,
  629. active: false,
  630. paused: false,
  631. // timer ID for when delayInitialFocus is true and initial focus in this trap
  632. // has been delayed during activation
  633. delayInitialFocusTimer: void 0,
  634. // the most recent KeyboardEvent for the configured nav key (typically [SHIFT+]TAB), if any
  635. recentNavEvent: void 0
  636. };
  637. var trap;
  638. var getOption = function getOption2(configOverrideOptions, optionName, configOptionName) {
  639. return configOverrideOptions && configOverrideOptions[optionName] !== void 0
  640. ? configOverrideOptions[optionName]
  641. : config[configOptionName || optionName];
  642. };
  643. var findContainerIndex = function findContainerIndex2(element, event) {
  644. var composedPath =
  645. typeof (event === null || event === void 0 ? void 0 : event.composedPath) === 'function'
  646. ? event.composedPath()
  647. : void 0;
  648. return state.containerGroups.findIndex(function (_ref) {
  649. var container = _ref.container,
  650. tabbableNodes = _ref.tabbableNodes;
  651. return (
  652. container.contains(element) || // fall back to explicit tabbable search which will take into consideration any
  653. // web components if the `tabbableOptions.getShadowRoot` option was used for
  654. // the trap, enabling shadow DOM support in tabbable (`Node.contains()` doesn't
  655. // look inside web components even if open)
  656. (composedPath === null || composedPath === void 0
  657. ? void 0
  658. : composedPath.includes(container)) ||
  659. tabbableNodes.find(function (node) {
  660. return node === element;
  661. })
  662. );
  663. });
  664. };
  665. var getNodeForOption = function getNodeForOption2(optionName) {
  666. var optionValue = config[optionName];
  667. if (typeof optionValue === 'function') {
  668. for (
  669. var _len2 = arguments.length,
  670. params = new Array(_len2 > 1 ? _len2 - 1 : 0),
  671. _key2 = 1;
  672. _key2 < _len2;
  673. _key2++
  674. ) {
  675. params[_key2 - 1] = arguments[_key2];
  676. }
  677. optionValue = optionValue.apply(void 0, params);
  678. }
  679. if (optionValue === true) {
  680. optionValue = void 0;
  681. }
  682. if (!optionValue) {
  683. if (optionValue === void 0 || optionValue === false) {
  684. return optionValue;
  685. }
  686. throw new Error(
  687. '`'.concat(
  688. optionName,
  689. '` was specified but was not a node, or did not return a node'
  690. )
  691. );
  692. }
  693. var node = optionValue;
  694. if (typeof optionValue === 'string') {
  695. node = doc.querySelector(optionValue);
  696. if (!node) {
  697. throw new Error('`'.concat(optionName, '` as selector refers to no known node'));
  698. }
  699. }
  700. return node;
  701. };
  702. var getInitialFocusNode = function getInitialFocusNode2() {
  703. var node = getNodeForOption('initialFocus');
  704. if (node === false) {
  705. return false;
  706. }
  707. if (node === void 0 || !isFocusable(node, config.tabbableOptions)) {
  708. if (findContainerIndex(doc.activeElement) >= 0) {
  709. node = doc.activeElement;
  710. } else {
  711. var firstTabbableGroup = state.tabbableGroups[0];
  712. var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode;
  713. node = firstTabbableNode || getNodeForOption('fallbackFocus');
  714. }
  715. }
  716. if (!node) {
  717. throw new Error('Your focus-trap needs to have at least one focusable element');
  718. }
  719. return node;
  720. };
  721. var updateTabbableNodes = function updateTabbableNodes2() {
  722. state.containerGroups = state.containers.map(function (container) {
  723. var tabbableNodes = tabbable(container, config.tabbableOptions);
  724. var focusableNodes = focusable(container, config.tabbableOptions);
  725. var firstTabbableNode = tabbableNodes.length > 0 ? tabbableNodes[0] : void 0;
  726. var lastTabbableNode =
  727. tabbableNodes.length > 0 ? tabbableNodes[tabbableNodes.length - 1] : void 0;
  728. var firstDomTabbableNode = focusableNodes.find(function (node) {
  729. return isTabbable(node);
  730. });
  731. var lastDomTabbableNode = focusableNodes
  732. .slice()
  733. .reverse()
  734. .find(function (node) {
  735. return isTabbable(node);
  736. });
  737. var posTabIndexesFound = !!tabbableNodes.find(function (node) {
  738. return getTabIndex(node) > 0;
  739. });
  740. return {
  741. container,
  742. tabbableNodes,
  743. focusableNodes,
  744. /** True if at least one node with positive `tabindex` was found in this container. */
  745. posTabIndexesFound,
  746. /** First tabbable node in container, __tabindex__ order; `undefined` if none. */
  747. firstTabbableNode,
  748. /** Last tabbable node in container, __tabindex__ order; `undefined` if none. */
  749. lastTabbableNode,
  750. // NOTE: DOM order is NOT NECESSARILY "document position" order, but figuring that out
  751. // would require more than just https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
  752. // because that API doesn't work with Shadow DOM as well as it should (@see
  753. // https://github.com/whatwg/dom/issues/320) and since this first/last is only needed, so far,
  754. // to address an edge case related to positive tabindex support, this seems like a much easier,
  755. // "close enough most of the time" alternative for positive tabindexes which should generally
  756. // be avoided anyway...
  757. /** First tabbable node in container, __DOM__ order; `undefined` if none. */
  758. firstDomTabbableNode,
  759. /** Last tabbable node in container, __DOM__ order; `undefined` if none. */
  760. lastDomTabbableNode,
  761. /**
  762. * Finds the __tabbable__ node that follows the given node in the specified direction,
  763. * in this container, if any.
  764. * @param {HTMLElement} node
  765. * @param {boolean} [forward] True if going in forward tab order; false if going
  766. * in reverse.
  767. * @returns {HTMLElement|undefined} The next tabbable node, if any.
  768. */
  769. nextTabbableNode: function nextTabbableNode(node) {
  770. var forward =
  771. arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
  772. var nodeIdx = tabbableNodes.indexOf(node);
  773. if (nodeIdx < 0) {
  774. if (forward) {
  775. return focusableNodes
  776. .slice(focusableNodes.indexOf(node) + 1)
  777. .find(function (el) {
  778. return isTabbable(el);
  779. });
  780. }
  781. return focusableNodes
  782. .slice(0, focusableNodes.indexOf(node))
  783. .reverse()
  784. .find(function (el) {
  785. return isTabbable(el);
  786. });
  787. }
  788. return tabbableNodes[nodeIdx + (forward ? 1 : -1)];
  789. }
  790. };
  791. });
  792. state.tabbableGroups = state.containerGroups.filter(function (group) {
  793. return group.tabbableNodes.length > 0;
  794. });
  795. if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus')) {
  796. throw new Error(
  797. 'Your focus-trap must have at least one container with at least one tabbable node in it at all times'
  798. );
  799. }
  800. if (
  801. state.containerGroups.find(function (g) {
  802. return g.posTabIndexesFound;
  803. }) &&
  804. state.containerGroups.length > 1
  805. ) {
  806. throw new Error(
  807. "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."
  808. );
  809. }
  810. };
  811. var getActiveElement = function getActiveElement2(el) {
  812. var activeElement = el.activeElement;
  813. if (!activeElement) {
  814. return;
  815. }
  816. if (activeElement.shadowRoot && activeElement.shadowRoot.activeElement !== null) {
  817. return getActiveElement2(activeElement.shadowRoot);
  818. }
  819. return activeElement;
  820. };
  821. var tryFocus = function tryFocus2(node) {
  822. if (node === false) {
  823. return;
  824. }
  825. if (node === getActiveElement(document)) {
  826. return;
  827. }
  828. if (!node || !node.focus) {
  829. tryFocus2(getInitialFocusNode());
  830. return;
  831. }
  832. node.focus({
  833. preventScroll: !!config.preventScroll
  834. });
  835. state.mostRecentlyFocusedNode = node;
  836. if (isSelectableInput(node)) {
  837. node.select();
  838. }
  839. };
  840. var getReturnFocusNode = function getReturnFocusNode2(previousActiveElement) {
  841. var node = getNodeForOption('setReturnFocus', previousActiveElement);
  842. return node ? node : node === false ? false : previousActiveElement;
  843. };
  844. var findNextNavNode = function findNextNavNode2(_ref2) {
  845. var target = _ref2.target,
  846. event = _ref2.event,
  847. _ref2$isBackward = _ref2.isBackward,
  848. isBackward = _ref2$isBackward === void 0 ? false : _ref2$isBackward;
  849. target = target || getActualTarget(event);
  850. updateTabbableNodes();
  851. var destinationNode = null;
  852. if (state.tabbableGroups.length > 0) {
  853. var containerIndex = findContainerIndex(target, event);
  854. var containerGroup =
  855. containerIndex >= 0 ? state.containerGroups[containerIndex] : void 0;
  856. if (containerIndex < 0) {
  857. if (isBackward) {
  858. destinationNode =
  859. state.tabbableGroups[state.tabbableGroups.length - 1].lastTabbableNode;
  860. } else {
  861. destinationNode = state.tabbableGroups[0].firstTabbableNode;
  862. }
  863. } else if (isBackward) {
  864. var startOfGroupIndex = findIndex(state.tabbableGroups, function (_ref3) {
  865. var firstTabbableNode = _ref3.firstTabbableNode;
  866. return target === firstTabbableNode;
  867. });
  868. if (
  869. startOfGroupIndex < 0 &&
  870. (containerGroup.container === target ||
  871. (isFocusable(target, config.tabbableOptions) &&
  872. !isTabbable(target, config.tabbableOptions) &&
  873. !containerGroup.nextTabbableNode(target, false)))
  874. ) {
  875. startOfGroupIndex = containerIndex;
  876. }
  877. if (startOfGroupIndex >= 0) {
  878. var destinationGroupIndex =
  879. startOfGroupIndex === 0
  880. ? state.tabbableGroups.length - 1
  881. : startOfGroupIndex - 1;
  882. var destinationGroup = state.tabbableGroups[destinationGroupIndex];
  883. destinationNode =
  884. getTabIndex(target) >= 0
  885. ? destinationGroup.lastTabbableNode
  886. : destinationGroup.lastDomTabbableNode;
  887. } else if (!isTabEvent(event)) {
  888. destinationNode = containerGroup.nextTabbableNode(target, false);
  889. }
  890. } else {
  891. var lastOfGroupIndex = findIndex(state.tabbableGroups, function (_ref4) {
  892. var lastTabbableNode = _ref4.lastTabbableNode;
  893. return target === lastTabbableNode;
  894. });
  895. if (
  896. lastOfGroupIndex < 0 &&
  897. (containerGroup.container === target ||
  898. (isFocusable(target, config.tabbableOptions) &&
  899. !isTabbable(target, config.tabbableOptions) &&
  900. !containerGroup.nextTabbableNode(target)))
  901. ) {
  902. lastOfGroupIndex = containerIndex;
  903. }
  904. if (lastOfGroupIndex >= 0) {
  905. var _destinationGroupIndex =
  906. lastOfGroupIndex === state.tabbableGroups.length - 1
  907. ? 0
  908. : lastOfGroupIndex + 1;
  909. var _destinationGroup = state.tabbableGroups[_destinationGroupIndex];
  910. destinationNode =
  911. getTabIndex(target) >= 0
  912. ? _destinationGroup.firstTabbableNode
  913. : _destinationGroup.firstDomTabbableNode;
  914. } else if (!isTabEvent(event)) {
  915. destinationNode = containerGroup.nextTabbableNode(target);
  916. }
  917. }
  918. } else {
  919. destinationNode = getNodeForOption('fallbackFocus');
  920. }
  921. return destinationNode;
  922. };
  923. var checkPointerDown = function checkPointerDown2(e) {
  924. var target = getActualTarget(e);
  925. if (findContainerIndex(target, e) >= 0) {
  926. return;
  927. }
  928. if (valueOrHandler(config.clickOutsideDeactivates, e)) {
  929. trap.deactivate({
  930. // NOTE: by setting `returnFocus: false`, deactivate() will do nothing,
  931. // which will result in the outside click setting focus to the node
  932. // that was clicked (and if not focusable, to "nothing"); by setting
  933. // `returnFocus: true`, we'll attempt to re-focus the node originally-focused
  934. // on activation (or the configured `setReturnFocus` node), whether the
  935. // outside click was on a focusable node or not
  936. returnFocus: config.returnFocusOnDeactivate
  937. });
  938. return;
  939. }
  940. if (valueOrHandler(config.allowOutsideClick, e)) {
  941. return;
  942. }
  943. e.preventDefault();
  944. };
  945. var checkFocusIn = function checkFocusIn2(event) {
  946. var target = getActualTarget(event);
  947. var targetContained = findContainerIndex(target, event) >= 0;
  948. if (targetContained || target instanceof Document) {
  949. if (targetContained) {
  950. state.mostRecentlyFocusedNode = target;
  951. }
  952. } else {
  953. event.stopImmediatePropagation();
  954. var nextNode;
  955. var navAcrossContainers = true;
  956. if (state.mostRecentlyFocusedNode) {
  957. if (getTabIndex(state.mostRecentlyFocusedNode) > 0) {
  958. var mruContainerIdx = findContainerIndex(state.mostRecentlyFocusedNode);
  959. var tabbableNodes = state.containerGroups[mruContainerIdx].tabbableNodes;
  960. if (tabbableNodes.length > 0) {
  961. var mruTabIdx = tabbableNodes.findIndex(function (node) {
  962. return node === state.mostRecentlyFocusedNode;
  963. });
  964. if (mruTabIdx >= 0) {
  965. if (config.isKeyForward(state.recentNavEvent)) {
  966. if (mruTabIdx + 1 < tabbableNodes.length) {
  967. nextNode = tabbableNodes[mruTabIdx + 1];
  968. navAcrossContainers = false;
  969. }
  970. } else {
  971. if (mruTabIdx - 1 >= 0) {
  972. nextNode = tabbableNodes[mruTabIdx - 1];
  973. navAcrossContainers = false;
  974. }
  975. }
  976. }
  977. }
  978. } else {
  979. if (
  980. !state.containerGroups.some(function (g) {
  981. return g.tabbableNodes.some(function (n) {
  982. return getTabIndex(n) > 0;
  983. });
  984. })
  985. ) {
  986. navAcrossContainers = false;
  987. }
  988. }
  989. } else {
  990. navAcrossContainers = false;
  991. }
  992. if (navAcrossContainers) {
  993. nextNode = findNextNavNode({
  994. // move FROM the MRU node, not event-related node (which will be the node that is
  995. // outside the trap causing the focus escape we're trying to fix)
  996. target: state.mostRecentlyFocusedNode,
  997. isBackward: config.isKeyBackward(state.recentNavEvent)
  998. });
  999. }
  1000. if (nextNode) {
  1001. tryFocus(nextNode);
  1002. } else {
  1003. tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());
  1004. }
  1005. }
  1006. state.recentNavEvent = void 0;
  1007. };
  1008. var checkKeyNav = function checkKeyNav2(event) {
  1009. var isBackward = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : false;
  1010. state.recentNavEvent = event;
  1011. var destinationNode = findNextNavNode({
  1012. event,
  1013. isBackward
  1014. });
  1015. if (destinationNode) {
  1016. if (isTabEvent(event)) {
  1017. event.preventDefault();
  1018. }
  1019. tryFocus(destinationNode);
  1020. }
  1021. };
  1022. var checkKey = function checkKey2(event) {
  1023. if (isEscapeEvent(event) && valueOrHandler(config.escapeDeactivates, event) !== false) {
  1024. event.preventDefault();
  1025. trap.deactivate();
  1026. return;
  1027. }
  1028. if (config.isKeyForward(event) || config.isKeyBackward(event)) {
  1029. checkKeyNav(event, config.isKeyBackward(event));
  1030. }
  1031. };
  1032. var checkClick = function checkClick2(e) {
  1033. var target = getActualTarget(e);
  1034. if (findContainerIndex(target, e) >= 0) {
  1035. return;
  1036. }
  1037. if (valueOrHandler(config.clickOutsideDeactivates, e)) {
  1038. return;
  1039. }
  1040. if (valueOrHandler(config.allowOutsideClick, e)) {
  1041. return;
  1042. }
  1043. e.preventDefault();
  1044. e.stopImmediatePropagation();
  1045. };
  1046. var addListeners = function addListeners2() {
  1047. if (!state.active) {
  1048. return;
  1049. }
  1050. activeFocusTraps.activateTrap(trapStack, trap);
  1051. state.delayInitialFocusTimer = config.delayInitialFocus
  1052. ? delay(function () {
  1053. tryFocus(getInitialFocusNode());
  1054. })
  1055. : tryFocus(getInitialFocusNode());
  1056. doc.addEventListener('focusin', checkFocusIn, true);
  1057. doc.addEventListener('mousedown', checkPointerDown, {
  1058. capture: true,
  1059. passive: false
  1060. });
  1061. doc.addEventListener('touchstart', checkPointerDown, {
  1062. capture: true,
  1063. passive: false
  1064. });
  1065. doc.addEventListener('click', checkClick, {
  1066. capture: true,
  1067. passive: false
  1068. });
  1069. doc.addEventListener('keydown', checkKey, {
  1070. capture: true,
  1071. passive: false
  1072. });
  1073. return trap;
  1074. };
  1075. var removeListeners = function removeListeners2() {
  1076. if (!state.active) {
  1077. return;
  1078. }
  1079. doc.removeEventListener('focusin', checkFocusIn, true);
  1080. doc.removeEventListener('mousedown', checkPointerDown, true);
  1081. doc.removeEventListener('touchstart', checkPointerDown, true);
  1082. doc.removeEventListener('click', checkClick, true);
  1083. doc.removeEventListener('keydown', checkKey, true);
  1084. return trap;
  1085. };
  1086. var checkDomRemoval = function checkDomRemoval2(mutations) {
  1087. var isFocusedNodeRemoved = mutations.some(function (mutation) {
  1088. var removedNodes = Array.from(mutation.removedNodes);
  1089. return removedNodes.some(function (node) {
  1090. return node === state.mostRecentlyFocusedNode;
  1091. });
  1092. });
  1093. if (isFocusedNodeRemoved) {
  1094. tryFocus(getInitialFocusNode());
  1095. }
  1096. };
  1097. var mutationObserver =
  1098. typeof window !== 'undefined' && 'MutationObserver' in window
  1099. ? new MutationObserver(checkDomRemoval)
  1100. : void 0;
  1101. var updateObservedNodes = function updateObservedNodes2() {
  1102. if (!mutationObserver) {
  1103. return;
  1104. }
  1105. mutationObserver.disconnect();
  1106. if (state.active && !state.paused) {
  1107. state.containers.map(function (container) {
  1108. mutationObserver.observe(container, {
  1109. subtree: true,
  1110. childList: true
  1111. });
  1112. });
  1113. }
  1114. };
  1115. trap = {
  1116. get active() {
  1117. return state.active;
  1118. },
  1119. get paused() {
  1120. return state.paused;
  1121. },
  1122. activate: function activate(activateOptions) {
  1123. if (state.active) {
  1124. return this;
  1125. }
  1126. var onActivate = getOption(activateOptions, 'onActivate');
  1127. var onPostActivate = getOption(activateOptions, 'onPostActivate');
  1128. var checkCanFocusTrap = getOption(activateOptions, 'checkCanFocusTrap');
  1129. if (!checkCanFocusTrap) {
  1130. updateTabbableNodes();
  1131. }
  1132. state.active = true;
  1133. state.paused = false;
  1134. state.nodeFocusedBeforeActivation = doc.activeElement;
  1135. onActivate === null || onActivate === void 0 || onActivate();
  1136. var finishActivation = function finishActivation2() {
  1137. if (checkCanFocusTrap) {
  1138. updateTabbableNodes();
  1139. }
  1140. addListeners();
  1141. updateObservedNodes();
  1142. onPostActivate === null || onPostActivate === void 0 || onPostActivate();
  1143. };
  1144. if (checkCanFocusTrap) {
  1145. checkCanFocusTrap(state.containers.concat()).then(
  1146. finishActivation,
  1147. finishActivation
  1148. );
  1149. return this;
  1150. }
  1151. finishActivation();
  1152. return this;
  1153. },
  1154. deactivate: function deactivate(deactivateOptions) {
  1155. if (!state.active) {
  1156. return this;
  1157. }
  1158. var options = _objectSpread2(
  1159. {
  1160. onDeactivate: config.onDeactivate,
  1161. onPostDeactivate: config.onPostDeactivate,
  1162. checkCanReturnFocus: config.checkCanReturnFocus
  1163. },
  1164. deactivateOptions
  1165. );
  1166. clearTimeout(state.delayInitialFocusTimer);
  1167. state.delayInitialFocusTimer = void 0;
  1168. removeListeners();
  1169. state.active = false;
  1170. state.paused = false;
  1171. updateObservedNodes();
  1172. activeFocusTraps.deactivateTrap(trapStack, trap);
  1173. var onDeactivate = getOption(options, 'onDeactivate');
  1174. var onPostDeactivate = getOption(options, 'onPostDeactivate');
  1175. var checkCanReturnFocus = getOption(options, 'checkCanReturnFocus');
  1176. var returnFocus = getOption(options, 'returnFocus', 'returnFocusOnDeactivate');
  1177. onDeactivate === null || onDeactivate === void 0 || onDeactivate();
  1178. var finishDeactivation = function finishDeactivation2() {
  1179. delay(function () {
  1180. if (returnFocus) {
  1181. tryFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation));
  1182. }
  1183. onPostDeactivate === null || onPostDeactivate === void 0 || onPostDeactivate();
  1184. });
  1185. };
  1186. if (returnFocus && checkCanReturnFocus) {
  1187. checkCanReturnFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation)).then(
  1188. finishDeactivation,
  1189. finishDeactivation
  1190. );
  1191. return this;
  1192. }
  1193. finishDeactivation();
  1194. return this;
  1195. },
  1196. pause: function pause(pauseOptions) {
  1197. if (state.paused || !state.active) {
  1198. return this;
  1199. }
  1200. var onPause = getOption(pauseOptions, 'onPause');
  1201. var onPostPause = getOption(pauseOptions, 'onPostPause');
  1202. state.paused = true;
  1203. onPause === null || onPause === void 0 || onPause();
  1204. removeListeners();
  1205. updateObservedNodes();
  1206. onPostPause === null || onPostPause === void 0 || onPostPause();
  1207. return this;
  1208. },
  1209. unpause: function unpause(unpauseOptions) {
  1210. if (!state.paused || !state.active) {
  1211. return this;
  1212. }
  1213. var onUnpause = getOption(unpauseOptions, 'onUnpause');
  1214. var onPostUnpause = getOption(unpauseOptions, 'onPostUnpause');
  1215. state.paused = false;
  1216. onUnpause === null || onUnpause === void 0 || onUnpause();
  1217. updateTabbableNodes();
  1218. addListeners();
  1219. updateObservedNodes();
  1220. onPostUnpause === null || onPostUnpause === void 0 || onPostUnpause();
  1221. return this;
  1222. },
  1223. updateContainerElements: function updateContainerElements(containerElements) {
  1224. var elementsAsArray = [].concat(containerElements).filter(Boolean);
  1225. state.containers = elementsAsArray.map(function (element) {
  1226. return typeof element === 'string' ? doc.querySelector(element) : element;
  1227. });
  1228. if (state.active) {
  1229. updateTabbableNodes();
  1230. }
  1231. updateObservedNodes();
  1232. return this;
  1233. }
  1234. };
  1235. trap.updateContainerElements(elements);
  1236. return trap;
  1237. };
  1238. // node_modules/.pnpm/@vueuse+integrations@10.7.2_axios@1.6.5_focus-trap@7.5.4_nprogress@0.2.0_vue@3.4.15/node_modules/@vueuse/integrations/useFocusTrap.mjs
  1239. function useFocusTrap(target, options = {}) {
  1240. let trap;
  1241. const { immediate, ...focusTrapOptions } = options;
  1242. const hasFocus = ref(false);
  1243. const isPaused = ref(false);
  1244. const activate = opts => trap && trap.activate(opts);
  1245. const deactivate = opts => trap && trap.deactivate(opts);
  1246. const pause = () => {
  1247. if (trap) {
  1248. trap.pause();
  1249. isPaused.value = true;
  1250. }
  1251. };
  1252. const unpause = () => {
  1253. if (trap) {
  1254. trap.unpause();
  1255. isPaused.value = false;
  1256. }
  1257. };
  1258. watch(
  1259. () => unrefElement(target),
  1260. el => {
  1261. if (!el) return;
  1262. trap = createFocusTrap(el, {
  1263. ...focusTrapOptions,
  1264. onActivate() {
  1265. hasFocus.value = true;
  1266. if (options.onActivate) options.onActivate();
  1267. },
  1268. onDeactivate() {
  1269. hasFocus.value = false;
  1270. if (options.onDeactivate) options.onDeactivate();
  1271. }
  1272. });
  1273. if (immediate) activate();
  1274. },
  1275. { flush: 'post' }
  1276. );
  1277. tryOnScopeDispose(() => deactivate());
  1278. return {
  1279. hasFocus,
  1280. isPaused,
  1281. activate,
  1282. deactivate,
  1283. pause,
  1284. unpause
  1285. };
  1286. }
  1287. export { useFocusTrap };
  1288. /*! Bundled license information:
  1289. tabbable/dist/index.esm.js:
  1290. (*!
  1291. * tabbable 6.2.0
  1292. * @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
  1293. *)
  1294. focus-trap/dist/focus-trap.esm.js:
  1295. (*!
  1296. * focus-trap 7.5.4
  1297. * @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
  1298. *)
  1299. */
  1300. //# sourceMappingURL=vitepress___@vueuse_integrations_useFocusTrap.js.map