Skip to content

Commit

Permalink
Return focus on outside click if clicked element is not focusable (#62)
Browse files Browse the repository at this point in the history
Closes #60.
  • Loading branch information
davidtheclark committed Jul 29, 2018
1 parent 1b8b3a6 commit 4e23484
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
37 changes: 20 additions & 17 deletions index.js
Expand Up @@ -4,8 +4,9 @@ var xtend = require('xtend');
var listeningFocusTrap = null;

function focusTrap(element, userOptions) {
var doc = document;
var container =
typeof element === 'string' ? document.querySelector(element) : element;
typeof element === 'string' ? doc.querySelector(element) : element;

var config = xtend(
{
Expand Down Expand Up @@ -40,7 +41,7 @@ function focusTrap(element, userOptions) {

state.active = true;
state.paused = false;
state.nodeFocusedBeforeActivation = document.activeElement;
state.nodeFocusedBeforeActivation = doc.activeElement;

var onActivate =
activateOptions && activateOptions.onActivate
Expand Down Expand Up @@ -110,23 +111,23 @@ function focusTrap(element, userOptions) {
delay(function() {
tryFocus(getInitialFocusNode());
});
document.addEventListener('focusin', checkFocusIn, true);
document.addEventListener('mousedown', checkPointerDown, true);
document.addEventListener('touchstart', checkPointerDown, true);
document.addEventListener('click', checkClick, true);
document.addEventListener('keydown', checkKey, true);
doc.addEventListener('focusin', checkFocusIn, true);
doc.addEventListener('mousedown', checkPointerDown, true);
doc.addEventListener('touchstart', checkPointerDown, true);
doc.addEventListener('click', checkClick, true);
doc.addEventListener('keydown', checkKey, true);

return trap;
}

function removeListeners() {
if (!state.active || listeningFocusTrap !== trap) return;

document.removeEventListener('focusin', checkFocusIn, true);
document.removeEventListener('mousedown', checkPointerDown, true);
document.removeEventListener('touchstart', checkPointerDown, true);
document.removeEventListener('click', checkClick, true);
document.removeEventListener('keydown', checkKey, true);
doc.removeEventListener('focusin', checkFocusIn, true);
doc.removeEventListener('mousedown', checkPointerDown, true);
doc.removeEventListener('touchstart', checkPointerDown, true);
doc.removeEventListener('click', checkClick, true);
doc.removeEventListener('keydown', checkKey, true);

listeningFocusTrap = null;

Expand All @@ -140,7 +141,7 @@ function focusTrap(element, userOptions) {
return null;
}
if (typeof optionValue === 'string') {
node = document.querySelector(optionValue);
node = doc.querySelector(optionValue);
if (!node) {
throw new Error('`' + optionName + '` refers to no known node');
}
Expand All @@ -158,8 +159,8 @@ function focusTrap(element, userOptions) {
var node;
if (getNodeForOption('initialFocus') !== null) {
node = getNodeForOption('initialFocus');
} else if (container.contains(document.activeElement)) {
node = document.activeElement;
} else if (container.contains(doc.activeElement)) {
node = doc.activeElement;
} else {
node = state.firstTabbableNode || getNodeForOption('fallbackFocus');
}
Expand All @@ -178,7 +179,9 @@ function focusTrap(element, userOptions) {
function checkPointerDown(e) {
if (container.contains(e.target)) return;
if (config.clickOutsideDeactivates) {
deactivate({ returnFocus: false });
deactivate({
returnFocus: !tabbable.isFocusable(e.target)
});
} else {
e.preventDefault();
}
Expand Down Expand Up @@ -239,7 +242,7 @@ function focusTrap(element, userOptions) {
}

function tryFocus(node) {
if (node === document.activeElement) return;
if (node === doc.activeElement) return;
if (!node || !node.focus) {
tryFocus(getInitialFocusNode());
return;
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -36,7 +36,7 @@
},
"homepage": "https://github.com/davidtheclark/focus-trap#readme",
"dependencies": {
"tabbable": "^3.0.0",
"tabbable": "^3.1.0",
"xtend": "^4.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit 4e23484

Please sign in to comment.