9 lines
17 KiB
JavaScript
9 lines
17 KiB
JavaScript
/*!
|
|
* focus-trap 8.1.0
|
|
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
|
|
*/
|
|
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tabbable")):"function"==typeof define&&define.amd?define(["exports","tabbable"],t):(e="undefined"!=typeof globalThis?globalThis:e||self,function(){var n=e.focusTrap,r=e.focusTrap={};t(r,e.tabbable),r.noConflict=function(){return e.focusTrap=n,r}}())}(this,(function(e,t){"use strict";function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function r(e,t,n,r,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void n(e)}u.done?t(c):Promise.resolve(c).then(r,o)}function o(e){return function(){var t=this,n=arguments;return new Promise((function(o,a){var i=e.apply(t,n);function u(e){r(i,o,a,u,c,"next",e)}function c(e){r(i,o,a,u,c,"throw",e)}u(void 0)}))}}function a(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=f(e))||t){n&&(e=n);var r=0,o=function(){};return{s:o,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,i=!0,u=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return i=e.done,e},e:function(e){u=!0,a=e},f:function(){try{i||null==n.return||n.return()}finally{if(u)throw a}}}}function i(e,t,n){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t);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:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function u(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 c(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?u(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):u(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function s(){
|
|
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
|
var e,t,n="function"==typeof Symbol?Symbol:{},r=n.iterator||"@@iterator",o=n.toStringTag||"@@toStringTag";function a(n,r,o,a){var c=r&&r.prototype instanceof u?r:u,s=Object.create(c.prototype);return l(s,"_invoke",function(n,r,o){var a,u,c,s=0,l=o||[],d=!1,f={p:0,n:0,v:e,a:b,f:b.bind(e,4),d:function(t,n){return a=t,u=0,c=e,f.n=n,i}};function b(n,r){for(u=n,c=r,t=0;!d&&s&&!o&&t<l.length;t++){var o,a=l[t],b=f.p,v=a[2];n>3?(o=v===r)&&(c=a[(u=a[4])?5:(u=3,3)],a[4]=a[5]=e):a[0]<=b&&((o=n<2&&b<a[1])?(u=0,f.v=r,f.n=a[1]):b<v&&(o=n<3||a[0]>r||r>v)&&(a[4]=n,a[5]=r,f.n=v,u=0))}if(o||n>1)return i;throw d=!0,r}return function(o,l,v){if(s>1)throw TypeError("Generator is already running");for(d&&1===l&&b(l,v),u=l,c=v;(t=u<2?e:c)||!d;){a||(u?u<3?(u>1&&(f.n=-1),b(u,c)):f.n=c:f.v=c);try{if(s=2,a){if(u||(o="next"),t=a[o]){if(!(t=t.call(a,c)))throw TypeError("iterator result is not an object");if(!t.done)return t;c=t.value,u<2&&(u=0)}else 1===u&&(t=a.return)&&t.call(a),u<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),u=1);a=e}else if((t=(d=f.n<0)?c:n.call(r,f))!==i)break}catch(t){a=e,u=1,c=t}finally{s=1}}return{value:t,done:d}}}(n,o,a),!0),s}var i={};function u(){}function c(){}function d(){}t=Object.getPrototypeOf;var f=[][r]?t(t([][r]())):(l(t={},r,(function(){return this})),t),b=d.prototype=u.prototype=Object.create(f);function v(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,d):(e.__proto__=d,l(e,o,"GeneratorFunction")),e.prototype=Object.create(b),e}return c.prototype=d,l(b,"constructor",d),l(d,"constructor",c),c.displayName="GeneratorFunction",l(d,o,"GeneratorFunction"),l(b),l(b,o,"Generator"),l(b,r,(function(){return this})),l(b,"toString",(function(){return"[object Generator]"})),(s=function(){return{w:a,m:v}})()}function l(e,t,n,r){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}l=function(e,t,n,r){function a(t,n){l(e,t,(function(e){return this._invoke(t,n,e)}))}t?o?o(e,t,{value:n,enumerable:!r,configurable:!r,writable:!r}):e[t]=n:(a("next",0),a("throw",1),a("return",2))},l(e,t,n,r)}function d(e){return function(e){if(Array.isArray(e))return n(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||f(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function f(e,t){if(e){if("string"==typeof e)return n(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}}var b={getActiveTrap:function(e){return(null==e?void 0:e.length)>0?e[e.length-1]:null},activateTrap:function(e,t){t!==b.getActiveTrap(e)&&b.pauseTrap(e);var n=e.indexOf(t);-1===n||e.splice(n,1),e.push(t)},deactivateTrap:function(e,t){var n=e.indexOf(t);-1!==n&&e.splice(n,1),b.unpauseTrap(e)},pauseTrap:function(e){var t=b.getActiveTrap(e);null==t||t._setPausedState(!0)},unpauseTrap:function(e){var t=b.getActiveTrap(e);t&&!t._isManuallyPaused()&&t._setPausedState(!1)}},v=function(e){return"Tab"===(null==e?void 0:e.key)||9===(null==e?void 0:e.keyCode)},p=function(e){return v(e)&&!e.shiftKey},y=function(e){return v(e)&&e.shiftKey},h=function(e){return setTimeout(e,0)},m=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},w=function(e){return e.target.shadowRoot&&"function"==typeof e.composedPath?e.composedPath()[0]:e.target},g=[];e.createFocusTrap=function(e,n){var r,i=(null==n?void 0:n.document)||document,u=(null==n?void 0:n.trapStack)||g,l=c({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0,isolateSubtrees:!1,isKeyForward:p,isKeyBackward:y},n),f={containers:[],containerGroups:[],tabbableGroups:[],adjacentElements:new Set,alreadySilent:new Set,nodeFocusedBeforeActivation:null,mostRecentlyFocusedNode:null,active:!1,paused:!1,manuallyPaused:!1,delayInitialFocusTimer:void 0,recentNavEvent:void 0},T=function(e,t,n){return e&&void 0!==e[t]?e[t]:l[n||t]},S=function(e,t){var n="function"==typeof(null==t?void 0:t.composedPath)?t.composedPath():void 0;return f.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}))}))},O=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.hasFallback,r=void 0!==n&&n,o=t.params,a=void 0===o?[]:o,u=l[e];if("function"==typeof u&&(u=u.apply(void 0,d(a))),!0===u&&(u=void 0),!u){if(void 0===u||!1===u)return u;throw new Error("`".concat(e,"` was specified but was not a node, or did not return a node"))}var c=u;if("string"==typeof u){try{c=i.querySelector(u)}catch(t){throw new Error("`".concat(e,'` appears to be an invalid selector; error="').concat(t.message,'"'))}if(!c&&!r)throw new Error("`".concat(e,"` as selector refers to no known node"))}return c},F=function(){var e=O("initialFocus",{hasFallback:!0});if(!1===e)return!1;if(void 0===e||e&&!t.isFocusable(e,l.tabbableOptions))if(S(i.activeElement)>=0)e=i.activeElement;else{var n=f.tabbableGroups[0];e=n&&n.firstTabbableNode||O("fallbackFocus")}else null===e&&(e=O("fallbackFocus"));if(!e)throw new Error("Your focus-trap needs to have at least one focusable element");return e},N=function(){if(f.containerGroups=f.containers.map((function(e){var n=t.tabbable(e,l.tabbableOptions),r=t.focusable(e,l.tabbableOptions),o=n.length>0?n[0]:void 0,a=n.length>0?n[n.length-1]:void 0,i=r.find((function(e){return t.isTabbable(e)})),u=r.slice().reverse().find((function(e){return t.isTabbable(e)})),c=!!n.find((function(e){return t.getTabIndex(e)>0}));return{container:e,tabbableNodes:n,focusableNodes:r,posTabIndexesFound:c,firstTabbableNode:o,lastTabbableNode:a,firstDomTabbableNode:i,lastDomTabbableNode:u,nextTabbableNode:function(e){var o=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],a=n.indexOf(e);return a<0?o?r.slice(r.indexOf(e)+1).find((function(e){return t.isTabbable(e)})):r.slice(0,r.indexOf(e)).reverse().find((function(e){return t.isTabbable(e)})):n[a+(o?1:-1)]}}})),f.tabbableGroups=f.containerGroups.filter((function(e){return e.tabbableNodes.length>0})),f.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");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},E=function(e){!1!==e&&e!==P(document)&&(e&&e.focus?(e.focus({preventScroll:!!l.preventScroll}),f.mostRecentlyFocusedNode=e,function(e){return e.tagName&&"input"===e.tagName.toLowerCase()&&"function"==typeof e.select}(e)&&e.select()):E(F()))},k=function(e){var t=O("setReturnFocus",{params:[e]});return t||!1!==t&&e},I=function(e){var n=e.target,r=e.event,o=e.isBackward,a=void 0!==o&&o;n=n||w(r),N();var i=null;if(f.tabbableGroups.length>0){var u=S(n,r),c=u>=0?f.containerGroups[u]:void 0;if(u<0)i=a?f.tabbableGroups[f.tabbableGroups.length-1].lastTabbableNode:f.tabbableGroups[0].firstTabbableNode;else if(a){var s=f.tabbableGroups.findIndex((function(e){var t=e.firstTabbableNode;return n===t}));if(s<0&&(c.container===n||t.isFocusable(n,l.tabbableOptions)&&!t.isTabbable(n,l.tabbableOptions)&&!c.nextTabbableNode(n,!1))&&(s=u),s>=0){var d=0===s?f.tabbableGroups.length-1:s-1,b=f.tabbableGroups[d];i=t.getTabIndex(n)>=0?b.lastTabbableNode:b.lastDomTabbableNode}else v(r)||(i=c.nextTabbableNode(n,!1))}else{var p=f.tabbableGroups.findIndex((function(e){var t=e.lastTabbableNode;return n===t}));if(p<0&&(c.container===n||t.isFocusable(n,l.tabbableOptions)&&!t.isTabbable(n,l.tabbableOptions)&&!c.nextTabbableNode(n))&&(p=u),p>=0){var y=p===f.tabbableGroups.length-1?0:p+1,h=f.tabbableGroups[y];i=t.getTabIndex(n)>=0?h.firstTabbableNode:h.firstDomTabbableNode}else v(r)||(i=c.nextTabbableNode(n))}}else i=O("fallbackFocus");return i},j=function(e){var t=w(e);S(t,e)>=0||(m(l.clickOutsideDeactivates,e)?r.deactivate({returnFocus:l.returnFocusOnDeactivate}):m(l.allowOutsideClick,e)||e.preventDefault())},x=function(e){var n=w(e),r=S(n,e)>=0;if(r||n instanceof Document)r&&(f.mostRecentlyFocusedNode=n);else{var o;e.stopImmediatePropagation();var a=!0;if(f.mostRecentlyFocusedNode)if(t.getTabIndex(f.mostRecentlyFocusedNode)>0){var i=S(f.mostRecentlyFocusedNode),u=f.containerGroups[i].tabbableNodes;if(u.length>0){var c=u.findIndex((function(e){return e===f.mostRecentlyFocusedNode}));c>=0&&(l.isKeyForward(f.recentNavEvent)?c+1<u.length&&(o=u[c+1],a=!1):c-1>=0&&(o=u[c-1],a=!1))}}else f.containerGroups.some((function(e){return e.tabbableNodes.some((function(e){return t.getTabIndex(e)>0}))}))||(a=!1);else a=!1;a&&(o=I({target:f.mostRecentlyFocusedNode,isBackward:l.isKeyBackward(f.recentNavEvent)})),E(o||(f.mostRecentlyFocusedNode||F()))}f.recentNavEvent=void 0},A=function(e){(l.isKeyForward(e)||l.isKeyBackward(e))&&function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];f.recentNavEvent=e;var n=I({event:e,isBackward:t});n&&(v(e)&&e.preventDefault(),E(n))}(e,l.isKeyBackward(e))},G=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(l.escapeDeactivates,e)||(e.preventDefault(),r.deactivate())},D=function(e){var t=w(e);S(t,e)>=0||m(l.clickOutsideDeactivates,e)||m(l.allowOutsideClick,e)||(e.preventDefault(),e.stopImmediatePropagation())},R=function(){return f.active?(b.activateTrap(u,r),l.delayInitialFocus?e=new Promise((function(e){f.delayInitialFocusTimer=h((function(){E(F()),e()}))})):(e=Promise.resolve(),E(F())),i.addEventListener("focusin",x,!0),i.addEventListener("mousedown",j,{capture:!0,passive:!1}),i.addEventListener("touchstart",j,{capture:!0,passive:!1}),i.addEventListener("click",D,{capture:!0,passive:!1}),i.addEventListener("keydown",A,{capture:!0,passive:!1}),i.addEventListener("keydown",G),e):Promise.resolve();var e},_=function(){if(f.active)return i.removeEventListener("focusin",x,!0),i.removeEventListener("mousedown",j,!0),i.removeEventListener("touchstart",j,!0),i.removeEventListener("click",D,!0),i.removeEventListener("keydown",A,!0),i.removeEventListener("keydown",G),r},L="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}))}))&&E(F())})):void 0,C=function(){L&&(L.disconnect(),f.active&&!f.paused&&f.containers.map((function(e){L.observe(e,{subtree:!0,childList:!0})})))};return r={get active(){return f.active},get paused(){return f.paused},activate:function(e){if(f.active)return this;var t,n=T(e,"onActivate"),a=T(e,"onPostActivate"),c=T(e,"checkCanFocusTrap"),l=b.getActiveTrap(u),d=!1;l&&!l.paused&&(null===(t=l._setSubtreeIsolation)||void 0===t||t.call(l,!1),d=!0);try{c||N(),f.active=!0,f.paused=!1,f.nodeFocusedBeforeActivation=P(i),null==n||n({trap:r});var v=function(){var e=o(s().m((function e(){return s().w((function(e){for(;;)switch(e.n){case 0:return c&&N(),e.n=1,R();case 1:r._setSubtreeIsolation(!0),C(),null==a||a({trap:r});case 2:return e.a(2)}}),e)})));return function(){return e.apply(this,arguments)}}();if(c)return c(f.containers.concat()).then(v,v),this;v()}catch(e){var p;if(l===b.getActiveTrap(u)&&d)null===(p=l._setSubtreeIsolation)||void 0===p||p.call(l,!0);throw e}return this},deactivate:function(e){if(!f.active)return this;var t=c({onDeactivate:l.onDeactivate,onPostDeactivate:l.onPostDeactivate,checkCanReturnFocus:l.checkCanReturnFocus},e);clearTimeout(f.delayInitialFocusTimer),f.delayInitialFocusTimer=void 0,f.paused||r._setSubtreeIsolation(!1),f.alreadySilent.clear(),_(),f.active=!1,f.paused=!1,C(),b.deactivateTrap(u,r);var n=T(t,"onDeactivate"),o=T(t,"onPostDeactivate"),a=T(t,"checkCanReturnFocus"),i=T(t,"returnFocus","returnFocusOnDeactivate");null==n||n({trap:r});var s=function(){h((function(){i&&E(k(f.nodeFocusedBeforeActivation)),null==o||o({trap:r})}))};return i&&a?(a(k(f.nodeFocusedBeforeActivation)).then(s,s),this):(s(),this)},pause:function(e){return f.active?(f.manuallyPaused=!0,this._setPausedState(!0,e)):this},unpause:function(e){return f.active?(f.manuallyPaused=!1,u[u.length-1]!==this?this:this._setPausedState(!1,e)):this},updateContainerElements:function(e){var t=[].concat(e).filter(Boolean);return f.containers=t.map((function(e){return"string"==typeof e?i.querySelector(e):e})),l.isolateSubtrees&&function(e){f.active&&!f.paused&&r._setSubtreeIsolation(!1),f.adjacentElements.clear(),f.alreadySilent.clear();var t,n=new Set,o=new Set,i=a(e);try{for(i.s();!(t=i.n()).done;){var u=t.value;n.add(u);for(var c="undefined"!=typeof ShadowRoot&&u.getRootNode()instanceof ShadowRoot,s=u;s;){n.add(s);var l=s.parentElement,d=[];l?d=l.children:!l&&c&&(d=s.getRootNode().children,l=s.getRootNode().host,c="undefined"!=typeof ShadowRoot&&l.getRootNode()instanceof ShadowRoot);var b,v=a(d);try{for(v.s();!(b=v.n()).done;){var p=b.value;o.add(p)}}catch(e){v.e(e)}finally{v.f()}s=l}}}catch(e){i.e(e)}finally{i.f()}n.forEach((function(e){o.delete(e)})),f.adjacentElements=o}(f.containers),f.active&&(N(),f.paused||r._setSubtreeIsolation(!0)),C(),this}},Object.defineProperties(r,{_isManuallyPaused:{value:function(){return f.manuallyPaused}},_setPausedState:{value:function(e,t){if(f.paused===e)return this;if(f.paused=e,e){var n=T(t,"onPause"),a=T(t,"onPostPause");null==n||n({trap:r}),_(),r._setSubtreeIsolation(!1),C(),null==a||a({trap:r})}else{var i=T(t,"onUnpause"),u=T(t,"onPostUnpause");null==i||i({trap:r});var c=function(){var e=o(s().m((function e(){return s().w((function(e){for(;;)switch(e.n){case 0:return N(),e.n=1,R();case 1:r._setSubtreeIsolation(!0),C(),null==u||u({trap:r});case 2:return e.a(2)}}),e)})));return function(){return e.apply(this,arguments)}}();c()}return this}},_setSubtreeIsolation:{value:function(e){l.isolateSubtrees&&f.adjacentElements.forEach((function(t){var n;if(e)if("aria-hidden"===l.isolateSubtrees)"true"!==t.ariaHidden&&"true"!==(null===(n=t.getAttribute("aria-hidden"))||void 0===n?void 0:n.toLowerCase())||f.alreadySilent.add(t),t.setAttribute("aria-hidden","true");else(t.inert||t.hasAttribute("inert"))&&f.alreadySilent.add(t),t.setAttribute("inert",!0);else if(f.alreadySilent.has(t));else if("aria-hidden"===l.isolateSubtrees)t.removeAttribute("aria-hidden");else t.removeAttribute("inert")}))}}}),r.updateContainerElements(e),r}}));
|
|
//# sourceMappingURL=focus-trap.umd.min.js.map
|