doc.yeswiki.pro/node_modules/focus-trap/dist/focus-trap.esm.min.js
2023-05-20 19:38:02 +03:00

6 lines
8.5 KiB
JavaScript

/*!
* focus-trap 7.4.3
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
*/
import{isFocusable as e,tabbable as t,focusable as n,isTabbable as r}from"tabbable";function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t,n){return(t=function(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var u=function(e,t){if(e.length>0){var n=e[e.length-1];n!==t&&n.pause()}var r=e.indexOf(t);-1===r||e.splice(r,1),e.push(t)},c=function(e,t){var n=e.indexOf(t);-1!==n&&e.splice(n,1),e.length>0&&e[e.length-1].unpause()},s=function(e){return"Tab"===e.key||9===e.keyCode},l=function(e){return s(e)&&!e.shiftKey},b=function(e){return s(e)&&e.shiftKey},f=function(e){return setTimeout(e,0)},v=function(e,t){var n=-1;return e.every((function(e,r){return!t(e)||(n=r,!1)})),n},d=function(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];return"function"==typeof e?e.apply(void 0,n):e},p=function(e){return e.target.shadowRoot&&"function"==typeof e.composedPath?e.composedPath()[0]:e.target},m=[],y=function(o,i){var y,h=(null==i?void 0:i.document)||document,w=(null==i?void 0:i.trapStack)||m,g=a({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0,isKeyForward:l,isKeyBackward:b},i),O={containers:[],containerGroups:[],tabbableGroups:[],nodeFocusedBeforeActivation:null,mostRecentlyFocusedNode:null,active:!1,paused:!1,delayInitialFocusTimer:void 0},F=function(e,t,n){return e&&void 0!==e[t]?e[t]:g[n||t]},N=function(e,t){var n="function"==typeof(null==t?void 0:t.composedPath)?t.composedPath():void 0;return O.containerGroups.findIndex((function(t){var r=t.container,o=t.tabbableNodes;return r.contains(e)||(null==n?void 0:n.includes(r))||o.find((function(t){return t===e}))}))},k=function(e){var t=g[e];if("function"==typeof t){for(var n=arguments.length,r=new Array(n>1?n-1:0),o=1;o<n;o++)r[o-1]=arguments[o];t=t.apply(void 0,r)}if(!0===t&&(t=void 0),!t){if(void 0===t||!1===t)return t;throw new Error("`".concat(e,"` was specified but was not a node, or did not return a node"))}var a=t;if("string"==typeof t&&!(a=h.querySelector(t)))throw new Error("`".concat(e,"` as selector refers to no known node"));return a},E=function(){var t=k("initialFocus");if(!1===t)return!1;if(void 0===t||!e(t,g.tabbableOptions))if(N(h.activeElement)>=0)t=h.activeElement;else{var n=O.tabbableGroups[0];t=n&&n.firstTabbableNode||k("fallbackFocus")}if(!t)throw new Error("Your focus-trap needs to have at least one focusable element");return t},P=function(){if(O.containerGroups=O.containers.map((function(e){var o=t(e,g.tabbableOptions),a=n(e,g.tabbableOptions);return{container:e,tabbableNodes:o,focusableNodes:a,firstTabbableNode:o.length>0?o[0]:null,lastTabbableNode:o.length>0?o[o.length-1]:null,nextTabbableNode:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=a.findIndex((function(t){return t===e}));if(!(n<0))return t?a.slice(n+1).find((function(e){return r(e,g.tabbableOptions)})):a.slice(0,n).reverse().find((function(e){return r(e,g.tabbableOptions)}))}}})),O.tabbableGroups=O.containerGroups.filter((function(e){return e.tabbableNodes.length>0})),O.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")},T=function e(t){!1!==t&&t!==h.activeElement&&(t&&t.focus?(t.focus({preventScroll:!!g.preventScroll}),O.mostRecentlyFocusedNode=t,function(e){return e.tagName&&"input"===e.tagName.toLowerCase()&&"function"==typeof e.select}(t)&&t.select()):e(E()))},D=function(e){var t=k("setReturnFocus",e);return t||!1!==t&&e},G=function(e){var t=p(e);N(t,e)>=0||(d(g.clickOutsideDeactivates,e)?y.deactivate({returnFocus:g.returnFocusOnDeactivate}):d(g.allowOutsideClick,e)||e.preventDefault())},j=function(e){var t=p(e),n=N(t,e)>=0;n||t instanceof Document?n&&(O.mostRecentlyFocusedNode=t):(e.stopImmediatePropagation(),T(O.mostRecentlyFocusedNode||E()))},L=function(t){if(!(n=t,"Escape"!==n.key&&"Esc"!==n.key&&27!==n.keyCode||!1===d(g.escapeDeactivates,t)))return t.preventDefault(),void y.deactivate();var n;(g.isKeyForward(t)||g.isKeyBackward(t))&&function(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],o=p(t);P();var a=null;if(O.tabbableGroups.length>0){var i=N(o,t),u=i>=0?O.containerGroups[i]:void 0;if(i<0)a=n?O.tabbableGroups[O.tabbableGroups.length-1].lastTabbableNode:O.tabbableGroups[0].firstTabbableNode;else if(n){var c=v(O.tabbableGroups,(function(e){var t=e.firstTabbableNode;return o===t}));if(c<0&&(u.container===o||e(o,g.tabbableOptions)&&!r(o,g.tabbableOptions)&&!u.nextTabbableNode(o,!1))&&(c=i),c>=0){var l=0===c?O.tabbableGroups.length-1:c-1;a=O.tabbableGroups[l].lastTabbableNode}else s(t)||(a=u.nextTabbableNode(o,!1))}else{var b=v(O.tabbableGroups,(function(e){var t=e.lastTabbableNode;return o===t}));if(b<0&&(u.container===o||e(o,g.tabbableOptions)&&!r(o,g.tabbableOptions)&&!u.nextTabbableNode(o))&&(b=i),b>=0){var f=b===O.tabbableGroups.length-1?0:b+1;a=O.tabbableGroups[f].firstTabbableNode}else s(t)||(a=u.nextTabbableNode(o))}}else a=k("fallbackFocus");a&&(s(t)&&t.preventDefault(),T(a))}(t,g.isKeyBackward(t))},C=function(e){var t=p(e);N(t,e)>=0||d(g.clickOutsideDeactivates,e)||d(g.allowOutsideClick,e)||(e.preventDefault(),e.stopImmediatePropagation())},x=function(){if(O.active)return u(w,y),O.delayInitialFocusTimer=g.delayInitialFocus?f((function(){T(E())})):T(E()),h.addEventListener("focusin",j,!0),h.addEventListener("mousedown",G,{capture:!0,passive:!1}),h.addEventListener("touchstart",G,{capture:!0,passive:!1}),h.addEventListener("click",C,{capture:!0,passive:!1}),h.addEventListener("keydown",L,{capture:!0,passive:!1}),y},I=function(){if(O.active)return h.removeEventListener("focusin",j,!0),h.removeEventListener("mousedown",G,!0),h.removeEventListener("touchstart",G,!0),h.removeEventListener("click",C,!0),h.removeEventListener("keydown",L,!0),y},R="undefined"!=typeof window&&"MutationObserver"in window?new MutationObserver((function(e){e.some((function(e){return Array.from(e.removedNodes).some((function(e){return e===O.mostRecentlyFocusedNode}))}))&&T(E())})):void 0,S=function(){R&&(R.disconnect(),O.active&&!O.paused&&O.containers.map((function(e){R.observe(e,{subtree:!0,childList:!0})})))};return(y={get active(){return O.active},get paused(){return O.paused},activate:function(e){if(O.active)return this;var t=F(e,"onActivate"),n=F(e,"onPostActivate"),r=F(e,"checkCanFocusTrap");r||P(),O.active=!0,O.paused=!1,O.nodeFocusedBeforeActivation=h.activeElement,null==t||t();var o=function(){r&&P(),x(),S(),null==n||n()};return r?(r(O.containers.concat()).then(o,o),this):(o(),this)},deactivate:function(e){if(!O.active)return this;var t=a({onDeactivate:g.onDeactivate,onPostDeactivate:g.onPostDeactivate,checkCanReturnFocus:g.checkCanReturnFocus},e);clearTimeout(O.delayInitialFocusTimer),O.delayInitialFocusTimer=void 0,I(),O.active=!1,O.paused=!1,S(),c(w,y);var n=F(t,"onDeactivate"),r=F(t,"onPostDeactivate"),o=F(t,"checkCanReturnFocus"),i=F(t,"returnFocus","returnFocusOnDeactivate");null==n||n();var u=function(){f((function(){i&&T(D(O.nodeFocusedBeforeActivation)),null==r||r()}))};return i&&o?(o(D(O.nodeFocusedBeforeActivation)).then(u,u),this):(u(),this)},pause:function(e){if(O.paused||!O.active)return this;var t=F(e,"onPause"),n=F(e,"onPostPause");return O.paused=!0,null==t||t(),I(),S(),null==n||n(),this},unpause:function(e){if(!O.paused||!O.active)return this;var t=F(e,"onUnpause"),n=F(e,"onPostUnpause");return O.paused=!1,null==t||t(),P(),x(),S(),null==n||n(),this},updateContainerElements:function(e){var t=[].concat(e).filter(Boolean);return O.containers=t.map((function(e){return"string"==typeof e?h.querySelector(e):e})),O.active&&P(),S(),this}}).updateContainerElements(o),y};export{y as createFocusTrap};
//# sourceMappingURL=focus-trap.esm.min.js.map