Skip to content

Commit

Permalink
keep focus on modal if no tabbable elements are within it
Browse files Browse the repository at this point in the history
add a test case
  • Loading branch information
everdimension committed Mar 26, 2016
1 parent 23eee3b commit c844719
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/helpers/scopeTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var findTabbable = require('../helpers/tabbable');

module.exports = function(node, event) {
var tabbable = findTabbable(node);
if (!tabbable.length) return;
if (!tabbable.length) {
event.preventDefault();
return;
}
var finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
var leavingFinalTabbable = (
finalTabbable === document.activeElement ||
Expand Down
13 changes: 13 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ describe('Modal', function () {
unmountModal();
});

it('keeps focus inside the modal when child has no tabbable elements', function() {
var tabPrevented = false;
var modal = renderModal({isOpen: true}, 'hello');
strictEqual(document.activeElement, modal.portal.refs.content);
Simulate.keyDown(modal.portal.refs.content, {
key: "Tab",
keyCode: 9,
which: 9,
preventDefault: function() { tabPrevented = true; }
});
equal(tabPrevented, true);
});

it('supports custom className', function() {
var modal = renderModal({isOpen: true, className: 'myClass'});
equal(modal.portal.refs.content.className.contains('myClass'), true);
Expand Down

0 comments on commit c844719

Please sign in to comment.