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