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