focus-trap 6.6.1 → 6.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/README.md +26 -9
- package/dist/focus-trap.esm.js +148 -68
- package/dist/focus-trap.esm.js.map +1 -1
- package/dist/focus-trap.esm.min.js +2 -2
- package/dist/focus-trap.esm.min.js.map +1 -1
- package/dist/focus-trap.js +147 -67
- package/dist/focus-trap.js.map +1 -1
- package/dist/focus-trap.min.js +2 -2
- package/dist/focus-trap.min.js.map +1 -1
- package/dist/focus-trap.umd.js +150 -70
- package/dist/focus-trap.umd.js.map +1 -1
- package/dist/focus-trap.umd.min.js +2 -2
- package/dist/focus-trap.umd.min.js.map +1 -1
- package/index.d.ts +25 -4
- package/index.js +150 -48
- package/package.json +27 -25
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"focus-trap.min.js","sources":["../index.js"],"sourcesContent":["import { tabbable, isFocusable } from 'tabbable';\n\nconst activeFocusTraps = (function () {\n const trapQueue = [];\n return {\n activateTrap(trap) {\n if (trapQueue.length > 0) {\n const activeTrap = trapQueue[trapQueue.length - 1];\n if (activeTrap !== trap) {\n activeTrap.pause();\n }\n }\n\n const trapIndex = trapQueue.indexOf(trap);\n if (trapIndex === -1) {\n trapQueue.push(trap);\n } else {\n // move this existing trap to the front of the queue\n trapQueue.splice(trapIndex, 1);\n trapQueue.push(trap);\n }\n },\n\n deactivateTrap(trap) {\n const trapIndex = trapQueue.indexOf(trap);\n if (trapIndex !== -1) {\n trapQueue.splice(trapIndex, 1);\n }\n\n if (trapQueue.length > 0) {\n trapQueue[trapQueue.length - 1].unpause();\n }\n },\n };\n})();\n\nconst isSelectableInput = function (node) {\n return (\n node.tagName &&\n node.tagName.toLowerCase() === 'input' &&\n typeof node.select === 'function'\n );\n};\n\nconst isEscapeEvent = function (e) {\n return e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27;\n};\n\nconst isTabEvent = function (e) {\n return e.key === 'Tab' || e.keyCode === 9;\n};\n\nconst delay = function (fn) {\n return setTimeout(fn, 0);\n};\n\n// Array.find/findIndex() are not supported on IE; this replicates enough\n// of Array.findIndex() for our needs\nconst findIndex = function (arr, fn) {\n let idx = -1;\n\n arr.every(function (value, i) {\n if (fn(value)) {\n idx = i;\n return false; // break\n }\n\n return true; // next\n });\n\n return idx;\n};\n\n/**\n * Get an option's value when it could be a plain value, or a handler that provides\n * the value.\n * @param {*} value Option's value to check.\n * @param {...*} [params] Any parameters to pass to the handler, if `value` is a function.\n * @returns {*} The `value`, or the handler's returned value.\n */\nconst valueOrHandler = function (value, ...params) {\n return typeof value === 'function' ? value(...params) : value;\n};\n\nconst createFocusTrap = function (elements, userOptions) {\n const doc = document;\n\n const config = {\n returnFocusOnDeactivate: true,\n escapeDeactivates: true,\n delayInitialFocus: true,\n ...userOptions,\n };\n\n const state = {\n // @type {Array<HTMLElement>}\n containers: [],\n\n // list of objects identifying the first and last tabbable nodes in all containers/groups in\n // the trap\n // NOTE: it's possible that a group has no tabbable nodes if nodes get removed while the trap\n // is active, but the trap should never get to a state where there isn't at least one group\n // with at least one tabbable node in it (that would lead to an error condition that would\n // result in an error being thrown)\n // @type {Array<{ container: HTMLElement, firstTabbableNode: HTMLElement|null, lastTabbableNode: HTMLElement|null }>}\n tabbableGroups: [],\n\n nodeFocusedBeforeActivation: null,\n mostRecentlyFocusedNode: null,\n active: false,\n paused: false,\n\n // timer ID for when delayInitialFocus is true and initial focus in this trap\n // has been delayed during activation\n delayInitialFocusTimer: undefined,\n };\n\n let trap; // eslint-disable-line prefer-const -- some private functions reference it, and its methods reference private functions, so we must declare here and define later\n\n const getOption = (configOverrideOptions, optionName, configOptionName) => {\n return configOverrideOptions &&\n configOverrideOptions[optionName] !== undefined\n ? configOverrideOptions[optionName]\n : config[configOptionName || optionName];\n };\n\n const containersContain = function (element) {\n return state.containers.some((container) => container.contains(element));\n };\n\n const getNodeForOption = function (optionName) {\n const optionValue = config[optionName];\n if (!optionValue) {\n return null;\n }\n\n let node = optionValue;\n\n if (typeof optionValue === 'string') {\n node = doc.querySelector(optionValue);\n if (!node) {\n throw new Error(`\\`${optionName}\\` refers to no known node`);\n }\n }\n\n if (typeof optionValue === 'function') {\n node = optionValue();\n if (!node) {\n throw new Error(`\\`${optionName}\\` did not return a node`);\n }\n }\n\n return node;\n };\n\n const getInitialFocusNode = function () {\n let node;\n\n // false indicates we want no initialFocus at all\n if (getOption({}, 'initialFocus') === false) {\n return false;\n }\n\n if (getNodeForOption('initialFocus') !== null) {\n node = getNodeForOption('initialFocus');\n } else if (containersContain(doc.activeElement)) {\n node = doc.activeElement;\n } else {\n const firstTabbableGroup = state.tabbableGroups[0];\n const firstTabbableNode =\n firstTabbableGroup && firstTabbableGroup.firstTabbableNode;\n node = firstTabbableNode || getNodeForOption('fallbackFocus');\n }\n\n if (!node) {\n throw new Error(\n 'Your focus-trap needs to have at least one focusable element'\n );\n }\n\n return node;\n };\n\n const updateTabbableNodes = function () {\n state.tabbableGroups = state.containers\n .map((container) => {\n const tabbableNodes = tabbable(container);\n\n if (tabbableNodes.length > 0) {\n return {\n container,\n firstTabbableNode: tabbableNodes[0],\n lastTabbableNode: tabbableNodes[tabbableNodes.length - 1],\n };\n }\n\n return undefined;\n })\n .filter((group) => !!group); // remove groups with no tabbable nodes\n\n // throw if no groups have tabbable nodes and we don't have a fallback focus node either\n if (\n state.tabbableGroups.length <= 0 &&\n !getNodeForOption('fallbackFocus')\n ) {\n throw new Error(\n 'Your focus-trap must have at least one container with at least one tabbable node in it at all times'\n );\n }\n };\n\n const tryFocus = function (node) {\n if (node === false) {\n return;\n }\n\n if (node === doc.activeElement) {\n return;\n }\n\n if (!node || !node.focus) {\n tryFocus(getInitialFocusNode());\n return;\n }\n\n node.focus({ preventScroll: !!config.preventScroll });\n state.mostRecentlyFocusedNode = node;\n\n if (isSelectableInput(node)) {\n node.select();\n }\n };\n\n const getReturnFocusNode = function (previousActiveElement) {\n const node = getNodeForOption('setReturnFocus');\n\n return node ? node : previousActiveElement;\n };\n\n // This needs to be done on mousedown and touchstart instead of click\n // so that it precedes the focus event.\n const checkPointerDown = function (e) {\n if (containersContain(e.target)) {\n // allow the click since it ocurred inside the trap\n return;\n }\n\n if (valueOrHandler(config.clickOutsideDeactivates, e)) {\n // immediately deactivate the trap\n trap.deactivate({\n // if, on deactivation, we should return focus to the node originally-focused\n // when the trap was activated (or the configured `setReturnFocus` node),\n // then assume it's also OK to return focus to the outside node that was\n // just clicked, causing deactivation, as long as that node is focusable;\n // if it isn't focusable, then return focus to the original node focused\n // on activation (or the configured `setReturnFocus` node)\n // NOTE: by setting `returnFocus: false`, deactivate() will do nothing,\n // which will result in the outside click setting focus to the node\n // that was clicked, whether it's focusable or not; by setting\n // `returnFocus: true`, we'll attempt to re-focus the node originally-focused\n // on activation (or the configured `setReturnFocus` node)\n returnFocus: config.returnFocusOnDeactivate && !isFocusable(e.target),\n });\n return;\n }\n\n // This is needed for mobile devices.\n // (If we'll only let `click` events through,\n // then on mobile they will be blocked anyways if `touchstart` is blocked.)\n if (valueOrHandler(config.allowOutsideClick, e)) {\n // allow the click outside the trap to take place\n return;\n }\n\n // otherwise, prevent the click\n e.preventDefault();\n };\n\n // In case focus escapes the trap for some strange reason, pull it back in.\n const checkFocusIn = function (e) {\n const targetContained = containersContain(e.target);\n // In Firefox when you Tab out of an iframe the Document is briefly focused.\n if (targetContained || e.target instanceof Document) {\n if (targetContained) {\n state.mostRecentlyFocusedNode = e.target;\n }\n } else {\n // escaped! pull it back in to where it just left\n e.stopImmediatePropagation();\n tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());\n }\n };\n\n // Hijack Tab events on the first and last focusable nodes of the trap,\n // in order to prevent focus from escaping. If it escapes for even a\n // moment it can end up scrolling the page and causing confusion so we\n // kind of need to capture the action at the keydown phase.\n const checkTab = function (e) {\n updateTabbableNodes();\n\n let destinationNode = null;\n\n if (state.tabbableGroups.length > 0) {\n // make sure the target is actually contained in a group\n // NOTE: the target may also be the container itself if it's tabbable\n // with tabIndex='-1' and was given initial focus\n const containerIndex = findIndex(state.tabbableGroups, ({ container }) =>\n container.contains(e.target)\n );\n\n if (containerIndex < 0) {\n // target not found in any group: quite possible focus has escaped the trap,\n // so bring it back in to...\n if (e.shiftKey) {\n // ...the last node in the last group\n destinationNode =\n state.tabbableGroups[state.tabbableGroups.length - 1]\n .lastTabbableNode;\n } else {\n // ...the first node in the first group\n destinationNode = state.tabbableGroups[0].firstTabbableNode;\n }\n } else if (e.shiftKey) {\n // REVERSE\n\n // is the target the first tabbable node in a group?\n let startOfGroupIndex = findIndex(\n state.tabbableGroups,\n ({ firstTabbableNode }) => e.target === firstTabbableNode\n );\n\n if (\n startOfGroupIndex < 0 &&\n state.tabbableGroups[containerIndex].container === e.target\n ) {\n // an exception case where the target is the container itself, in which\n // case, we should handle shift+tab as if focus were on the container's\n // first tabbable node, and go to the last tabbable node of the LAST group\n startOfGroupIndex = containerIndex;\n }\n\n if (startOfGroupIndex >= 0) {\n // YES: then shift+tab should go to the last tabbable node in the\n // previous group (and wrap around to the last tabbable node of\n // the LAST group if it's the first tabbable node of the FIRST group)\n const destinationGroupIndex =\n startOfGroupIndex === 0\n ? state.tabbableGroups.length - 1\n : startOfGroupIndex - 1;\n\n const destinationGroup = state.tabbableGroups[destinationGroupIndex];\n destinationNode = destinationGroup.lastTabbableNode;\n }\n } else {\n // FORWARD\n\n // is the target the last tabbable node in a group?\n let lastOfGroupIndex = findIndex(\n state.tabbableGroups,\n ({ lastTabbableNode }) => e.target === lastTabbableNode\n );\n\n if (\n lastOfGroupIndex < 0 &&\n state.tabbableGroups[containerIndex].container === e.target\n ) {\n // an exception case where the target is the container itself, in which\n // case, we should handle tab as if focus were on the container's\n // last tabbable node, and go to the first tabbable node of the FIRST group\n lastOfGroupIndex = containerIndex;\n }\n\n if (lastOfGroupIndex >= 0) {\n // YES: then tab should go to the first tabbable node in the next\n // group (and wrap around to the first tabbable node of the FIRST\n // group if it's the last tabbable node of the LAST group)\n const destinationGroupIndex =\n lastOfGroupIndex === state.tabbableGroups.length - 1\n ? 0\n : lastOfGroupIndex + 1;\n\n const destinationGroup = state.tabbableGroups[destinationGroupIndex];\n destinationNode = destinationGroup.firstTabbableNode;\n }\n }\n } else {\n destinationNode = getNodeForOption('fallbackFocus');\n }\n\n if (destinationNode) {\n e.preventDefault();\n tryFocus(destinationNode);\n }\n // else, let the browser take care of [shift+]tab and move the focus\n };\n\n const checkKey = function (e) {\n if (\n isEscapeEvent(e) &&\n valueOrHandler(config.escapeDeactivates) !== false\n ) {\n e.preventDefault();\n trap.deactivate();\n return;\n }\n\n if (isTabEvent(e)) {\n checkTab(e);\n return;\n }\n };\n\n const checkClick = function (e) {\n if (valueOrHandler(config.clickOutsideDeactivates, e)) {\n return;\n }\n\n if (containersContain(e.target)) {\n return;\n }\n\n if (valueOrHandler(config.allowOutsideClick, e)) {\n return;\n }\n\n e.preventDefault();\n e.stopImmediatePropagation();\n };\n\n //\n // EVENT LISTENERS\n //\n\n const addListeners = function () {\n if (!state.active) {\n return;\n }\n\n // There can be only one listening focus trap at a time\n activeFocusTraps.activateTrap(trap);\n\n // Delay ensures that the focused element doesn't capture the event\n // that caused the focus trap activation.\n state.delayInitialFocusTimer = config.delayInitialFocus\n ? delay(function () {\n tryFocus(getInitialFocusNode());\n })\n : tryFocus(getInitialFocusNode());\n\n doc.addEventListener('focusin', checkFocusIn, true);\n doc.addEventListener('mousedown', checkPointerDown, {\n capture: true,\n passive: false,\n });\n doc.addEventListener('touchstart', checkPointerDown, {\n capture: true,\n passive: false,\n });\n doc.addEventListener('click', checkClick, {\n capture: true,\n passive: false,\n });\n doc.addEventListener('keydown', checkKey, {\n capture: true,\n passive: false,\n });\n\n return trap;\n };\n\n const removeListeners = function () {\n if (!state.active) {\n return;\n }\n\n doc.removeEventListener('focusin', checkFocusIn, true);\n doc.removeEventListener('mousedown', checkPointerDown, true);\n doc.removeEventListener('touchstart', checkPointerDown, true);\n doc.removeEventListener('click', checkClick, true);\n doc.removeEventListener('keydown', checkKey, true);\n\n return trap;\n };\n\n //\n // TRAP DEFINITION\n //\n\n trap = {\n activate(activateOptions) {\n if (state.active) {\n return this;\n }\n\n const onActivate = getOption(activateOptions, 'onActivate');\n const onPostActivate = getOption(activateOptions, 'onPostActivate');\n const checkCanFocusTrap = getOption(activateOptions, 'checkCanFocusTrap');\n\n if (!checkCanFocusTrap) {\n updateTabbableNodes();\n }\n\n state.active = true;\n state.paused = false;\n state.nodeFocusedBeforeActivation = doc.activeElement;\n\n if (onActivate) {\n onActivate();\n }\n\n const finishActivation = () => {\n if (checkCanFocusTrap) {\n updateTabbableNodes();\n }\n addListeners();\n if (onPostActivate) {\n onPostActivate();\n }\n };\n\n if (checkCanFocusTrap) {\n checkCanFocusTrap(state.containers.concat()).then(\n finishActivation,\n finishActivation\n );\n return this;\n }\n\n finishActivation();\n return this;\n },\n\n deactivate(deactivateOptions) {\n if (!state.active) {\n return this;\n }\n\n clearTimeout(state.delayInitialFocusTimer); // noop if undefined\n state.delayInitialFocusTimer = undefined;\n\n removeListeners();\n state.active = false;\n state.paused = false;\n\n activeFocusTraps.deactivateTrap(trap);\n\n const onDeactivate = getOption(deactivateOptions, 'onDeactivate');\n const onPostDeactivate = getOption(deactivateOptions, 'onPostDeactivate');\n const checkCanReturnFocus = getOption(\n deactivateOptions,\n 'checkCanReturnFocus'\n );\n\n if (onDeactivate) {\n onDeactivate();\n }\n\n const returnFocus = getOption(\n deactivateOptions,\n 'returnFocus',\n 'returnFocusOnDeactivate'\n );\n\n const finishDeactivation = () => {\n delay(() => {\n if (returnFocus) {\n tryFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation));\n }\n if (onPostDeactivate) {\n onPostDeactivate();\n }\n });\n };\n\n if (returnFocus && checkCanReturnFocus) {\n checkCanReturnFocus(\n getReturnFocusNode(state.nodeFocusedBeforeActivation)\n ).then(finishDeactivation, finishDeactivation);\n return this;\n }\n\n finishDeactivation();\n return this;\n },\n\n pause() {\n if (state.paused || !state.active) {\n return this;\n }\n\n state.paused = true;\n removeListeners();\n\n return this;\n },\n\n unpause() {\n if (!state.paused || !state.active) {\n return this;\n }\n\n state.paused = false;\n updateTabbableNodes();\n addListeners();\n\n return this;\n },\n\n updateContainerElements(containerElements) {\n const elementsAsArray = [].concat(containerElements).filter(Boolean);\n\n state.containers = elementsAsArray.map((element) =>\n typeof element === 'string' ? doc.querySelector(element) : element\n );\n\n if (state.active) {\n updateTabbableNodes();\n }\n\n return this;\n },\n };\n\n // initialize container elements\n trap.updateContainerElements(elements);\n\n return trap;\n};\n\nexport { createFocusTrap };\n"],"names":["trapQueue","activeFocusTraps","activateTrap","trap","length","activeTrap","pause","trapIndex","indexOf","splice","push","deactivateTrap","unpause","delay","fn","setTimeout","findIndex","arr","idx","every","value","i","valueOrHandler","params","elements","userOptions","doc","document","config","returnFocusOnDeactivate","escapeDeactivates","delayInitialFocus","state","containers","tabbableGroups","nodeFocusedBeforeActivation","mostRecentlyFocusedNode","active","paused","delayInitialFocusTimer","undefined","getOption","configOverrideOptions","optionName","configOptionName","containersContain","element","some","container","contains","getNodeForOption","optionValue","node","querySelector","Error","getInitialFocusNode","activeElement","firstTabbableGroup","firstTabbableNode","updateTabbableNodes","map","tabbableNodes","tabbable","lastTabbableNode","filter","group","tryFocus","focus","preventScroll","tagName","toLowerCase","select","isSelectableInput","getReturnFocusNode","previousActiveElement","checkPointerDown","e","target","clickOutsideDeactivates","deactivate","returnFocus","isFocusable","allowOutsideClick","preventDefault","checkFocusIn","targetContained","Document","stopImmediatePropagation","checkKey","key","keyCode","isEscapeEvent","isTabEvent","destinationNode","containerIndex","shiftKey","startOfGroupIndex","destinationGroupIndex","lastOfGroupIndex","checkTab","checkClick","addListeners","addEventListener","capture","passive","removeListeners","removeEventListener","activate","activateOptions","this","onActivate","onPostActivate","checkCanFocusTrap","finishActivation","concat","then","deactivateOptions","clearTimeout","onDeactivate","onPostDeactivate","checkCanReturnF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AmB,WACnBD,GACFrD,IAEF0C,IACIU,GACFA,YAIAC,GACFA,EAAkBhF,EAAMC,WAAWiF,UAAUC,KAC3CF,EACAA,GAEKJ,OAGTI,IACOJ,OAGT9B,oBAAWqC,OACJpF,EAAMK,cACFwE,KAGTQ,aAAarF,EAAMO,wBACnBP,EAAMO,4BAAyBC,EAE/BiE,IACAzE,EAAMK,QAAS,EACfL,EAAMM,QAAS,EAEfrC,EAAiBU,eAAeR,OAE1BmH,EAAe7E,EAAU2E,EAAmB,gBAC5CG,EAAmB9E,EAAU2E,EAAmB,oBAChDI,EAAsB/E,EAC1B2E,EACA,uBAGEE,GACFA,QAGItC,EAAcvC,EAClB2E,EACA,cACA,2BAGIK,EAAqB,WACzB5G,GAAM,WACAmE,GACFd,EAASO,EAAmBzC,EAAMG,8BAEhCoF,GACFA,eAKFvC,GAAewC,GACjBA,EACE/C,EAAmBzC,EAAMG,8BACzBgF,KAAKM,EAAoBA,GACpBZ,OAGTY,IACOZ,OAGTvG,wBACM0B,EAAMM,SAAWN,EAAMK,SAI3BL,EAAMM,QAAS,EACfmE,KAJSI,MASXjG,0BACOoB,EAAMM,QAAWN,EAAMK,QAI5BL,EAAMM,QAAS,EACfqB,IACA0C,IAEOQ,MAPEA,MAUXa,iCAAwBC,OAChBC,EAAkB,GAAGV,OAAOS,GAAmB3D,OAAO6D,gBAE5D7F,EAAMC,WAAa2F,EAAgBhE,KAAI,SAACd,SACnB,iBAAZA,EAAuBpB,EAAI2B,cAAcP,GAAWA,KAGzDd,EAAMK,QACRsB,IAGKkD,QAKNa,wBAAwBlG,GAEtBrB"}
|
|
1
|
+
{"version":3,"file":"focus-trap.min.js","sources":["../index.js"],"sourcesContent":["import { tabbable, focusable, isFocusable, isTabbable } from 'tabbable';\n\nconst activeFocusTraps = (function () {\n const trapQueue = [];\n return {\n activateTrap(trap) {\n if (trapQueue.length > 0) {\n const activeTrap = trapQueue[trapQueue.length - 1];\n if (activeTrap !== trap) {\n activeTrap.pause();\n }\n }\n\n const trapIndex = trapQueue.indexOf(trap);\n if (trapIndex === -1) {\n trapQueue.push(trap);\n } else {\n // move this existing trap to the front of the queue\n trapQueue.splice(trapIndex, 1);\n trapQueue.push(trap);\n }\n },\n\n deactivateTrap(trap) {\n const trapIndex = trapQueue.indexOf(trap);\n if (trapIndex !== -1) {\n trapQueue.splice(trapIndex, 1);\n }\n\n if (trapQueue.length > 0) {\n trapQueue[trapQueue.length - 1].unpause();\n }\n },\n };\n})();\n\nconst isSelectableInput = function (node) {\n return (\n node.tagName &&\n node.tagName.toLowerCase() === 'input' &&\n typeof node.select === 'function'\n );\n};\n\nconst isEscapeEvent = function (e) {\n return e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27;\n};\n\nconst isTabEvent = function (e) {\n return e.key === 'Tab' || e.keyCode === 9;\n};\n\nconst delay = function (fn) {\n return setTimeout(fn, 0);\n};\n\n// Array.find/findIndex() are not supported on IE; this replicates enough\n// of Array.findIndex() for our needs\nconst findIndex = function (arr, fn) {\n let idx = -1;\n\n arr.every(function (value, i) {\n if (fn(value)) {\n idx = i;\n return false; // break\n }\n\n return true; // next\n });\n\n return idx;\n};\n\n/**\n * Get an option's value when it could be a plain value, or a handler that provides\n * the value.\n * @param {*} value Option's value to check.\n * @param {...*} [params] Any parameters to pass to the handler, if `value` is a function.\n * @returns {*} The `value`, or the handler's returned value.\n */\nconst valueOrHandler = function (value, ...params) {\n return typeof value === 'function' ? value(...params) : value;\n};\n\nconst getActualTarget = function (event) {\n // NOTE: If the trap is _inside_ a shadow DOM, event.target will always be the\n // shadow host. However, event.target.composedPath() will be an array of\n // nodes \"clicked\" from inner-most (the actual element inside the shadow) to\n // outer-most (the host HTML document). If we have access to composedPath(),\n // then use its first element; otherwise, fall back to event.target (and\n // this only works for an _open_ shadow DOM; otherwise,\n // composedPath()[0] === event.target always).\n return event.target.shadowRoot && typeof event.composedPath === 'function'\n ? event.composedPath()[0]\n : event.target;\n};\n\nconst createFocusTrap = function (elements, userOptions) {\n // SSR: a live trap shouldn't be created in this type of environment so this\n // should be safe code to execute if the `document` option isn't specified\n const doc = userOptions?.document || document;\n\n const config = {\n returnFocusOnDeactivate: true,\n escapeDeactivates: true,\n delayInitialFocus: true,\n ...userOptions,\n };\n\n const state = {\n // @type {Array<HTMLElement>}\n containers: [],\n\n // list of objects identifying the first and last tabbable nodes in all containers/groups in\n // the trap\n // NOTE: it's possible that a group has no tabbable nodes if nodes get removed while the trap\n // is active, but the trap should never get to a state where there isn't at least one group\n // with at least one tabbable node in it (that would lead to an error condition that would\n // result in an error being thrown)\n // @type {Array<{\n // container: HTMLElement,\n // firstTabbableNode: HTMLElement|null,\n // lastTabbableNode: HTMLElement|null,\n // nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined\n // }>}\n tabbableGroups: [],\n\n nodeFocusedBeforeActivation: null,\n mostRecentlyFocusedNode: null,\n active: false,\n paused: false,\n\n // timer ID for when delayInitialFocus is true and initial focus in this trap\n // has been delayed during activation\n delayInitialFocusTimer: undefined,\n };\n\n let trap; // eslint-disable-line prefer-const -- some private functions reference it, and its methods reference private functions, so we must declare here and define later\n\n const getOption = (configOverrideOptions, optionName, configOptionName) => {\n return configOverrideOptions &&\n configOverrideOptions[optionName] !== undefined\n ? configOverrideOptions[optionName]\n : config[configOptionName || optionName];\n };\n\n const containersContain = function (element) {\n return !!(\n element &&\n state.containers.some((container) => container.contains(element))\n );\n };\n\n /**\n * Gets the node for the given option, which is expected to be an option that\n * can be either a DOM node, a string that is a selector to get a node, `false`\n * (if a node is explicitly NOT given), or a function that returns any of these\n * values.\n * @param {string} optionName\n * @returns {undefined | false | HTMLElement | SVGElement} Returns\n * `undefined` if the option is not specified; `false` if the option\n * resolved to `false` (node explicitly not given); otherwise, the resolved\n * DOM node.\n * @throws {Error} If the option is set, not `false`, and is not, or does not\n * resolve to a node.\n */\n const getNodeForOption = function (optionName, ...params) {\n let optionValue = config[optionName];\n\n if (typeof optionValue === 'function') {\n optionValue = optionValue(...params);\n }\n\n if (!optionValue) {\n if (optionValue === undefined || optionValue === false) {\n return optionValue;\n }\n // else, empty string (invalid), null (invalid), 0 (invalid)\n\n throw new Error(\n `\\`${optionName}\\` was specified but was not a node, or did not return a node`\n );\n }\n\n let node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point\n\n if (typeof optionValue === 'string') {\n node = doc.querySelector(optionValue); // resolve to node, or null if fails\n if (!node) {\n throw new Error(\n `\\`${optionName}\\` as selector refers to no known node`\n );\n }\n }\n\n return node;\n };\n\n const getInitialFocusNode = function () {\n let node = getNodeForOption('initialFocus');\n\n // false explicitly indicates we want no initialFocus at all\n if (node === false) {\n return false;\n }\n\n if (node === undefined) {\n // option not specified: use fallback options\n if (containersContain(doc.activeElement)) {\n node = doc.activeElement;\n } else {\n const firstTabbableGroup = state.tabbableGroups[0];\n const firstTabbableNode =\n firstTabbableGroup && firstTabbableGroup.firstTabbableNode;\n\n // NOTE: `fallbackFocus` option function cannot return `false` (not supported)\n node = firstTabbableNode || getNodeForOption('fallbackFocus');\n }\n }\n\n if (!node) {\n throw new Error(\n 'Your focus-trap needs to have at least one focusable element'\n );\n }\n\n return node;\n };\n\n const updateTabbableNodes = function () {\n state.tabbableGroups = state.containers\n .map((container) => {\n const tabbableNodes = tabbable(container);\n\n // NOTE: if we have tabbable nodes, we must have focusable nodes; focusable nodes\n // are a superset of tabbable nodes\n const focusableNodes = focusable(container);\n\n if (tabbableNodes.length > 0) {\n return {\n container,\n firstTabbableNode: tabbableNodes[0],\n lastTabbableNode: tabbableNodes[tabbableNodes.length - 1],\n\n /**\n * Finds the __tabbable__ node that follows the given node in the specified direction,\n * in this container, if any.\n * @param {HTMLElement} node\n * @param {boolean} [forward] True if going in forward tab order; false if going\n * in reverse.\n * @returns {HTMLElement|undefined} The next tabbable node, if any.\n */\n nextTabbableNode(node, forward = true) {\n // NOTE: If tabindex is positive (in order to manipulate the tab order separate\n // from the DOM order), this __will not work__ because the list of focusableNodes,\n // while it contains tabbable nodes, does not sort its nodes in any order other\n // than DOM order, because it can't: Where would you place focusable (but not\n // tabbable) nodes in that order? They have no order, because they aren't tabbale...\n // Support for positive tabindex is already broken and hard to manage (possibly\n // not supportable, TBD), so this isn't going to make things worse than they\n // already are, and at least makes things better for the majority of cases where\n // tabindex is either 0/unset or negative.\n // FYI, positive tabindex issue: https://github.com/focus-trap/focus-trap/issues/375\n const nodeIdx = focusableNodes.findIndex((n) => n === node);\n if (forward) {\n return focusableNodes\n .slice(nodeIdx + 1)\n .find((n) => isTabbable(n));\n }\n return focusableNodes\n .slice(0, nodeIdx)\n .reverse()\n .find((n) => isTabbable(n));\n },\n };\n }\n\n return undefined;\n })\n .filter((group) => !!group); // remove groups with no tabbable nodes\n\n // throw if no groups have tabbable nodes and we don't have a fallback focus node either\n if (\n state.tabbableGroups.length <= 0 &&\n !getNodeForOption('fallbackFocus') // returning false not supported for this option\n ) {\n throw new Error(\n 'Your focus-trap must have at least one container with at least one tabbable node in it at all times'\n );\n }\n };\n\n const tryFocus = function (node) {\n if (node === false) {\n return;\n }\n\n if (node === doc.activeElement) {\n return;\n }\n\n if (!node || !node.focus) {\n tryFocus(getInitialFocusNode());\n return;\n }\n\n node.focus({ preventScroll: !!config.preventScroll });\n state.mostRecentlyFocusedNode = node;\n\n if (isSelectableInput(node)) {\n node.select();\n }\n };\n\n const getReturnFocusNode = function (previousActiveElement) {\n const node = getNodeForOption('setReturnFocus', previousActiveElement);\n return node ? node : node === false ? false : previousActiveElement;\n };\n\n // This needs to be done on mousedown and touchstart instead of click\n // so that it precedes the focus event.\n const checkPointerDown = function (e) {\n const target = getActualTarget(e);\n\n if (containersContain(target)) {\n // allow the click since it ocurred inside the trap\n return;\n }\n\n if (valueOrHandler(config.clickOutsideDeactivates, e)) {\n // immediately deactivate the trap\n trap.deactivate({\n // if, on deactivation, we should return focus to the node originally-focused\n // when the trap was activated (or the configured `setReturnFocus` node),\n // then assume it's also OK to return focus to the outside node that was\n // just clicked, causing deactivation, as long as that node is focusable;\n // if it isn't focusable, then return focus to the original node focused\n // on activation (or the configured `setReturnFocus` node)\n // NOTE: by setting `returnFocus: false`, deactivate() will do nothing,\n // which will result in the outside click setting focus to the node\n // that was clicked, whether it's focusable or not; by setting\n // `returnFocus: true`, we'll attempt to re-focus the node originally-focused\n // on activation (or the configured `setReturnFocus` node)\n returnFocus: config.returnFocusOnDeactivate && !isFocusable(target),\n });\n return;\n }\n\n // This is needed for mobile devices.\n // (If we'll only let `click` events through,\n // then on mobile they will be blocked anyways if `touchstart` is blocked.)\n if (valueOrHandler(config.allowOutsideClick, e)) {\n // allow the click outside the trap to take place\n return;\n }\n\n // otherwise, prevent the click\n e.preventDefault();\n };\n\n // In case focus escapes the trap for some strange reason, pull it back in.\n const checkFocusIn = function (e) {\n const target = getActualTarget(e);\n const targetContained = containersContain(target);\n\n // In Firefox when you Tab out of an iframe the Document is briefly focused.\n if (targetContained || target instanceof Document) {\n if (targetContained) {\n state.mostRecentlyFocusedNode = target;\n }\n } else {\n // escaped! pull it back in to where it just left\n e.stopImmediatePropagation();\n tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());\n }\n };\n\n // Hijack Tab events on the first and last focusable nodes of the trap,\n // in order to prevent focus from escaping. If it escapes for even a\n // moment it can end up scrolling the page and causing confusion so we\n // kind of need to capture the action at the keydown phase.\n const checkTab = function (e) {\n const target = getActualTarget(e);\n updateTabbableNodes();\n\n let destinationNode = null;\n\n if (state.tabbableGroups.length > 0) {\n // make sure the target is actually contained in a group\n // NOTE: the target may also be the container itself if it's focusable\n // with tabIndex='-1' and was given initial focus\n const containerIndex = findIndex(state.tabbableGroups, ({ container }) =>\n container.contains(target)\n );\n const containerGroup =\n containerIndex >= 0 ? state.tabbableGroups[containerIndex] : undefined;\n\n if (containerIndex < 0) {\n // target not found in any group: quite possible focus has escaped the trap,\n // so bring it back in to...\n if (e.shiftKey) {\n // ...the last node in the last group\n destinationNode =\n state.tabbableGroups[state.tabbableGroups.length - 1]\n .lastTabbableNode;\n } else {\n // ...the first node in the first group\n destinationNode = state.tabbableGroups[0].firstTabbableNode;\n }\n } else if (e.shiftKey) {\n // REVERSE\n\n // is the target the first tabbable node in a group?\n let startOfGroupIndex = findIndex(\n state.tabbableGroups,\n ({ firstTabbableNode }) => target === firstTabbableNode\n );\n\n if (\n startOfGroupIndex < 0 &&\n (containerGroup.container === target ||\n (isFocusable(target) &&\n !isTabbable(target) &&\n !containerGroup.nextTabbableNode(target, false)))\n ) {\n // an exception case where the target is either the container itself, or\n // a non-tabbable node that was given focus (i.e. tabindex is negative\n // and user clicked on it or node was programmatically given focus)\n // and is not followed by any other tabbable node, in which\n // case, we should handle shift+tab as if focus were on the container's\n // first tabbable node, and go to the last tabbable node of the LAST group\n startOfGroupIndex = containerIndex;\n }\n\n if (startOfGroupIndex >= 0) {\n // YES: then shift+tab should go to the last tabbable node in the\n // previous group (and wrap around to the last tabbable node of\n // the LAST group if it's the first tabbable node of the FIRST group)\n const destinationGroupIndex =\n startOfGroupIndex === 0\n ? state.tabbableGroups.length - 1\n : startOfGroupIndex - 1;\n\n const destinationGroup = state.tabbableGroups[destinationGroupIndex];\n destinationNode = destinationGroup.lastTabbableNode;\n }\n } else {\n // FORWARD\n\n // is the target the last tabbable node in a group?\n let lastOfGroupIndex = findIndex(\n state.tabbableGroups,\n ({ lastTabbableNode }) => target === lastTabbableNode\n );\n\n if (\n lastOfGroupIndex < 0 &&\n (containerGroup.container === target ||\n (isFocusable(target) &&\n !isTabbable(target) &&\n !containerGroup.nextTabbableNode(target)))\n ) {\n // an exception case where the target is the container itself, or\n // a non-tabbable node that was given focus (i.e. tabindex is negative\n // and user clicked on it or node was programmatically given focus)\n // and is not followed by any other tabbable node, in which\n // case, we should handle tab as if focus were on the container's\n // last tabbable node, and go to the first tabbable node of the FIRST group\n lastOfGroupIndex = containerIndex;\n }\n\n if (lastOfGroupIndex >= 0) {\n // YES: then tab should go to the first tabbable node in the next\n // group (and wrap around to the first tabbable node of the FIRST\n // group if it's the last tabbable node of the LAST group)\n const destinationGroupIndex =\n lastOfGroupIndex === state.tabbableGroups.length - 1\n ? 0\n : lastOfGroupIndex + 1;\n\n const destinationGroup = state.tabbableGroups[destinationGroupIndex];\n destinationNode = destinationGroup.firstTabbableNode;\n }\n }\n } else {\n // NOTE: the fallbackFocus option does not support returning false to opt-out\n destinationNode = getNodeForOption('fallbackFocus');\n }\n\n if (destinationNode) {\n e.preventDefault();\n tryFocus(destinationNode);\n }\n // else, let the browser take care of [shift+]tab and move the focus\n };\n\n const checkKey = function (e) {\n if (\n isEscapeEvent(e) &&\n valueOrHandler(config.escapeDeactivates, e) !== false\n ) {\n e.preventDefault();\n trap.deactivate();\n return;\n }\n\n if (isTabEvent(e)) {\n checkTab(e);\n return;\n }\n };\n\n const checkClick = function (e) {\n if (valueOrHandler(config.clickOutsideDeactivates, e)) {\n return;\n }\n\n const target = getActualTarget(e);\n\n if (containersContain(target)) {\n return;\n }\n\n if (valueOrHandler(config.allowOutsideClick, e)) {\n return;\n }\n\n e.preventDefault();\n e.stopImmediatePropagation();\n };\n\n //\n // EVENT LISTENERS\n //\n\n const addListeners = function () {\n if (!state.active) {\n return;\n }\n\n // There can be only one listening focus trap at a time\n activeFocusTraps.activateTrap(trap);\n\n // Delay ensures that the focused element doesn't capture the event\n // that caused the focus trap activation.\n state.delayInitialFocusTimer = config.delayInitialFocus\n ? delay(function () {\n tryFocus(getInitialFocusNode());\n })\n : tryFocus(getInitialFocusNode());\n\n doc.addEventListener('focusin', checkFocusIn, true);\n doc.addEventListener('mousedown', checkPointerDown, {\n capture: true,\n passive: false,\n });\n doc.addEventListener('touchstart', checkPointerDown, {\n capture: true,\n passive: false,\n });\n doc.addEventListener('click', checkClick, {\n capture: true,\n passive: false,\n });\n doc.addEventListener('keydown', checkKey, {\n capture: true,\n passive: false,\n });\n\n return trap;\n };\n\n const removeListeners = function () {\n if (!state.active) {\n return;\n }\n\n doc.removeEventListener('focusin', checkFocusIn, true);\n doc.removeEventListener('mousedown', checkPointerDown, true);\n doc.removeEventListener('touchstart', checkPointerDown, true);\n doc.removeEventListener('click', checkClick, true);\n doc.removeEventListener('keydown', checkKey, true);\n\n return trap;\n };\n\n //\n // TRAP DEFINITION\n //\n\n trap = {\n activate(activateOptions) {\n if (state.active) {\n return this;\n }\n\n const onActivate = getOption(activateOptions, 'onActivate');\n const onPostActivate = getOption(activateOptions, 'onPostActivate');\n const checkCanFocusTrap = getOption(activateOptions, 'checkCanFocusTrap');\n\n if (!checkCanFocusTrap) {\n updateTabbableNodes();\n }\n\n state.active = true;\n state.paused = false;\n state.nodeFocusedBeforeActivation = doc.activeElement;\n\n if (onActivate) {\n onActivate();\n }\n\n const finishActivation = () => {\n if (checkCanFocusTrap) {\n updateTabbableNodes();\n }\n addListeners();\n if (onPostActivate) {\n onPostActivate();\n }\n };\n\n if (checkCanFocusTrap) {\n checkCanFocusTrap(state.containers.concat()).then(\n finishActivation,\n finishActivation\n );\n return this;\n }\n\n finishActivation();\n return this;\n },\n\n deactivate(deactivateOptions) {\n if (!state.active) {\n return this;\n }\n\n clearTimeout(state.delayInitialFocusTimer); // noop if undefined\n state.delayInitialFocusTimer = undefined;\n\n removeListeners();\n state.active = false;\n state.paused = false;\n\n activeFocusTraps.deactivateTrap(trap);\n\n const onDeactivate = getOption(deactivateOptions, 'onDeactivate');\n const onPostDeactivate = getOption(deactivateOptions, 'onPostDeactivate');\n const checkCanReturnFocus = getOption(\n deactivateOptions,\n 'checkCanReturnFocus'\n );\n\n if (onDeactivate) {\n onDeactivate();\n }\n\n const returnFocus = getOption(\n deactivateOptions,\n 'returnFocus',\n 'returnFocusOnDeactivate'\n );\n\n const finishDeactivation = () => {\n delay(() => {\n if (returnFocus) {\n tryFocus(getReturnFocusNode(state.nodeFocusedBeforeActivation));\n }\n if (onPostDeactivate) {\n onPostDeactivate();\n }\n });\n };\n\n if (returnFocus && checkCanReturnFocus) {\n checkCanReturnFocus(\n getReturnFocusNode(state.nodeFocusedBeforeActivation)\n ).then(finishDeactivation, finishDeactivation);\n return this;\n }\n\n finishDeactivation();\n return this;\n },\n\n pause() {\n if (state.paused || !state.active) {\n return this;\n }\n\n state.paused = true;\n removeListeners();\n\n return this;\n },\n\n unpause() {\n if (!state.paused || !state.active) {\n return this;\n }\n\n state.paused = false;\n updateTabbableNodes();\n addListeners();\n\n return this;\n },\n\n updateContainerElements(containerElements) {\n const elementsAsArray = [].concat(containerElements).filter(Boolean);\n\n state.containers = elementsAsArray.map((element) =>\n typeof element === 'string' ? doc.querySelector(element) : element\n );\n\n if (state.active) {\n updateTabbableNodes();\n }\n\n return this;\n },\n };\n\n // initialize container elements\n trap.updateContainerElements(elements);\n\n return trap;\n};\n\nexport { createFocusTrap };\n"],"names":["trapQueue","activeFocusTraps","activateTrap","trap","length","activeTrap","pause","trapIndex","indexOf","splice","push","deactivateTrap","unpause","delay","fn","setTimeout","findIndex","arr","idx","every","value","i","valueOrHandler","params","getActualTarget","event","target","shadowRoot","composedPath","elements","userOptions","doc","document","config","returnFocusOnDeactivate","escapeDeactivates","delayInitialFocus","state","containers","tabbableGroups","nodeFocusedBeforeActivation","mostRecentlyFocusedNode","active","paused","delayInitialFocusTimer","undefined","getOption","configOverrideOptions","optionName","configOptionName","containersContain","element","some","container","contains","getNodeForOption","optionValue","Error","node","querySelector","getInitialFocusNode","activeElement","firstTabbableGroup","firstTabbableNode","updateTabbableNodes","map","tabbableNodes","tabbable","focusableNodes","focusable","lastTabbableNode","nextTabbableNode","forward","nodeIdx","n","slice","find","isTabbable","reverse","filter","group","tryFocus","focus","preventScroll","tagName","toLowerCase","select","isSelectableInput","getReturnFocusNode","previousActiveElement","checkPointerDown","e","clickOutsideDeactivates","deactivate","returnFocus","isFocusable","allowOutsideClick","preventDefault","checkFocusIn","targetContained","Document","stopImmediatePropagation","checkKey","key","keyCode","isEscapeEvent","isTabEvent","destinationNode","containerIndex","containerGroup","shiftKey","startOfGroupIndex","destinationGroupIndex","lastOfGroupIndex","checkTab","checkClick","addListeners","addEventListener","capture","passive","removeListeners","removeEventListener","activate","activateOptions","this","onActivate","onPostActivate","checkCanFocusTrap","finishActivation","concat","then","deactivateOptions","clearTimeout","onDeactivate","onPostDeactivate","checkCanReturnFocus","finishDeactivation","updateContainerElements","containerElements","elementsAsArray","Boolean"],"mappings":";;;;obAEA,IACQA,EADFC,GACED,EAAY,GACX,CACLE,sBAAaC,MACPH,EAAUI,OAAS,EAAG,KAClBC,EAAaL,EAAUA,EAAUI,OAAS,GAC5CC,IAAeF,GACjBE,EAAWC,YAITC,EAAYP,EAAUQ,QAAQL,IACjB,IAAfI,GAIFP,EAAUS,OAAOF,EAAW,GAH5BP,EAAUU,KAAKP,IAQnBQ,wBAAeR,OACPI,EAAYP,EAAUQ,QAAQL,IACjB,IAAfI,GACFP,EAAUS,OAAOF,EAAW,GAG1BP,EAAUI,OAAS,GACrBJ,EAAUA,EAAUI,OAAS,GAAGQ,aAsBlCC,EAAQ,SAAUC,UACfC,WAAWD,EAAI,IAKlBE,EAAY,SAAUC,EAAKH,OAC3BI,GAAO,SAEXD,EAAIE,OAAM,SAAUC,EAAOC,UACrBP,EAAGM,KACLF,EAAMG,GACC,MAMJH,GAUHI,EAAiB,SAAUF,8BAAUG,mCAAAA,0BACjB,mBAAVH,EAAuBA,eAASG,GAAUH,GAGpDI,EAAkB,SAAUC,UAQzBA,EAAMC,OAAOC,YAA4C,mBAAvBF,EAAMG,aAC3CH,EAAMG,eAAe,GACrBH,EAAMC,gCAGY,SAAUG,EAAUC,OAwCtC3B,EArCE4B,GAAMD,MAAAA,SAAAA,EAAaE,WAAYA,SAE/BC,mWACJC,yBAAyB,EACzBC,mBAAmB,EACnBC,mBAAmB,GAChBN,GAGCO,EAAQ,CAEZC,WAAY,GAcZC,eAAgB,GAEhBC,4BAA6B,KAC7BC,wBAAyB,KACzBC,QAAQ,EACRC,QAAQ,EAIRC,4BAAwBC,GAKpBC,EAAY,SAACC,EAAuBC,EAAYC,UAC7CF,QACiCF,IAAtCE,EAAsBC,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O,EAAmBnD,EAAMG,8BAEhC8F,GACFA,eAKFxC,GAAeyC,GACjBA,EACE/C,EAAmBnD,EAAMG,8BACzB0F,KAAKM,EAAoBA,GACpBZ,OAGTY,IACOZ,OAGTtH,wBACM+B,EAAMM,SAAWN,EAAMK,SAI3BL,EAAMM,QAAS,EACf6E,KAJSI,MASXhH,0BACOyB,EAAMM,QAAWN,EAAMK,QAI5BL,EAAMM,QAAS,EACfqB,IACAoD,IAEOQ,MAPEA,MAUXa,iCAAwBC,OAChBC,EAAkB,GAAGV,OAAOS,GAAmB3D,OAAO6D,gBAE5DvG,EAAMC,WAAaqG,EAAgB1E,KAAI,SAACd,SACnB,iBAAZA,EAAuBpB,EAAI4B,cAAcR,GAAWA,KAGzDd,EAAMK,QACRsB,IAGK4D,QAKNa,wBAAwB5G,GAEtB1B"}
|
package/dist/focus-trap.umd.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* focus-trap 6.
|
|
2
|
+
* focus-trap 6.7.3
|
|
3
3
|
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
|
|
4
4
|
*/
|
|
5
5
|
(function (global, factory) {
|
|
@@ -10,22 +10,17 @@
|
|
|
10
10
|
var exports = global.focusTrap = {};
|
|
11
11
|
factory(exports, global.tabbable);
|
|
12
12
|
exports.noConflict = function () { global.focusTrap = current; return exports; };
|
|
13
|
-
}())
|
|
14
|
-
}(this, (function (exports, tabbable) { 'use strict';
|
|
13
|
+
})());
|
|
14
|
+
})(this, (function (exports, tabbable) { 'use strict';
|
|
15
15
|
|
|
16
16
|
function ownKeys(object, enumerableOnly) {
|
|
17
17
|
var keys = Object.keys(object);
|
|
18
18
|
|
|
19
19
|
if (Object.getOwnPropertySymbols) {
|
|
20
20
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
keys.push.apply(keys, symbols);
|
|
21
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
22
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
23
|
+
})), keys.push.apply(keys, symbols);
|
|
29
24
|
}
|
|
30
25
|
|
|
31
26
|
return keys;
|
|
@@ -33,19 +28,12 @@
|
|
|
33
28
|
|
|
34
29
|
function _objectSpread2(target) {
|
|
35
30
|
for (var i = 1; i < arguments.length; i++) {
|
|
36
|
-
var source = arguments[i]
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
} else if (Object.getOwnPropertyDescriptors) {
|
|
43
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
44
|
-
} else {
|
|
45
|
-
ownKeys(Object(source)).forEach(function (key) {
|
|
46
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
47
|
-
});
|
|
48
|
-
}
|
|
31
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
32
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
33
|
+
_defineProperty(target, key, source[key]);
|
|
34
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
35
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
36
|
+
});
|
|
49
37
|
}
|
|
50
38
|
|
|
51
39
|
return target;
|
|
@@ -149,8 +137,21 @@
|
|
|
149
137
|
return typeof value === 'function' ? value.apply(void 0, params) : value;
|
|
150
138
|
};
|
|
151
139
|
|
|
140
|
+
var getActualTarget = function getActualTarget(event) {
|
|
141
|
+
// NOTE: If the trap is _inside_ a shadow DOM, event.target will always be the
|
|
142
|
+
// shadow host. However, event.target.composedPath() will be an array of
|
|
143
|
+
// nodes "clicked" from inner-most (the actual element inside the shadow) to
|
|
144
|
+
// outer-most (the host HTML document). If we have access to composedPath(),
|
|
145
|
+
// then use its first element; otherwise, fall back to event.target (and
|
|
146
|
+
// this only works for an _open_ shadow DOM; otherwise,
|
|
147
|
+
// composedPath()[0] === event.target always).
|
|
148
|
+
return event.target.shadowRoot && typeof event.composedPath === 'function' ? event.composedPath()[0] : event.target;
|
|
149
|
+
};
|
|
150
|
+
|
|
152
151
|
var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
153
|
-
|
|
152
|
+
// SSR: a live trap shouldn't be created in this type of environment so this
|
|
153
|
+
// should be safe code to execute if the `document` option isn't specified
|
|
154
|
+
var doc = (userOptions === null || userOptions === void 0 ? void 0 : userOptions.document) || document;
|
|
154
155
|
|
|
155
156
|
var config = _objectSpread2({
|
|
156
157
|
returnFocusOnDeactivate: true,
|
|
@@ -167,7 +168,12 @@
|
|
|
167
168
|
// is active, but the trap should never get to a state where there isn't at least one group
|
|
168
169
|
// with at least one tabbable node in it (that would lead to an error condition that would
|
|
169
170
|
// result in an error being thrown)
|
|
170
|
-
// @type {Array<{
|
|
171
|
+
// @type {Array<{
|
|
172
|
+
// container: HTMLElement,
|
|
173
|
+
// firstTabbableNode: HTMLElement|null,
|
|
174
|
+
// lastTabbableNode: HTMLElement|null,
|
|
175
|
+
// nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined
|
|
176
|
+
// }>}
|
|
171
177
|
tabbableGroups: [],
|
|
172
178
|
nodeFocusedBeforeActivation: null,
|
|
173
179
|
mostRecentlyFocusedNode: null,
|
|
@@ -184,33 +190,52 @@
|
|
|
184
190
|
};
|
|
185
191
|
|
|
186
192
|
var containersContain = function containersContain(element) {
|
|
187
|
-
return state.containers.some(function (container) {
|
|
193
|
+
return !!(element && state.containers.some(function (container) {
|
|
188
194
|
return container.contains(element);
|
|
189
|
-
});
|
|
195
|
+
}));
|
|
190
196
|
};
|
|
197
|
+
/**
|
|
198
|
+
* Gets the node for the given option, which is expected to be an option that
|
|
199
|
+
* can be either a DOM node, a string that is a selector to get a node, `false`
|
|
200
|
+
* (if a node is explicitly NOT given), or a function that returns any of these
|
|
201
|
+
* values.
|
|
202
|
+
* @param {string} optionName
|
|
203
|
+
* @returns {undefined | false | HTMLElement | SVGElement} Returns
|
|
204
|
+
* `undefined` if the option is not specified; `false` if the option
|
|
205
|
+
* resolved to `false` (node explicitly not given); otherwise, the resolved
|
|
206
|
+
* DOM node.
|
|
207
|
+
* @throws {Error} If the option is set, not `false`, and is not, or does not
|
|
208
|
+
* resolve to a node.
|
|
209
|
+
*/
|
|
210
|
+
|
|
191
211
|
|
|
192
212
|
var getNodeForOption = function getNodeForOption(optionName) {
|
|
193
213
|
var optionValue = config[optionName];
|
|
194
214
|
|
|
195
|
-
if (
|
|
196
|
-
|
|
215
|
+
if (typeof optionValue === 'function') {
|
|
216
|
+
for (var _len2 = arguments.length, params = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
217
|
+
params[_key2 - 1] = arguments[_key2];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
optionValue = optionValue.apply(void 0, params);
|
|
197
221
|
}
|
|
198
222
|
|
|
199
|
-
|
|
223
|
+
if (!optionValue) {
|
|
224
|
+
if (optionValue === undefined || optionValue === false) {
|
|
225
|
+
return optionValue;
|
|
226
|
+
} // else, empty string (invalid), null (invalid), 0 (invalid)
|
|
200
227
|
|
|
201
|
-
if (typeof optionValue === 'string') {
|
|
202
|
-
node = doc.querySelector(optionValue);
|
|
203
228
|
|
|
204
|
-
|
|
205
|
-
throw new Error("`".concat(optionName, "` refers to no known node"));
|
|
206
|
-
}
|
|
229
|
+
throw new Error("`".concat(optionName, "` was specified but was not a node, or did not return a node"));
|
|
207
230
|
}
|
|
208
231
|
|
|
209
|
-
|
|
210
|
-
|
|
232
|
+
var node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point
|
|
233
|
+
|
|
234
|
+
if (typeof optionValue === 'string') {
|
|
235
|
+
node = doc.querySelector(optionValue); // resolve to node, or null if fails
|
|
211
236
|
|
|
212
237
|
if (!node) {
|
|
213
|
-
throw new Error("`".concat(optionName, "`
|
|
238
|
+
throw new Error("`".concat(optionName, "` as selector refers to no known node"));
|
|
214
239
|
}
|
|
215
240
|
}
|
|
216
241
|
|
|
@@ -218,20 +243,22 @@
|
|
|
218
243
|
};
|
|
219
244
|
|
|
220
245
|
var getInitialFocusNode = function getInitialFocusNode() {
|
|
221
|
-
var node; // false indicates we want no initialFocus at all
|
|
246
|
+
var node = getNodeForOption('initialFocus'); // false explicitly indicates we want no initialFocus at all
|
|
222
247
|
|
|
223
|
-
if (
|
|
248
|
+
if (node === false) {
|
|
224
249
|
return false;
|
|
225
250
|
}
|
|
226
251
|
|
|
227
|
-
if (
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
252
|
+
if (node === undefined) {
|
|
253
|
+
// option not specified: use fallback options
|
|
254
|
+
if (containersContain(doc.activeElement)) {
|
|
255
|
+
node = doc.activeElement;
|
|
256
|
+
} else {
|
|
257
|
+
var firstTabbableGroup = state.tabbableGroups[0];
|
|
258
|
+
var firstTabbableNode = firstTabbableGroup && firstTabbableGroup.firstTabbableNode; // NOTE: `fallbackFocus` option function cannot return `false` (not supported)
|
|
259
|
+
|
|
260
|
+
node = firstTabbableNode || getNodeForOption('fallbackFocus');
|
|
261
|
+
}
|
|
235
262
|
}
|
|
236
263
|
|
|
237
264
|
if (!node) {
|
|
@@ -243,13 +270,51 @@
|
|
|
243
270
|
|
|
244
271
|
var updateTabbableNodes = function updateTabbableNodes() {
|
|
245
272
|
state.tabbableGroups = state.containers.map(function (container) {
|
|
246
|
-
var tabbableNodes = tabbable.tabbable(container);
|
|
273
|
+
var tabbableNodes = tabbable.tabbable(container); // NOTE: if we have tabbable nodes, we must have focusable nodes; focusable nodes
|
|
274
|
+
// are a superset of tabbable nodes
|
|
275
|
+
|
|
276
|
+
var focusableNodes = tabbable.focusable(container);
|
|
247
277
|
|
|
248
278
|
if (tabbableNodes.length > 0) {
|
|
249
279
|
return {
|
|
250
280
|
container: container,
|
|
251
281
|
firstTabbableNode: tabbableNodes[0],
|
|
252
|
-
lastTabbableNode: tabbableNodes[tabbableNodes.length - 1]
|
|
282
|
+
lastTabbableNode: tabbableNodes[tabbableNodes.length - 1],
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Finds the __tabbable__ node that follows the given node in the specified direction,
|
|
286
|
+
* in this container, if any.
|
|
287
|
+
* @param {HTMLElement} node
|
|
288
|
+
* @param {boolean} [forward] True if going in forward tab order; false if going
|
|
289
|
+
* in reverse.
|
|
290
|
+
* @returns {HTMLElement|undefined} The next tabbable node, if any.
|
|
291
|
+
*/
|
|
292
|
+
nextTabbableNode: function nextTabbableNode(node) {
|
|
293
|
+
var forward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
294
|
+
// NOTE: If tabindex is positive (in order to manipulate the tab order separate
|
|
295
|
+
// from the DOM order), this __will not work__ because the list of focusableNodes,
|
|
296
|
+
// while it contains tabbable nodes, does not sort its nodes in any order other
|
|
297
|
+
// than DOM order, because it can't: Where would you place focusable (but not
|
|
298
|
+
// tabbable) nodes in that order? They have no order, because they aren't tabbale...
|
|
299
|
+
// Support for positive tabindex is already broken and hard to manage (possibly
|
|
300
|
+
// not supportable, TBD), so this isn't going to make things worse than they
|
|
301
|
+
// already are, and at least makes things better for the majority of cases where
|
|
302
|
+
// tabindex is either 0/unset or negative.
|
|
303
|
+
// FYI, positive tabindex issue: https://github.com/focus-trap/focus-trap/issues/375
|
|
304
|
+
var nodeIdx = focusableNodes.findIndex(function (n) {
|
|
305
|
+
return n === node;
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
if (forward) {
|
|
309
|
+
return focusableNodes.slice(nodeIdx + 1).find(function (n) {
|
|
310
|
+
return tabbable.isTabbable(n);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return focusableNodes.slice(0, nodeIdx).reverse().find(function (n) {
|
|
315
|
+
return tabbable.isTabbable(n);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
253
318
|
};
|
|
254
319
|
}
|
|
255
320
|
|
|
@@ -259,7 +324,8 @@
|
|
|
259
324
|
}); // remove groups with no tabbable nodes
|
|
260
325
|
// throw if no groups have tabbable nodes and we don't have a fallback focus node either
|
|
261
326
|
|
|
262
|
-
if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus')
|
|
327
|
+
if (state.tabbableGroups.length <= 0 && !getNodeForOption('fallbackFocus') // returning false not supported for this option
|
|
328
|
+
) {
|
|
263
329
|
throw new Error('Your focus-trap must have at least one container with at least one tabbable node in it at all times');
|
|
264
330
|
}
|
|
265
331
|
};
|
|
@@ -289,14 +355,16 @@
|
|
|
289
355
|
};
|
|
290
356
|
|
|
291
357
|
var getReturnFocusNode = function getReturnFocusNode(previousActiveElement) {
|
|
292
|
-
var node = getNodeForOption('setReturnFocus');
|
|
293
|
-
return node ? node : previousActiveElement;
|
|
358
|
+
var node = getNodeForOption('setReturnFocus', previousActiveElement);
|
|
359
|
+
return node ? node : node === false ? false : previousActiveElement;
|
|
294
360
|
}; // This needs to be done on mousedown and touchstart instead of click
|
|
295
361
|
// so that it precedes the focus event.
|
|
296
362
|
|
|
297
363
|
|
|
298
364
|
var checkPointerDown = function checkPointerDown(e) {
|
|
299
|
-
|
|
365
|
+
var target = getActualTarget(e);
|
|
366
|
+
|
|
367
|
+
if (containersContain(target)) {
|
|
300
368
|
// allow the click since it ocurred inside the trap
|
|
301
369
|
return;
|
|
302
370
|
}
|
|
@@ -315,7 +383,7 @@
|
|
|
315
383
|
// that was clicked, whether it's focusable or not; by setting
|
|
316
384
|
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused
|
|
317
385
|
// on activation (or the configured `setReturnFocus` node)
|
|
318
|
-
returnFocus: config.returnFocusOnDeactivate && !tabbable.isFocusable(
|
|
386
|
+
returnFocus: config.returnFocusOnDeactivate && !tabbable.isFocusable(target)
|
|
319
387
|
});
|
|
320
388
|
return;
|
|
321
389
|
} // This is needed for mobile devices.
|
|
@@ -334,11 +402,12 @@
|
|
|
334
402
|
|
|
335
403
|
|
|
336
404
|
var checkFocusIn = function checkFocusIn(e) {
|
|
337
|
-
var
|
|
405
|
+
var target = getActualTarget(e);
|
|
406
|
+
var targetContained = containersContain(target); // In Firefox when you Tab out of an iframe the Document is briefly focused.
|
|
338
407
|
|
|
339
|
-
if (targetContained ||
|
|
408
|
+
if (targetContained || target instanceof Document) {
|
|
340
409
|
if (targetContained) {
|
|
341
|
-
state.mostRecentlyFocusedNode =
|
|
410
|
+
state.mostRecentlyFocusedNode = target;
|
|
342
411
|
}
|
|
343
412
|
} else {
|
|
344
413
|
// escaped! pull it back in to where it just left
|
|
@@ -352,17 +421,19 @@
|
|
|
352
421
|
|
|
353
422
|
|
|
354
423
|
var checkTab = function checkTab(e) {
|
|
424
|
+
var target = getActualTarget(e);
|
|
355
425
|
updateTabbableNodes();
|
|
356
426
|
var destinationNode = null;
|
|
357
427
|
|
|
358
428
|
if (state.tabbableGroups.length > 0) {
|
|
359
429
|
// make sure the target is actually contained in a group
|
|
360
|
-
// NOTE: the target may also be the container itself if it's
|
|
430
|
+
// NOTE: the target may also be the container itself if it's focusable
|
|
361
431
|
// with tabIndex='-1' and was given initial focus
|
|
362
432
|
var containerIndex = findIndex(state.tabbableGroups, function (_ref) {
|
|
363
433
|
var container = _ref.container;
|
|
364
|
-
return container.contains(
|
|
434
|
+
return container.contains(target);
|
|
365
435
|
});
|
|
436
|
+
var containerGroup = containerIndex >= 0 ? state.tabbableGroups[containerIndex] : undefined;
|
|
366
437
|
|
|
367
438
|
if (containerIndex < 0) {
|
|
368
439
|
// target not found in any group: quite possible focus has escaped the trap,
|
|
@@ -379,11 +450,14 @@
|
|
|
379
450
|
// is the target the first tabbable node in a group?
|
|
380
451
|
var startOfGroupIndex = findIndex(state.tabbableGroups, function (_ref2) {
|
|
381
452
|
var firstTabbableNode = _ref2.firstTabbableNode;
|
|
382
|
-
return
|
|
453
|
+
return target === firstTabbableNode;
|
|
383
454
|
});
|
|
384
455
|
|
|
385
|
-
if (startOfGroupIndex < 0 &&
|
|
386
|
-
// an exception case where the target is the container itself,
|
|
456
|
+
if (startOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target) && !tabbable.isTabbable(target) && !containerGroup.nextTabbableNode(target, false))) {
|
|
457
|
+
// an exception case where the target is either the container itself, or
|
|
458
|
+
// a non-tabbable node that was given focus (i.e. tabindex is negative
|
|
459
|
+
// and user clicked on it or node was programmatically given focus)
|
|
460
|
+
// and is not followed by any other tabbable node, in which
|
|
387
461
|
// case, we should handle shift+tab as if focus were on the container's
|
|
388
462
|
// first tabbable node, and go to the last tabbable node of the LAST group
|
|
389
463
|
startOfGroupIndex = containerIndex;
|
|
@@ -402,11 +476,14 @@
|
|
|
402
476
|
// is the target the last tabbable node in a group?
|
|
403
477
|
var lastOfGroupIndex = findIndex(state.tabbableGroups, function (_ref3) {
|
|
404
478
|
var lastTabbableNode = _ref3.lastTabbableNode;
|
|
405
|
-
return
|
|
479
|
+
return target === lastTabbableNode;
|
|
406
480
|
});
|
|
407
481
|
|
|
408
|
-
if (lastOfGroupIndex < 0 &&
|
|
409
|
-
// an exception case where the target is the container itself,
|
|
482
|
+
if (lastOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target) && !tabbable.isTabbable(target) && !containerGroup.nextTabbableNode(target))) {
|
|
483
|
+
// an exception case where the target is the container itself, or
|
|
484
|
+
// a non-tabbable node that was given focus (i.e. tabindex is negative
|
|
485
|
+
// and user clicked on it or node was programmatically given focus)
|
|
486
|
+
// and is not followed by any other tabbable node, in which
|
|
410
487
|
// case, we should handle tab as if focus were on the container's
|
|
411
488
|
// last tabbable node, and go to the first tabbable node of the FIRST group
|
|
412
489
|
lastOfGroupIndex = containerIndex;
|
|
@@ -423,6 +500,7 @@
|
|
|
423
500
|
}
|
|
424
501
|
}
|
|
425
502
|
} else {
|
|
503
|
+
// NOTE: the fallbackFocus option does not support returning false to opt-out
|
|
426
504
|
destinationNode = getNodeForOption('fallbackFocus');
|
|
427
505
|
}
|
|
428
506
|
|
|
@@ -434,7 +512,7 @@
|
|
|
434
512
|
};
|
|
435
513
|
|
|
436
514
|
var checkKey = function checkKey(e) {
|
|
437
|
-
if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates) !== false) {
|
|
515
|
+
if (isEscapeEvent(e) && valueOrHandler(config.escapeDeactivates, e) !== false) {
|
|
438
516
|
e.preventDefault();
|
|
439
517
|
trap.deactivate();
|
|
440
518
|
return;
|
|
@@ -451,7 +529,9 @@
|
|
|
451
529
|
return;
|
|
452
530
|
}
|
|
453
531
|
|
|
454
|
-
|
|
532
|
+
var target = getActualTarget(e);
|
|
533
|
+
|
|
534
|
+
if (containersContain(target)) {
|
|
455
535
|
return;
|
|
456
536
|
}
|
|
457
537
|
|
|
@@ -639,5 +719,5 @@
|
|
|
639
719
|
|
|
640
720
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
641
721
|
|
|
642
|
-
}))
|
|
722
|
+
}));
|
|
643
723
|
//# sourceMappingURL=focus-trap.umd.js.map
|