/*! * focus-trap 7.6.2 * @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE */ import{isFocusable as e,tabbable as t,focusable as n,isTabbable as o,getTabIndex as r}from"tabbable";function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,o=Array(t);n0){var n=e[e.length-1];n!==t&&n.pause()}var o=e.indexOf(t);-1===o||e.splice(o,1),e.push(t)},d=function(e,t){var n=e.indexOf(t);-1!==n&&e.splice(n,1),e.length>0&&e[e.length-1].unpause()},f=function(e){return"Tab"===(null==e?void 0:e.key)||9===(null==e?void 0:e.keyCode)},b=function(e){return f(e)&&!e.shiftKey},v=function(e){return f(e)&&e.shiftKey},p=function(e){return setTimeout(e,0)},m=function(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),o=1;o1&&void 0!==arguments[1]?arguments[1]:{},n=t.hasFallback,o=void 0!==n&&n,r=t.params,a=void 0===r?[]:r,i=N[e];if("function"==typeof i&&(i=i.apply(void 0,s(a))),!0===i&&(i=void 0),!i){if(void 0===i||!1===i)return i;throw new Error("`".concat(e,"` was specified but was not a node, or did not return a node"))}var u=i;if("string"==typeof i){try{u=w.querySelector(i)}catch(t){throw new Error("`".concat(e,'` appears to be an invalid selector; error="').concat(t.message,'"'))}if(!u&&!o)throw new Error("`".concat(e,"` as selector refers to no known node"))}return u},T=function(){var t=k("initialFocus",{hasFallback:!0});if(!1===t)return!1;if(void 0===t||t&&!e(t,N.tabbableOptions))if(E(w.activeElement)>=0)t=w.activeElement;else{var n=F.tabbableGroups[0];t=n&&n.firstTabbableNode||k("fallbackFocus")}else null===t&&(t=k("fallbackFocus"));if(!t)throw new Error("Your focus-trap needs to have at least one focusable element");return t},D=function(){if(F.containerGroups=F.containers.map((function(e){var a=t(e,N.tabbableOptions),i=n(e,N.tabbableOptions),u=a.length>0?a[0]:void 0,c=a.length>0?a[a.length-1]:void 0,s=i.find((function(e){return o(e)})),l=i.slice().reverse().find((function(e){return o(e)})),d=!!a.find((function(e){return r(e)>0}));return{container:e,tabbableNodes:a,focusableNodes:i,posTabIndexesFound:d,firstTabbableNode:u,lastTabbableNode:c,firstDomTabbableNode:s,lastDomTabbableNode:l,nextTabbableNode:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=a.indexOf(e);return n<0?t?i.slice(i.indexOf(e)+1).find((function(e){return o(e)})):i.slice(0,i.indexOf(e)).reverse().find((function(e){return o(e)})):a[n+(t?1:-1)]}}})),F.tabbableGroups=F.containerGroups.filter((function(e){return e.tabbableNodes.length>0})),F.tabbableGroups.length<=0&&!k("fallbackFocus"))throw new Error("Your focus-trap must have at least one container with at least one tabbable node in it at all times");if(F.containerGroups.find((function(e){return e.posTabIndexesFound}))&&F.containerGroups.length>1)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.")},P=function(e){var t=e.activeElement;if(t)return t.shadowRoot&&null!==t.shadowRoot.activeElement?P(t.shadowRoot):t},G=function(e){!1!==e&&e!==P(document)&&(e&&e.focus?(e.focus({preventScroll:!!N.preventScroll}),F.mostRecentlyFocusedNode=e,function(e){return e.tagName&&"input"===e.tagName.toLowerCase()&&"function"==typeof e.select}(e)&&e.select()):G(T()))},x=function(e){var t=k("setReturnFocus",{params:[e]});return t||!1!==t&&e},R=function(t){var n=t.target,a=t.event,i=t.isBackward,u=void 0!==i&&i;n=n||y(a),D();var c=null;if(F.tabbableGroups.length>0){var s=E(n,a),l=s>=0?F.containerGroups[s]:void 0;if(s<0)c=u?F.tabbableGroups[F.tabbableGroups.length-1].lastTabbableNode:F.tabbableGroups[0].firstTabbableNode;else if(u){var d=F.tabbableGroups.findIndex((function(e){var t=e.firstTabbableNode;return n===t}));if(d<0&&(l.container===n||e(n,N.tabbableOptions)&&!o(n,N.tabbableOptions)&&!l.nextTabbableNode(n,!1))&&(d=s),d>=0){var b=0===d?F.tabbableGroups.length-1:d-1,v=F.tabbableGroups[b];c=r(n)>=0?v.lastTabbableNode:v.lastDomTabbableNode}else f(a)||(c=l.nextTabbableNode(n,!1))}else{var p=F.tabbableGroups.findIndex((function(e){var t=e.lastTabbableNode;return n===t}));if(p<0&&(l.container===n||e(n,N.tabbableOptions)&&!o(n,N.tabbableOptions)&&!l.nextTabbableNode(n))&&(p=s),p>=0){var m=p===F.tabbableGroups.length-1?0:p+1,h=F.tabbableGroups[m];c=r(n)>=0?h.firstTabbableNode:h.firstDomTabbableNode}else f(a)||(c=l.nextTabbableNode(n))}}else c=k("fallbackFocus");return c},I=function(e){var t=y(e);E(t,e)>=0||(m(N.clickOutsideDeactivates,e)?u.deactivate({returnFocus:N.returnFocusOnDeactivate}):m(N.allowOutsideClick,e)||e.preventDefault())},j=function(e){var t=y(e),n=E(t,e)>=0;if(n||t instanceof Document)n&&(F.mostRecentlyFocusedNode=t);else{var o;e.stopImmediatePropagation();var a=!0;if(F.mostRecentlyFocusedNode)if(r(F.mostRecentlyFocusedNode)>0){var i=E(F.mostRecentlyFocusedNode),u=F.containerGroups[i].tabbableNodes;if(u.length>0){var c=u.findIndex((function(e){return e===F.mostRecentlyFocusedNode}));c>=0&&(N.isKeyForward(F.recentNavEvent)?c+1=0&&(o=u[c-1],a=!1))}}else F.containerGroups.some((function(e){return e.tabbableNodes.some((function(e){return r(e)>0}))}))||(a=!1);else a=!1;a&&(o=R({target:F.mostRecentlyFocusedNode,isBackward:N.isKeyBackward(F.recentNavEvent)})),G(o||(F.mostRecentlyFocusedNode||T()))}F.recentNavEvent=void 0},A=function(e){(N.isKeyForward(e)||N.isKeyBackward(e))&&function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];F.recentNavEvent=e;var n=R({event:e,isBackward:t});n&&(f(e)&&e.preventDefault(),G(n))}(e,N.isKeyBackward(e))},L=function(e){var t;"Escape"!==(null==(t=e)?void 0:t.key)&&"Esc"!==(null==t?void 0:t.key)&&27!==(null==t?void 0:t.keyCode)||!1===m(N.escapeDeactivates,e)||(e.preventDefault(),u.deactivate())},S=function(e){var t=y(e);E(t,e)>=0||m(N.clickOutsideDeactivates,e)||m(N.allowOutsideClick,e)||(e.preventDefault(),e.stopImmediatePropagation())},B=function(){if(F.active)return l(g,u),F.delayInitialFocusTimer=N.delayInitialFocus?p((function(){G(T())})):G(T()),w.addEventListener("focusin",j,!0),w.addEventListener("mousedown",I,{capture:!0,passive:!1}),w.addEventListener("touchstart",I,{capture:!0,passive:!1}),w.addEventListener("click",S,{capture:!0,passive:!1}),w.addEventListener("keydown",A,{capture:!0,passive:!1}),w.addEventListener("keydown",L),u},C=function(){if(F.active)return w.removeEventListener("focusin",j,!0),w.removeEventListener("mousedown",I,!0),w.removeEventListener("touchstart",I,!0),w.removeEventListener("click",S,!0),w.removeEventListener("keydown",A,!0),w.removeEventListener("keydown",L),u},K="undefined"!=typeof window&&"MutationObserver"in window?new MutationObserver((function(e){e.some((function(e){return Array.from(e.removedNodes).some((function(e){return e===F.mostRecentlyFocusedNode}))}))&&G(T())})):void 0,M=function(){K&&(K.disconnect(),F.active&&!F.paused&&F.containers.map((function(e){K.observe(e,{subtree:!0,childList:!0})})))};return(u={get active(){return F.active},get paused(){return F.paused},activate:function(e){if(F.active)return this;var t=O(e,"onActivate"),n=O(e,"onPostActivate"),o=O(e,"checkCanFocusTrap");o||D(),F.active=!0,F.paused=!1,F.nodeFocusedBeforeActivation=w.activeElement,null==t||t();var r=function(){o&&D(),B(),M(),null==n||n()};return o?(o(F.containers.concat()).then(r,r),this):(r(),this)},deactivate:function(e){if(!F.active)return this;var t=c({onDeactivate:N.onDeactivate,onPostDeactivate:N.onPostDeactivate,checkCanReturnFocus:N.checkCanReturnFocus},e);clearTimeout(F.delayInitialFocusTimer),F.delayInitialFocusTimer=void 0,C(),F.active=!1,F.paused=!1,M(),d(g,u);var n=O(t,"onDeactivate"),o=O(t,"onPostDeactivate"),r=O(t,"checkCanReturnFocus"),a=O(t,"returnFocus","returnFocusOnDeactivate");null==n||n();var i=function(){p((function(){a&&G(x(F.nodeFocusedBeforeActivation)),null==o||o()}))};return a&&r?(r(x(F.nodeFocusedBeforeActivation)).then(i,i),this):(i(),this)},pause:function(e){if(F.paused||!F.active)return this;var t=O(e,"onPause"),n=O(e,"onPostPause");return F.paused=!0,null==t||t(),C(),M(),null==n||n(),this},unpause:function(e){if(!F.paused||!F.active)return this;var t=O(e,"onUnpause"),n=O(e,"onPostUnpause");return F.paused=!1,null==t||t(),D(),B(),M(),null==n||n(),this},updateContainerElements:function(e){var t=[].concat(e).filter(Boolean);return F.containers=t.map((function(e){return"string"==typeof e?w.querySelector(e):e})),F.active&&D(),M(),this}}).updateContainerElements(a),u};export{w as createFocusTrap}; //# sourceMappingURL=focus-trap.esm.min.js.map