mirror of
https://github.com/mivodev/mivodev.github.io.git
synced 2026-01-26 21:41:53 +07:00
7 lines
14 KiB
JavaScript
7 lines
14 KiB
JavaScript
/*!
|
|
* focus-trap 7.8.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,a=e.focusTrap={};t(a,e.tabbable),a.noConflict=function(){return e.focusTrap=n,a}}())}(this,(function(e,t){"use strict";function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,a=Array(t);n<t;n++)a[n]=e[n];return a}function a(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=s(e))||t){n&&(e=n);var a=0,o=function(){};return{s:o,n:function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}},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 r,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,r=e},f:function(){try{i||null==n.return||n.return()}finally{if(u)throw r}}}}function o(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 a=n.call(e,t);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:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function r(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 i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){o(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function u(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)||s(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 s(e,t){if(e){if("string"==typeof e)return n(e,t);var a={}.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?n(e,t):void 0}}var c={getActiveTrap:function(e){return(null==e?void 0:e.length)>0?e[e.length-1]:null},activateTrap:function(e,t){t!==c.getActiveTrap(e)&&c.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),c.unpauseTrap(e)},pauseTrap:function(e){var t=c.getActiveTrap(e);null==t||t._setPausedState(!0)},unpauseTrap:function(e){var t=c.getActiveTrap(e);t&&!t._isManuallyPaused()&&t._setPausedState(!1)}},l=function(e){return"Tab"===(null==e?void 0:e.key)||9===(null==e?void 0:e.keyCode)},d=function(e){return l(e)&&!e.shiftKey},f=function(e){return l(e)&&e.shiftKey},b=function(e){return setTimeout(e,0)},v=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},p=function(e){return e.target.shadowRoot&&"function"==typeof e.composedPath?e.composedPath()[0]:e.target},y=[];e.createFocusTrap=function(e,n){var o,r=(null==n?void 0:n.document)||document,s=(null==n?void 0:n.trapStack)||y,h=i({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0,isolateSubtrees:!1,isKeyForward:d,isKeyBackward:f},n),m={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},w=function(e,t,n){return e&&void 0!==e[t]?e[t]:h[n||t]},g=function(e,t){var n="function"==typeof(null==t?void 0:t.composedPath)?t.composedPath():void 0;return m.containerGroups.findIndex((function(t){var a=t.container,o=t.tabbableNodes;return a.contains(e)||(null==n?void 0:n.includes(a))||o.find((function(t){return t===e}))}))},T=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.hasFallback,a=void 0!==n&&n,o=t.params,i=void 0===o?[]:o,s=h[e];if("function"==typeof s&&(s=s.apply(void 0,u(i))),!0===s&&(s=void 0),!s){if(void 0===s||!1===s)return s;throw new Error("`".concat(e,"` was specified but was not a node, or did not return a node"))}var c=s;if("string"==typeof s){try{c=r.querySelector(s)}catch(t){throw new Error("`".concat(e,'` appears to be an invalid selector; error="').concat(t.message,'"'))}if(!c&&!a)throw new Error("`".concat(e,"` as selector refers to no known node"))}return c},S=function(){var e=T("initialFocus",{hasFallback:!0});if(!1===e)return!1;if(void 0===e||e&&!t.isFocusable(e,h.tabbableOptions))if(g(r.activeElement)>=0)e=r.activeElement;else{var n=m.tabbableGroups[0];e=n&&n.firstTabbableNode||T("fallbackFocus")}else null===e&&(e=T("fallbackFocus"));if(!e)throw new Error("Your focus-trap needs to have at least one focusable element");return e},N=function(){if(m.containerGroups=m.containers.map((function(e){var n=t.tabbable(e,h.tabbableOptions),a=t.focusable(e,h.tabbableOptions),o=n.length>0?n[0]:void 0,r=n.length>0?n[n.length-1]:void 0,i=a.find((function(e){return t.isTabbable(e)})),u=a.slice().reverse().find((function(e){return t.isTabbable(e)})),s=!!n.find((function(e){return t.getTabIndex(e)>0}));return{container:e,tabbableNodes:n,focusableNodes:a,posTabIndexesFound:s,firstTabbableNode:o,lastTabbableNode:r,firstDomTabbableNode:i,lastDomTabbableNode:u,nextTabbableNode:function(e){var o=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r=n.indexOf(e);return r<0?o?a.slice(a.indexOf(e)+1).find((function(e){return t.isTabbable(e)})):a.slice(0,a.indexOf(e)).reverse().find((function(e){return t.isTabbable(e)})):n[r+(o?1:-1)]}}})),m.tabbableGroups=m.containerGroups.filter((function(e){return e.tabbableNodes.length>0})),m.tabbableGroups.length<=0&&!T("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(m.containerGroups.find((function(e){return e.posTabIndexesFound}))&&m.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.")},F=function(e){var t=e.activeElement;if(t)return t.shadowRoot&&null!==t.shadowRoot.activeElement?F(t.shadowRoot):t},E=function(e){!1!==e&&e!==F(document)&&(e&&e.focus?(e.focus({preventScroll:!!h.preventScroll}),m.mostRecentlyFocusedNode=e,function(e){return e.tagName&&"input"===e.tagName.toLowerCase()&&"function"==typeof e.select}(e)&&e.select()):E(S()))},O=function(e){var t=T("setReturnFocus",{params:[e]});return t||!1!==t&&e},P=function(e){var n=e.target,a=e.event,o=e.isBackward,r=void 0!==o&&o;n=n||p(a),N();var i=null;if(m.tabbableGroups.length>0){var u=g(n,a),s=u>=0?m.containerGroups[u]:void 0;if(u<0)i=r?m.tabbableGroups[m.tabbableGroups.length-1].lastTabbableNode:m.tabbableGroups[0].firstTabbableNode;else if(r){var c=m.tabbableGroups.findIndex((function(e){var t=e.firstTabbableNode;return n===t}));if(c<0&&(s.container===n||t.isFocusable(n,h.tabbableOptions)&&!t.isTabbable(n,h.tabbableOptions)&&!s.nextTabbableNode(n,!1))&&(c=u),c>=0){var d=0===c?m.tabbableGroups.length-1:c-1,f=m.tabbableGroups[d];i=t.getTabIndex(n)>=0?f.lastTabbableNode:f.lastDomTabbableNode}else l(a)||(i=s.nextTabbableNode(n,!1))}else{var b=m.tabbableGroups.findIndex((function(e){var t=e.lastTabbableNode;return n===t}));if(b<0&&(s.container===n||t.isFocusable(n,h.tabbableOptions)&&!t.isTabbable(n,h.tabbableOptions)&&!s.nextTabbableNode(n))&&(b=u),b>=0){var v=b===m.tabbableGroups.length-1?0:b+1,y=m.tabbableGroups[v];i=t.getTabIndex(n)>=0?y.firstTabbableNode:y.firstDomTabbableNode}else l(a)||(i=s.nextTabbableNode(n))}}else i=T("fallbackFocus");return i},k=function(e){var t=p(e);g(t,e)>=0||(v(h.clickOutsideDeactivates,e)?o.deactivate({returnFocus:h.returnFocusOnDeactivate}):v(h.allowOutsideClick,e)||e.preventDefault())},I=function(e){var n=p(e),a=g(n,e)>=0;if(a||n instanceof Document)a&&(m.mostRecentlyFocusedNode=n);else{var o;e.stopImmediatePropagation();var r=!0;if(m.mostRecentlyFocusedNode)if(t.getTabIndex(m.mostRecentlyFocusedNode)>0){var i=g(m.mostRecentlyFocusedNode),u=m.containerGroups[i].tabbableNodes;if(u.length>0){var s=u.findIndex((function(e){return e===m.mostRecentlyFocusedNode}));s>=0&&(h.isKeyForward(m.recentNavEvent)?s+1<u.length&&(o=u[s+1],r=!1):s-1>=0&&(o=u[s-1],r=!1))}}else m.containerGroups.some((function(e){return e.tabbableNodes.some((function(e){return t.getTabIndex(e)>0}))}))||(r=!1);else r=!1;r&&(o=P({target:m.mostRecentlyFocusedNode,isBackward:h.isKeyBackward(m.recentNavEvent)})),E(o||(m.mostRecentlyFocusedNode||S()))}m.recentNavEvent=void 0},A=function(e){(h.isKeyForward(e)||h.isKeyBackward(e))&&function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];m.recentNavEvent=e;var n=P({event:e,isBackward:t});n&&(l(e)&&e.preventDefault(),E(n))}(e,h.isKeyBackward(e))},x=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===v(h.escapeDeactivates,e)||(e.preventDefault(),o.deactivate())},D=function(e){var t=p(e);g(t,e)>=0||v(h.clickOutsideDeactivates,e)||v(h.allowOutsideClick,e)||(e.preventDefault(),e.stopImmediatePropagation())},R=function(){if(m.active)return c.activateTrap(s,o),m.delayInitialFocusTimer=h.delayInitialFocus?b((function(){E(S())})):E(S()),r.addEventListener("focusin",I,!0),r.addEventListener("mousedown",k,{capture:!0,passive:!1}),r.addEventListener("touchstart",k,{capture:!0,passive:!1}),r.addEventListener("click",D,{capture:!0,passive:!1}),r.addEventListener("keydown",A,{capture:!0,passive:!1}),r.addEventListener("keydown",x),o},j=function(){if(m.active)return r.removeEventListener("focusin",I,!0),r.removeEventListener("mousedown",k,!0),r.removeEventListener("touchstart",k,!0),r.removeEventListener("click",D,!0),r.removeEventListener("keydown",A,!0),r.removeEventListener("keydown",x),o},G="undefined"!=typeof window&&"MutationObserver"in window?new MutationObserver((function(e){e.some((function(e){return Array.from(e.removedNodes).some((function(e){return e===m.mostRecentlyFocusedNode}))}))&&E(S())})):void 0,_=function(){G&&(G.disconnect(),m.active&&!m.paused&&m.containers.map((function(e){G.observe(e,{subtree:!0,childList:!0})})))};return o={get active(){return m.active},get paused(){return m.paused},activate:function(e){if(m.active)return this;var t,n=w(e,"onActivate"),a=w(e,"onPostActivate"),i=w(e,"checkCanFocusTrap"),u=c.getActiveTrap(s),l=!1;u&&!u.paused&&(null===(t=u._setSubtreeIsolation)||void 0===t||t.call(u,!1),l=!0);try{i||N(),m.active=!0,m.paused=!1,m.nodeFocusedBeforeActivation=F(r),null==n||n();var d=function(){i&&N(),R(),_(),h.isolateSubtrees&&o._setSubtreeIsolation(!0),null==a||a()};if(i)return i(m.containers.concat()).then(d,d),this;d()}catch(e){var f;if(u===c.getActiveTrap(s)&&l)null===(f=u._setSubtreeIsolation)||void 0===f||f.call(u,!0);throw e}return this},deactivate:function(e){if(!m.active)return this;var t=i({onDeactivate:h.onDeactivate,onPostDeactivate:h.onPostDeactivate,checkCanReturnFocus:h.checkCanReturnFocus},e);clearTimeout(m.delayInitialFocusTimer),m.delayInitialFocusTimer=void 0,m.paused||o._setSubtreeIsolation(!1),m.alreadySilent.clear(),j(),m.active=!1,m.paused=!1,_(),c.deactivateTrap(s,o);var n=w(t,"onDeactivate"),a=w(t,"onPostDeactivate"),r=w(t,"checkCanReturnFocus"),u=w(t,"returnFocus","returnFocusOnDeactivate");null==n||n();var l=function(){b((function(){u&&E(O(m.nodeFocusedBeforeActivation)),null==a||a()}))};return u&&r?(r(O(m.nodeFocusedBeforeActivation)).then(l,l),this):(l(),this)},pause:function(e){return m.active?(m.manuallyPaused=!0,this._setPausedState(!0,e)):this},unpause:function(e){return m.active?(m.manuallyPaused=!1,s[s.length-1]!==this?this:this._setPausedState(!1,e)):this},updateContainerElements:function(e){var t=[].concat(e).filter(Boolean);return m.containers=t.map((function(e){return"string"==typeof e?r.querySelector(e):e})),h.isolateSubtrees&&function(e){m.active&&!m.paused&&o._setSubtreeIsolation(!1),m.adjacentElements.clear(),m.alreadySilent.clear();var t,n=new Set,r=new Set,i=a(e);try{for(i.s();!(t=i.n()).done;){var u=t.value;n.add(u);for(var s="undefined"!=typeof ShadowRoot&&u.getRootNode()instanceof ShadowRoot,c=u;c;){n.add(c);var l=c.parentElement,d=[];l?d=l.children:!l&&s&&(d=c.getRootNode().children,l=c.getRootNode().host,s="undefined"!=typeof ShadowRoot&&l.getRootNode()instanceof ShadowRoot);var f,b=a(d);try{for(b.s();!(f=b.n()).done;){var v=f.value;r.add(v)}}catch(e){b.e(e)}finally{b.f()}c=l}}}catch(e){i.e(e)}finally{i.f()}n.forEach((function(e){r.delete(e)})),m.adjacentElements=r}(m.containers),m.active&&(N(),h.isolateSubtrees&&!m.paused&&o._setSubtreeIsolation(!0)),_(),this}},Object.defineProperties(o,{_isManuallyPaused:{value:function(){return m.manuallyPaused}},_setPausedState:{value:function(e,t){if(m.paused===e)return this;if(m.paused=e,e){var n=w(t,"onPause"),a=w(t,"onPostPause");null==n||n(),j(),_(),o._setSubtreeIsolation(!1),null==a||a()}else{var r=w(t,"onUnpause"),i=w(t,"onPostUnpause");null==r||r(),o._setSubtreeIsolation(!0),N(),R(),_(),null==i||i()}return this}},_setSubtreeIsolation:{value:function(e){h.isolateSubtrees&&m.adjacentElements.forEach((function(t){var n;if(e)if("aria-hidden"===h.isolateSubtrees)"true"!==t.ariaHidden&&"true"!==(null===(n=t.getAttribute("aria-hidden"))||void 0===n?void 0:n.toLowerCase())||m.alreadySilent.add(t),t.setAttribute("aria-hidden","true");else(t.inert||t.hasAttribute("inert"))&&m.alreadySilent.add(t),t.setAttribute("inert",!0);else if(m.alreadySilent.has(t));else if("aria-hidden"===h.isolateSubtrees)t.removeAttribute("aria-hidden");else t.removeAttribute("inert")}))}}}),o.updateContainerElements(e),o}}));
|
|
//# sourceMappingURL=focus-trap.umd.min.js.map
|