diff --git a/lib/src/index.js b/lib/src/index.js index 3a1b8cc..5f6223f 100644 --- a/lib/src/index.js +++ b/lib/src/index.js @@ -189,18 +189,21 @@ const MicroModal = (() => { return (node.offsetParent !== null) }) + // no focusable nodes left + if (focusableNodes.length === 0) return + // if disableFocus is true if (!this.modal.contains(document.activeElement)) { focusableNodes[0].focus() } else { const focusedItemIndex = focusableNodes.indexOf(document.activeElement) - if (event.shiftKey && focusedItemIndex === 0) { + if (event.shiftKey && (focusedItemIndex === 0 || focusedItemIndex === -1)) { focusableNodes[focusableNodes.length - 1].focus() event.preventDefault() } - if (!event.shiftKey && focusableNodes.length > 0 && focusedItemIndex === focusableNodes.length - 1) { + if (!event.shiftKey && (focusedItemIndex === focusableNodes.length - 1 || focusedItemIndex === -1)) { focusableNodes[0].focus() event.preventDefault() }