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