focus-trap
Advanced tools
Comparing version 7.4.2 to 7.4.3
# Changelog | ||
## 7.4.3 | ||
### Patch Changes | ||
- 134678b: Ensure focus is kept inside the trap when focused element is removed from the DOM ([focus-trap/focus-trap-react#962](https://github.com/focus-trap/focus-trap-react/issues/962)) | ||
## 7.4.2 | ||
@@ -4,0 +10,0 @@ |
/*! | ||
* focus-trap 7.4.2 | ||
* focus-trap 7.4.3 | ||
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE | ||
*/ | ||
import { tabbable, focusable, isTabbable, isFocusable } from 'tabbable'; | ||
import { isFocusable, tabbable, focusable, isTabbable } from 'tabbable'; | ||
@@ -285,4 +285,4 @@ function ownKeys(object, enumerableOnly) { | ||
} | ||
if (node === undefined) { | ||
// option not specified: use fallback options | ||
if (node === undefined || !isFocusable(node, config.tabbableOptions)) { | ||
// option not specified nor focusable: use fallback options | ||
if (findContainerIndex(doc.activeElement) >= 0) { | ||
@@ -614,2 +614,39 @@ node = doc.activeElement; | ||
// | ||
// MUTATION OBSERVER | ||
// | ||
var checkDomRemoval = function checkDomRemoval(mutations) { | ||
var isFocusedNodeRemoved = mutations.some(function (mutation) { | ||
var removedNodes = Array.from(mutation.removedNodes); | ||
return removedNodes.some(function (node) { | ||
return node === state.mostRecentlyFocusedNode; | ||
}); | ||
}); | ||
// If the currently focused is removed then browsers will move focus to the | ||
// <body> element. If this happens, try to move focus back into the trap. | ||
if (isFocusedNodeRemoved) { | ||
tryFocus(getInitialFocusNode()); | ||
} | ||
}; | ||
// Use MutationObserver - if supported - to detect if focused node is removed | ||
// from the DOM. | ||
var mutationObserver = typeof window !== 'undefined' && 'MutationObserver' in window ? new MutationObserver(checkDomRemoval) : undefined; | ||
var updateObservedNodes = function updateObservedNodes() { | ||
if (!mutationObserver) { | ||
return; | ||
} | ||
mutationObserver.disconnect(); | ||
if (state.active && !state.paused) { | ||
state.containers.map(function (container) { | ||
mutationObserver.observe(container, { | ||
subtree: true, | ||
childList: true | ||
}); | ||
}); | ||
} | ||
}; | ||
// | ||
// TRAP DEFINITION | ||
@@ -644,2 +681,3 @@ // | ||
addListeners(); | ||
updateObservedNodes(); | ||
onPostActivate === null || onPostActivate === void 0 ? void 0 : onPostActivate(); | ||
@@ -668,2 +706,3 @@ }; | ||
state.paused = false; | ||
updateObservedNodes(); | ||
activeFocusTraps.deactivateTrap(trapStack, trap); | ||
@@ -699,2 +738,3 @@ var onDeactivate = getOption(options, 'onDeactivate'); | ||
removeListeners(); | ||
updateObservedNodes(); | ||
onPostPause === null || onPostPause === void 0 ? void 0 : onPostPause(); | ||
@@ -713,2 +753,3 @@ return this; | ||
addListeners(); | ||
updateObservedNodes(); | ||
onPostUnpause === null || onPostUnpause === void 0 ? void 0 : onPostUnpause(); | ||
@@ -725,2 +766,3 @@ return this; | ||
} | ||
updateObservedNodes(); | ||
return this; | ||
@@ -727,0 +769,0 @@ } |
/*! | ||
* focus-trap 7.4.2 | ||
* focus-trap 7.4.3 | ||
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE | ||
*/ | ||
import{tabbable as e,focusable as t,isTabbable as n,isFocusable as r}from"tabbable";function a(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 o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?a(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):a(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(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 r=n.call(e,t||"default");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:String(t)}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var u=function(e,t){if(e.length>0){var n=e[e.length-1];n!==t&&n.pause()}var r=e.indexOf(t);-1===r||e.splice(r,1),e.push(t)},c=function(e,t){var n=e.indexOf(t);-1!==n&&e.splice(n,1),e.length>0&&e[e.length-1].unpause()},s=function(e){return"Tab"===e.key||9===e.keyCode},l=function(e){return s(e)&&!e.shiftKey},b=function(e){return s(e)&&e.shiftKey},f=function(e){return setTimeout(e,0)},v=function(e,t){var n=-1;return e.every((function(e,r){return!t(e)||(n=r,!1)})),n},d=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},p=function(e){return e.target.shadowRoot&&"function"==typeof e.composedPath?e.composedPath()[0]:e.target},h=[],y=function(a,i){var y,m=(null==i?void 0:i.document)||document,g=(null==i?void 0:i.trapStack)||h,w=o({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0,isKeyForward:l,isKeyBackward:b},i),O={containers:[],containerGroups:[],tabbableGroups:[],nodeFocusedBeforeActivation:null,mostRecentlyFocusedNode:null,active:!1,paused:!1,delayInitialFocusTimer:void 0},F=function(e,t,n){return e&&void 0!==e[t]?e[t]:w[n||t]},k=function(e,t){var n="function"==typeof(null==t?void 0:t.composedPath)?t.composedPath():void 0;return O.containerGroups.findIndex((function(t){var r=t.container,a=t.tabbableNodes;return r.contains(e)||(null==n?void 0:n.includes(r))||a.find((function(t){return t===e}))}))},E=function(e){var t=w[e];if("function"==typeof t){for(var n=arguments.length,r=new Array(n>1?n-1:0),a=1;a<n;a++)r[a-1]=arguments[a];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(e,"` was specified but was not a node, or did not return a node"))}var o=t;if("string"==typeof t&&!(o=m.querySelector(t)))throw new Error("`".concat(e,"` as selector refers to no known node"));return o},N=function(){var e=E("initialFocus");if(!1===e)return!1;if(void 0===e)if(k(m.activeElement)>=0)e=m.activeElement;else{var t=O.tabbableGroups[0];e=t&&t.firstTabbableNode||E("fallbackFocus")}if(!e)throw new Error("Your focus-trap needs to have at least one focusable element");return e},P=function(){if(O.containerGroups=O.containers.map((function(r){var a=e(r,w.tabbableOptions),o=t(r,w.tabbableOptions);return{container:r,tabbableNodes:a,focusableNodes:o,firstTabbableNode:a.length>0?a[0]:null,lastTabbableNode:a.length>0?a[a.length-1]:null,nextTabbableNode:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r=o.findIndex((function(t){return t===e}));if(!(r<0))return t?o.slice(r+1).find((function(e){return n(e,w.tabbableOptions)})):o.slice(0,r).reverse().find((function(e){return n(e,w.tabbableOptions)}))}}})),O.tabbableGroups=O.containerGroups.filter((function(e){return e.tabbableNodes.length>0})),O.tabbableGroups.length<=0&&!E("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 e(t){!1!==t&&t!==m.activeElement&&(t&&t.focus?(t.focus({preventScroll:!!w.preventScroll}),O.mostRecentlyFocusedNode=t,function(e){return e.tagName&&"input"===e.tagName.toLowerCase()&&"function"==typeof e.select}(t)&&t.select()):e(N()))},D=function(e){var t=E("setReturnFocus",e);return t||!1!==t&&e},G=function(e){var t=p(e);k(t,e)>=0||(d(w.clickOutsideDeactivates,e)?y.deactivate({returnFocus:w.returnFocusOnDeactivate}):d(w.allowOutsideClick,e)||e.preventDefault())},j=function(e){var t=p(e),n=k(t,e)>=0;n||t instanceof Document?n&&(O.mostRecentlyFocusedNode=t):(e.stopImmediatePropagation(),T(O.mostRecentlyFocusedNode||N()))},C=function(e){if(!(t=e,"Escape"!==t.key&&"Esc"!==t.key&&27!==t.keyCode||!1===d(w.escapeDeactivates,e)))return e.preventDefault(),void y.deactivate();var t;(w.isKeyForward(e)||w.isKeyBackward(e))&&function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1],a=p(e);P();var o=null;if(O.tabbableGroups.length>0){var i=k(a,e),u=i>=0?O.containerGroups[i]:void 0;if(i<0)o=t?O.tabbableGroups[O.tabbableGroups.length-1].lastTabbableNode:O.tabbableGroups[0].firstTabbableNode;else if(t){var c=v(O.tabbableGroups,(function(e){var t=e.firstTabbableNode;return a===t}));if(c<0&&(u.container===a||r(a,w.tabbableOptions)&&!n(a,w.tabbableOptions)&&!u.nextTabbableNode(a,!1))&&(c=i),c>=0){var l=0===c?O.tabbableGroups.length-1:c-1;o=O.tabbableGroups[l].lastTabbableNode}else s(e)||(o=u.nextTabbableNode(a,!1))}else{var b=v(O.tabbableGroups,(function(e){var t=e.lastTabbableNode;return a===t}));if(b<0&&(u.container===a||r(a,w.tabbableOptions)&&!n(a,w.tabbableOptions)&&!u.nextTabbableNode(a))&&(b=i),b>=0){var f=b===O.tabbableGroups.length-1?0:b+1;o=O.tabbableGroups[f].firstTabbableNode}else s(e)||(o=u.nextTabbableNode(a))}}else o=E("fallbackFocus");o&&(s(e)&&e.preventDefault(),T(o))}(e,w.isKeyBackward(e))},L=function(e){var t=p(e);k(t,e)>=0||d(w.clickOutsideDeactivates,e)||d(w.allowOutsideClick,e)||(e.preventDefault(),e.stopImmediatePropagation())},x=function(){if(O.active)return u(g,y),O.delayInitialFocusTimer=w.delayInitialFocus?f((function(){T(N())})):T(N()),m.addEventListener("focusin",j,!0),m.addEventListener("mousedown",G,{capture:!0,passive:!1}),m.addEventListener("touchstart",G,{capture:!0,passive:!1}),m.addEventListener("click",L,{capture:!0,passive:!1}),m.addEventListener("keydown",C,{capture:!0,passive:!1}),y},I=function(){if(O.active)return m.removeEventListener("focusin",j,!0),m.removeEventListener("mousedown",G,!0),m.removeEventListener("touchstart",G,!0),m.removeEventListener("click",L,!0),m.removeEventListener("keydown",C,!0),y};return(y={get active(){return O.active},get paused(){return O.paused},activate:function(e){if(O.active)return this;var t=F(e,"onActivate"),n=F(e,"onPostActivate"),r=F(e,"checkCanFocusTrap");r||P(),O.active=!0,O.paused=!1,O.nodeFocusedBeforeActivation=m.activeElement,null==t||t();var a=function(){r&&P(),x(),null==n||n()};return r?(r(O.containers.concat()).then(a,a),this):(a(),this)},deactivate:function(e){if(!O.active)return this;var t=o({onDeactivate:w.onDeactivate,onPostDeactivate:w.onPostDeactivate,checkCanReturnFocus:w.checkCanReturnFocus},e);clearTimeout(O.delayInitialFocusTimer),O.delayInitialFocusTimer=void 0,I(),O.active=!1,O.paused=!1,c(g,y);var n=F(t,"onDeactivate"),r=F(t,"onPostDeactivate"),a=F(t,"checkCanReturnFocus"),i=F(t,"returnFocus","returnFocusOnDeactivate");null==n||n();var u=function(){f((function(){i&&T(D(O.nodeFocusedBeforeActivation)),null==r||r()}))};return i&&a?(a(D(O.nodeFocusedBeforeActivation)).then(u,u),this):(u(),this)},pause:function(e){if(O.paused||!O.active)return this;var t=F(e,"onPause"),n=F(e,"onPostPause");return O.paused=!0,null==t||t(),I(),null==n||n(),this},unpause:function(e){if(!O.paused||!O.active)return this;var t=F(e,"onUnpause"),n=F(e,"onPostUnpause");return O.paused=!1,null==t||t(),P(),x(),null==n||n(),this},updateContainerElements:function(e){var t=[].concat(e).filter(Boolean);return O.containers=t.map((function(e){return"string"==typeof e?m.querySelector(e):e})),O.active&&P(),this}}).updateContainerElements(a),y};export{y as createFocusTrap}; | ||
import{isFocusable as e,tabbable as t,focusable as n,isTabbable as r}from"tabbable";function o(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 a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(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 r=n.call(e,t||"default");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:String(t)}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var u=function(e,t){if(e.length>0){var n=e[e.length-1];n!==t&&n.pause()}var r=e.indexOf(t);-1===r||e.splice(r,1),e.push(t)},c=function(e,t){var n=e.indexOf(t);-1!==n&&e.splice(n,1),e.length>0&&e[e.length-1].unpause()},s=function(e){return"Tab"===e.key||9===e.keyCode},l=function(e){return s(e)&&!e.shiftKey},b=function(e){return s(e)&&e.shiftKey},f=function(e){return setTimeout(e,0)},v=function(e,t){var n=-1;return e.every((function(e,r){return!t(e)||(n=r,!1)})),n},d=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},p=function(e){return e.target.shadowRoot&&"function"==typeof e.composedPath?e.composedPath()[0]:e.target},m=[],y=function(o,i){var y,h=(null==i?void 0:i.document)||document,w=(null==i?void 0:i.trapStack)||m,g=a({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0,isKeyForward:l,isKeyBackward:b},i),O={containers:[],containerGroups:[],tabbableGroups:[],nodeFocusedBeforeActivation:null,mostRecentlyFocusedNode:null,active:!1,paused:!1,delayInitialFocusTimer:void 0},F=function(e,t,n){return e&&void 0!==e[t]?e[t]:g[n||t]},N=function(e,t){var n="function"==typeof(null==t?void 0:t.composedPath)?t.composedPath():void 0;return O.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}))}))},k=function(e){var t=g[e];if("function"==typeof t){for(var n=arguments.length,r=new Array(n>1?n-1:0),o=1;o<n;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(e,"` was specified but was not a node, or did not return a node"))}var a=t;if("string"==typeof t&&!(a=h.querySelector(t)))throw new Error("`".concat(e,"` as selector refers to no known node"));return a},E=function(){var t=k("initialFocus");if(!1===t)return!1;if(void 0===t||!e(t,g.tabbableOptions))if(N(h.activeElement)>=0)t=h.activeElement;else{var n=O.tabbableGroups[0];t=n&&n.firstTabbableNode||k("fallbackFocus")}if(!t)throw new Error("Your focus-trap needs to have at least one focusable element");return t},P=function(){if(O.containerGroups=O.containers.map((function(e){var o=t(e,g.tabbableOptions),a=n(e,g.tabbableOptions);return{container:e,tabbableNodes:o,focusableNodes:a,firstTabbableNode:o.length>0?o[0]:null,lastTabbableNode:o.length>0?o[o.length-1]:null,nextTabbableNode:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=a.findIndex((function(t){return t===e}));if(!(n<0))return t?a.slice(n+1).find((function(e){return r(e,g.tabbableOptions)})):a.slice(0,n).reverse().find((function(e){return r(e,g.tabbableOptions)}))}}})),O.tabbableGroups=O.containerGroups.filter((function(e){return e.tabbableNodes.length>0})),O.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 e(t){!1!==t&&t!==h.activeElement&&(t&&t.focus?(t.focus({preventScroll:!!g.preventScroll}),O.mostRecentlyFocusedNode=t,function(e){return e.tagName&&"input"===e.tagName.toLowerCase()&&"function"==typeof e.select}(t)&&t.select()):e(E()))},D=function(e){var t=k("setReturnFocus",e);return t||!1!==t&&e},G=function(e){var t=p(e);N(t,e)>=0||(d(g.clickOutsideDeactivates,e)?y.deactivate({returnFocus:g.returnFocusOnDeactivate}):d(g.allowOutsideClick,e)||e.preventDefault())},j=function(e){var t=p(e),n=N(t,e)>=0;n||t instanceof Document?n&&(O.mostRecentlyFocusedNode=t):(e.stopImmediatePropagation(),T(O.mostRecentlyFocusedNode||E()))},L=function(t){if(!(n=t,"Escape"!==n.key&&"Esc"!==n.key&&27!==n.keyCode||!1===d(g.escapeDeactivates,t)))return t.preventDefault(),void y.deactivate();var n;(g.isKeyForward(t)||g.isKeyBackward(t))&&function(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],o=p(t);P();var a=null;if(O.tabbableGroups.length>0){var i=N(o,t),u=i>=0?O.containerGroups[i]:void 0;if(i<0)a=n?O.tabbableGroups[O.tabbableGroups.length-1].lastTabbableNode:O.tabbableGroups[0].firstTabbableNode;else if(n){var c=v(O.tabbableGroups,(function(e){var t=e.firstTabbableNode;return o===t}));if(c<0&&(u.container===o||e(o,g.tabbableOptions)&&!r(o,g.tabbableOptions)&&!u.nextTabbableNode(o,!1))&&(c=i),c>=0){var l=0===c?O.tabbableGroups.length-1:c-1;a=O.tabbableGroups[l].lastTabbableNode}else s(t)||(a=u.nextTabbableNode(o,!1))}else{var b=v(O.tabbableGroups,(function(e){var t=e.lastTabbableNode;return o===t}));if(b<0&&(u.container===o||e(o,g.tabbableOptions)&&!r(o,g.tabbableOptions)&&!u.nextTabbableNode(o))&&(b=i),b>=0){var f=b===O.tabbableGroups.length-1?0:b+1;a=O.tabbableGroups[f].firstTabbableNode}else s(t)||(a=u.nextTabbableNode(o))}}else a=k("fallbackFocus");a&&(s(t)&&t.preventDefault(),T(a))}(t,g.isKeyBackward(t))},C=function(e){var t=p(e);N(t,e)>=0||d(g.clickOutsideDeactivates,e)||d(g.allowOutsideClick,e)||(e.preventDefault(),e.stopImmediatePropagation())},x=function(){if(O.active)return u(w,y),O.delayInitialFocusTimer=g.delayInitialFocus?f((function(){T(E())})):T(E()),h.addEventListener("focusin",j,!0),h.addEventListener("mousedown",G,{capture:!0,passive:!1}),h.addEventListener("touchstart",G,{capture:!0,passive:!1}),h.addEventListener("click",C,{capture:!0,passive:!1}),h.addEventListener("keydown",L,{capture:!0,passive:!1}),y},I=function(){if(O.active)return h.removeEventListener("focusin",j,!0),h.removeEventListener("mousedown",G,!0),h.removeEventListener("touchstart",G,!0),h.removeEventListener("click",C,!0),h.removeEventListener("keydown",L,!0),y},R="undefined"!=typeof window&&"MutationObserver"in window?new MutationObserver((function(e){e.some((function(e){return Array.from(e.removedNodes).some((function(e){return e===O.mostRecentlyFocusedNode}))}))&&T(E())})):void 0,S=function(){R&&(R.disconnect(),O.active&&!O.paused&&O.containers.map((function(e){R.observe(e,{subtree:!0,childList:!0})})))};return(y={get active(){return O.active},get paused(){return O.paused},activate:function(e){if(O.active)return this;var t=F(e,"onActivate"),n=F(e,"onPostActivate"),r=F(e,"checkCanFocusTrap");r||P(),O.active=!0,O.paused=!1,O.nodeFocusedBeforeActivation=h.activeElement,null==t||t();var o=function(){r&&P(),x(),S(),null==n||n()};return r?(r(O.containers.concat()).then(o,o),this):(o(),this)},deactivate:function(e){if(!O.active)return this;var t=a({onDeactivate:g.onDeactivate,onPostDeactivate:g.onPostDeactivate,checkCanReturnFocus:g.checkCanReturnFocus},e);clearTimeout(O.delayInitialFocusTimer),O.delayInitialFocusTimer=void 0,I(),O.active=!1,O.paused=!1,S(),c(w,y);var n=F(t,"onDeactivate"),r=F(t,"onPostDeactivate"),o=F(t,"checkCanReturnFocus"),i=F(t,"returnFocus","returnFocusOnDeactivate");null==n||n();var u=function(){f((function(){i&&T(D(O.nodeFocusedBeforeActivation)),null==r||r()}))};return i&&o?(o(D(O.nodeFocusedBeforeActivation)).then(u,u),this):(u(),this)},pause:function(e){if(O.paused||!O.active)return this;var t=F(e,"onPause"),n=F(e,"onPostPause");return O.paused=!0,null==t||t(),I(),S(),null==n||n(),this},unpause:function(e){if(!O.paused||!O.active)return this;var t=F(e,"onUnpause"),n=F(e,"onPostUnpause");return O.paused=!1,null==t||t(),P(),x(),S(),null==n||n(),this},updateContainerElements:function(e){var t=[].concat(e).filter(Boolean);return O.containers=t.map((function(e){return"string"==typeof e?h.querySelector(e):e})),O.active&&P(),S(),this}}).updateContainerElements(o),y};export{y as createFocusTrap}; | ||
//# sourceMappingURL=focus-trap.esm.min.js.map |
/*! | ||
* focus-trap 7.4.2 | ||
* focus-trap 7.4.3 | ||
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE | ||
@@ -289,4 +289,4 @@ */ | ||
} | ||
if (node === undefined) { | ||
// option not specified: use fallback options | ||
if (node === undefined || !tabbable.isFocusable(node, config.tabbableOptions)) { | ||
// option not specified nor focusable: use fallback options | ||
if (findContainerIndex(doc.activeElement) >= 0) { | ||
@@ -618,2 +618,39 @@ node = doc.activeElement; | ||
// | ||
// MUTATION OBSERVER | ||
// | ||
var checkDomRemoval = function checkDomRemoval(mutations) { | ||
var isFocusedNodeRemoved = mutations.some(function (mutation) { | ||
var removedNodes = Array.from(mutation.removedNodes); | ||
return removedNodes.some(function (node) { | ||
return node === state.mostRecentlyFocusedNode; | ||
}); | ||
}); | ||
// If the currently focused is removed then browsers will move focus to the | ||
// <body> element. If this happens, try to move focus back into the trap. | ||
if (isFocusedNodeRemoved) { | ||
tryFocus(getInitialFocusNode()); | ||
} | ||
}; | ||
// Use MutationObserver - if supported - to detect if focused node is removed | ||
// from the DOM. | ||
var mutationObserver = typeof window !== 'undefined' && 'MutationObserver' in window ? new MutationObserver(checkDomRemoval) : undefined; | ||
var updateObservedNodes = function updateObservedNodes() { | ||
if (!mutationObserver) { | ||
return; | ||
} | ||
mutationObserver.disconnect(); | ||
if (state.active && !state.paused) { | ||
state.containers.map(function (container) { | ||
mutationObserver.observe(container, { | ||
subtree: true, | ||
childList: true | ||
}); | ||
}); | ||
} | ||
}; | ||
// | ||
// TRAP DEFINITION | ||
@@ -648,2 +685,3 @@ // | ||
addListeners(); | ||
updateObservedNodes(); | ||
onPostActivate === null || onPostActivate === void 0 ? void 0 : onPostActivate(); | ||
@@ -672,2 +710,3 @@ }; | ||
state.paused = false; | ||
updateObservedNodes(); | ||
activeFocusTraps.deactivateTrap(trapStack, trap); | ||
@@ -703,2 +742,3 @@ var onDeactivate = getOption(options, 'onDeactivate'); | ||
removeListeners(); | ||
updateObservedNodes(); | ||
onPostPause === null || onPostPause === void 0 ? void 0 : onPostPause(); | ||
@@ -717,2 +757,3 @@ return this; | ||
addListeners(); | ||
updateObservedNodes(); | ||
onPostUnpause === null || onPostUnpause === void 0 ? void 0 : onPostUnpause(); | ||
@@ -729,2 +770,3 @@ return this; | ||
} | ||
updateObservedNodes(); | ||
return this; | ||
@@ -731,0 +773,0 @@ } |
/*! | ||
* focus-trap 7.4.2 | ||
* 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,h=n({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0,isKeyForward:u,isKeyBackward:c},a),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 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=h[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 e=O("initialFocus");if(!1===e)return!1;if(void 0===e)if(w(p.activeElement)>=0)e=p.activeElement;else{var t=m.tabbableGroups[0];e=t&&t.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(t){var n=e.tabbable(t,h.tabbableOptions),a=e.focusable(t,h.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,h.tabbableOptions)})):a.slice(0,r).reverse().find((function(t){return e.isTabbable(t,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")},k=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()))},P=function(e){var t=O("setReturnFocus",e);return t||!1!==t&&e},E=function(e){var t=f(e);w(t,e)>=0||(b(h.clickOutsideDeactivates,e)?d.deactivate({returnFocus:h.returnFocusOnDeactivate}):b(h.allowOutsideClick,e)||e.preventDefault())},N=function(e){var t=f(e),n=w(t,e)>=0;n||t instanceof Document?n&&(m.mostRecentlyFocusedNode=t):(e.stopImmediatePropagation(),k(m.mostRecentlyFocusedNode||F()))},D=function(t){if(!(n=t,"Escape"!==n.key&&"Esc"!==n.key&&27!==n.keyCode||!1===b(h.escapeDeactivates,t)))return t.preventDefault(),void d.deactivate();var n;(h.isKeyForward(t)||h.isKeyBackward(t))&&function(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],a=f(t);T();var r=null;if(m.tabbableGroups.length>0){var o=w(a,t),u=o>=0?m.containerGroups[o]:void 0;if(o<0)r=n?m.tabbableGroups[m.tabbableGroups.length-1].lastTabbableNode:m.tabbableGroups[0].firstTabbableNode;else if(n){var c=l(m.tabbableGroups,(function(e){var t=e.firstTabbableNode;return a===t}));if(c<0&&(u.container===a||e.isFocusable(a,h.tabbableOptions)&&!e.isTabbable(a,h.tabbableOptions)&&!u.nextTabbableNode(a,!1))&&(c=o),c>=0){var s=0===c?m.tabbableGroups.length-1:c-1;r=m.tabbableGroups[s].lastTabbableNode}else i(t)||(r=u.nextTabbableNode(a,!1))}else{var b=l(m.tabbableGroups,(function(e){var t=e.lastTabbableNode;return a===t}));if(b<0&&(u.container===a||e.isFocusable(a,h.tabbableOptions)&&!e.isTabbable(a,h.tabbableOptions)&&!u.nextTabbableNode(a))&&(b=o),b>=0){var v=b===m.tabbableGroups.length-1?0:b+1;r=m.tabbableGroups[v].firstTabbableNode}else i(t)||(r=u.nextTabbableNode(a))}}else r=O("fallbackFocus");r&&(i(t)&&t.preventDefault(),k(r))}(t,h.isKeyBackward(t))},G=function(e){var t=f(e);w(t,e)>=0||b(h.clickOutsideDeactivates,e)||b(h.allowOutsideClick,e)||(e.preventDefault(),e.stopImmediatePropagation())},j=function(){if(m.active)return r(y,d),m.delayInitialFocusTimer=h.delayInitialFocus?s((function(){k(F())})):k(F()),p.addEventListener("focusin",N,!0),p.addEventListener("mousedown",E,{capture:!0,passive:!1}),p.addEventListener("touchstart",E,{capture:!0,passive:!1}),p.addEventListener("click",G,{capture:!0,passive:!1}),p.addEventListener("keydown",D,{capture:!0,passive:!1}),d},x=function(){if(m.active)return p.removeEventListener("focusin",N,!0),p.removeEventListener("mousedown",E,!0),p.removeEventListener("touchstart",E,!0),p.removeEventListener("click",G,!0),p.removeEventListener("keydown",D,!0),d};return(d={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"),a=g(e,"checkCanFocusTrap");a||T(),m.active=!0,m.paused=!1,m.nodeFocusedBeforeActivation=p.activeElement,null==t||t();var r=function(){a&&T(),j(),null==n||n()};return a?(a(m.containers.concat()).then(r,r),this):(r(),this)},deactivate:function(e){if(!m.active)return this;var t=n({onDeactivate:h.onDeactivate,onPostDeactivate:h.onPostDeactivate,checkCanReturnFocus:h.checkCanReturnFocus},e);clearTimeout(m.delayInitialFocusTimer),m.delayInitialFocusTimer=void 0,x(),m.active=!1,m.paused=!1,o(y,d);var a=g(t,"onDeactivate"),r=g(t,"onPostDeactivate"),i=g(t,"checkCanReturnFocus"),u=g(t,"returnFocus","returnFocusOnDeactivate");null==a||a();var c=function(){s((function(){u&&k(P(m.nodeFocusedBeforeActivation)),null==r||r()}))};return u&&i?(i(P(m.nodeFocusedBeforeActivation)).then(c,c),this):(c(),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(),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(),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(),this}}).updateContainerElements(t),d}; | ||
"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 |
/*! | ||
* focus-trap 7.4.2 | ||
* focus-trap 7.4.3 | ||
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE | ||
@@ -294,4 +294,4 @@ */ | ||
} | ||
if (node === undefined) { | ||
// option not specified: use fallback options | ||
if (node === undefined || !tabbable.isFocusable(node, config.tabbableOptions)) { | ||
// option not specified nor focusable: use fallback options | ||
if (findContainerIndex(doc.activeElement) >= 0) { | ||
@@ -623,2 +623,39 @@ node = doc.activeElement; | ||
// | ||
// MUTATION OBSERVER | ||
// | ||
var checkDomRemoval = function checkDomRemoval(mutations) { | ||
var isFocusedNodeRemoved = mutations.some(function (mutation) { | ||
var removedNodes = Array.from(mutation.removedNodes); | ||
return removedNodes.some(function (node) { | ||
return node === state.mostRecentlyFocusedNode; | ||
}); | ||
}); | ||
// If the currently focused is removed then browsers will move focus to the | ||
// <body> element. If this happens, try to move focus back into the trap. | ||
if (isFocusedNodeRemoved) { | ||
tryFocus(getInitialFocusNode()); | ||
} | ||
}; | ||
// Use MutationObserver - if supported - to detect if focused node is removed | ||
// from the DOM. | ||
var mutationObserver = typeof window !== 'undefined' && 'MutationObserver' in window ? new MutationObserver(checkDomRemoval) : undefined; | ||
var updateObservedNodes = function updateObservedNodes() { | ||
if (!mutationObserver) { | ||
return; | ||
} | ||
mutationObserver.disconnect(); | ||
if (state.active && !state.paused) { | ||
state.containers.map(function (container) { | ||
mutationObserver.observe(container, { | ||
subtree: true, | ||
childList: true | ||
}); | ||
}); | ||
} | ||
}; | ||
// | ||
// TRAP DEFINITION | ||
@@ -653,2 +690,3 @@ // | ||
addListeners(); | ||
updateObservedNodes(); | ||
onPostActivate === null || onPostActivate === void 0 ? void 0 : onPostActivate(); | ||
@@ -677,2 +715,3 @@ }; | ||
state.paused = false; | ||
updateObservedNodes(); | ||
activeFocusTraps.deactivateTrap(trapStack, trap); | ||
@@ -708,2 +747,3 @@ var onDeactivate = getOption(options, 'onDeactivate'); | ||
removeListeners(); | ||
updateObservedNodes(); | ||
onPostPause === null || onPostPause === void 0 ? void 0 : onPostPause(); | ||
@@ -722,2 +762,3 @@ return this; | ||
addListeners(); | ||
updateObservedNodes(); | ||
onPostUnpause === null || onPostUnpause === void 0 ? void 0 : onPostUnpause(); | ||
@@ -734,2 +775,3 @@ return this; | ||
} | ||
updateObservedNodes(); | ||
return this; | ||
@@ -736,0 +778,0 @@ } |
/*! | ||
* focus-trap 7.4.2 | ||
* 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,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){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 a(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?n(Object(a),!0).forEach((function(t){o(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):n(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function o(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)},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,a){return!t(e)||(n=a,!1)})),n},f=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},d=function(e){return e.target.shadowRoot&&"function"==typeof e.composedPath?e.composedPath()[0]:e.target},v=[];e.createFocusTrap=function(e,n){var o,p=(null==n?void 0:n.document)||document,y=(null==n?void 0:n.trapStack)||v,h=a({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 a=t.container,o=t.tabbableNodes;return a.contains(e)||(null==n?void 0:n.includes(a))||o.find((function(t){return t===e}))}))},O=function(e){var t=h[e];if("function"==typeof t){for(var n=arguments.length,a=new Array(n>1?n-1:0),o=1;o<n;o++)a[o-1]=arguments[o];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 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)if(w(p.activeElement)>=0)e=p.activeElement;else{var t=m.tabbableGroups[0];e=t&&t.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),a=t.focusable(e,h.tabbableOptions);return{container:e,tabbableNodes:n,focusableNodes:a,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],o=a.findIndex((function(t){return t===e}));if(!(o<0))return n?a.slice(o+1).find((function(e){return t.isTabbable(e,h.tabbableOptions)})):a.slice(0,o).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")},k=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()))},P=function(e){var t=O("setReturnFocus",e);return t||!1!==t&&e},E=function(e){var t=d(e);w(t,e)>=0||(f(h.clickOutsideDeactivates,e)?o.deactivate({returnFocus:h.returnFocusOnDeactivate}):f(h.allowOutsideClick,e)||e.preventDefault())},N=function(e){var t=d(e),n=w(t,e)>=0;n||t instanceof Document?n&&(m.mostRecentlyFocusedNode=t):(e.stopImmediatePropagation(),k(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 o.deactivate();var n;(h.isKeyForward(e)||h.isKeyBackward(e))&&function(e){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],a=d(e);T();var o=null;if(m.tabbableGroups.length>0){var r=w(a,e),i=r>=0?m.containerGroups[r]:void 0;if(r<0)o=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 a===t}));if(c<0&&(i.container===a||t.isFocusable(a,h.tabbableOptions)&&!t.isTabbable(a,h.tabbableOptions)&&!i.nextTabbableNode(a,!1))&&(c=r),c>=0){var s=0===c?m.tabbableGroups.length-1:c-1;o=m.tabbableGroups[s].lastTabbableNode}else u(e)||(o=i.nextTabbableNode(a,!1))}else{var l=b(m.tabbableGroups,(function(e){var t=e.lastTabbableNode;return a===t}));if(l<0&&(i.container===a||t.isFocusable(a,h.tabbableOptions)&&!t.isTabbable(a,h.tabbableOptions)&&!i.nextTabbableNode(a))&&(l=r),l>=0){var f=l===m.tabbableGroups.length-1?0:l+1;o=m.tabbableGroups[f].firstTabbableNode}else u(e)||(o=i.nextTabbableNode(a))}}else o=O("fallbackFocus");o&&(u(e)&&e.preventDefault(),k(o))}(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,o),m.delayInitialFocusTimer=h.delayInitialFocus?l((function(){k(F())})):k(F()),p.addEventListener("focusin",N,!0),p.addEventListener("mousedown",E,{capture:!0,passive:!1}),p.addEventListener("touchstart",E,{capture:!0,passive:!1}),p.addEventListener("click",G,{capture:!0,passive:!1}),p.addEventListener("keydown",D,{capture:!0,passive:!1}),o},x=function(){if(m.active)return p.removeEventListener("focusin",N,!0),p.removeEventListener("mousedown",E,!0),p.removeEventListener("touchstart",E,!0),p.removeEventListener("click",G,!0),p.removeEventListener("keydown",D,!0),o};return(o={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"),a=g(e,"checkCanFocusTrap");a||T(),m.active=!0,m.paused=!1,m.nodeFocusedBeforeActivation=p.activeElement,null==t||t();var o=function(){a&&T(),j(),null==n||n()};return a?(a(m.containers.concat()).then(o,o),this):(o(),this)},deactivate:function(e){if(!m.active)return this;var t=a({onDeactivate:h.onDeactivate,onPostDeactivate:h.onPostDeactivate,checkCanReturnFocus:h.checkCanReturnFocus},e);clearTimeout(m.delayInitialFocusTimer),m.delayInitialFocusTimer=void 0,x(),m.active=!1,m.paused=!1,i(y,o);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&&k(P(m.nodeFocusedBeforeActivation)),null==r||r()}))};return c&&u?(u(P(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(),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(),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(),this}}).updateContainerElements(e),o},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!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 |
52
index.js
@@ -263,4 +263,4 @@ import { tabbable, focusable, isFocusable, isTabbable } from 'tabbable'; | ||
if (node === undefined) { | ||
// option not specified: use fallback options | ||
if (node === undefined || !isFocusable(node, config.tabbableOptions)) { | ||
// option not specified nor focusable: use fallback options | ||
if (findContainerIndex(doc.activeElement) >= 0) { | ||
@@ -660,2 +660,44 @@ node = doc.activeElement; | ||
// | ||
// MUTATION OBSERVER | ||
// | ||
const checkDomRemoval = function (mutations) { | ||
const isFocusedNodeRemoved = mutations.some(function (mutation) { | ||
const removedNodes = Array.from(mutation.removedNodes); | ||
return removedNodes.some(function (node) { | ||
return node === state.mostRecentlyFocusedNode; | ||
}); | ||
}); | ||
// If the currently focused is removed then browsers will move focus to the | ||
// <body> element. If this happens, try to move focus back into the trap. | ||
if (isFocusedNodeRemoved) { | ||
tryFocus(getInitialFocusNode()); | ||
} | ||
}; | ||
// Use MutationObserver - if supported - to detect if focused node is removed | ||
// from the DOM. | ||
const mutationObserver = | ||
typeof window !== 'undefined' && 'MutationObserver' in window | ||
? new MutationObserver(checkDomRemoval) | ||
: undefined; | ||
const updateObservedNodes = function () { | ||
if (!mutationObserver) { | ||
return; | ||
} | ||
mutationObserver.disconnect(); | ||
if (state.active && !state.paused) { | ||
state.containers.map(function (container) { | ||
mutationObserver.observe(container, { | ||
subtree: true, | ||
childList: true, | ||
}); | ||
}); | ||
} | ||
}; | ||
// | ||
// TRAP DEFINITION | ||
@@ -697,2 +739,3 @@ // | ||
addListeners(); | ||
updateObservedNodes(); | ||
onPostActivate?.(); | ||
@@ -731,2 +774,3 @@ }; | ||
state.paused = false; | ||
updateObservedNodes(); | ||
@@ -778,2 +822,3 @@ activeFocusTraps.deactivateTrap(trapStack, trap); | ||
removeListeners(); | ||
updateObservedNodes(); | ||
@@ -797,2 +842,3 @@ onPostPause?.(); | ||
addListeners(); | ||
updateObservedNodes(); | ||
@@ -814,2 +860,4 @@ onPostUnpause?.(); | ||
updateObservedNodes(); | ||
return this; | ||
@@ -816,0 +864,0 @@ }, |
{ | ||
"name": "focus-trap", | ||
"version": "7.4.2", | ||
"version": "7.4.3", | ||
"description": "Trap focus within a DOM node.", | ||
@@ -5,0 +5,0 @@ "main": "dist/focus-trap.js", |
@@ -363,3 +363,3 @@ # focus-trap [![CI](https://github.com/focus-trap/focus-trap/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/focus-trap/focus-trap/actions?query=workflow:CI+branch:master) [![license](https://badgen.now.sh/badge/license/MIT)](./LICENSE) | ||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/skjnldsv"><img src="https://avatars.githubusercontent.com/u/14975046?v=4?s=100" width="100px;" alt="John Molakvoæ"/><br /><sub><b>John Molakvoæ</b></sub></a><br /><a href="#ideas-skjnldsv" title="Ideas, Planning, & Feedback">🤔</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="http://reload.dk"><img src="https://avatars.githubusercontent.com/u/73966?v=4?s=100" width="100px;" alt="Kasper Garnæs"/><br /><sub><b>Kasper Garnæs</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=kasperg" title="Documentation">📖</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="http://reload.dk"><img src="https://avatars.githubusercontent.com/u/73966?v=4?s=100" width="100px;" alt="Kasper Garnæs"/><br /><sub><b>Kasper Garnæs</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/commits?author=kasperg" title="Documentation">📖</a> <a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Akasperg" title="Bug reports">🐛</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=kasperg" title="Code">💻</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="http://blogs.esri.com/esri/arcgis/"><img src="https://avatars.githubusercontent.com/u/1231455?v=4?s=100" width="100px;" alt="Matt Driscoll"/><br /><sub><b>Matt Driscoll</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Adriskull" title="Bug reports">🐛</a> <a href="https://github.com/focus-trap/focus-trap/commits?author=driskull" title="Code">💻</a> <a href="#tutorial-driskull" title="Tutorials">✅</a></td> | ||
@@ -366,0 +366,0 @@ <td align="center" valign="top" width="14.28%"><a href="https://github.com/michael-ar"><img src="https://avatars3.githubusercontent.com/u/18557997?v=4?s=100" width="100px;" alt="Michael Reynolds"/><br /><sub><b>Michael Reynolds</b></sub></a><br /><a href="https://github.com/focus-trap/focus-trap/issues?q=author%3Amichael-ar" title="Bug reports">🐛</a></td> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
516420
3279