Skip to content

Commit 5429f7c

Browse files
conlanpatrekclaydiffrient
authored andcommittedSep 14, 2016
[fixed] Don't steal focus from a descendent when rendering (#222)
1 parent 8e767e9 commit 5429f7c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
 

‎lib/components/ModalPortal.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ var ModalPortal = module.exports = React.createClass({
9999
},
100100

101101
focusContent: function() {
102-
this.refs.content.focus();
102+
// Don't steal focus from inner elements
103+
if (!this.contentHasFocus()) {
104+
this.refs.content.focus();
105+
}
103106
},
104107

105108
closeWithTimeout: function() {
@@ -166,6 +169,10 @@ var ModalPortal = module.exports = React.createClass({
166169
return !this.props.isOpen && !this.state.beforeClose;
167170
},
168171

172+
contentHasFocus() {
173+
return document.activeElement === this.refs.content || this.refs.content.contains(document.activeElement);
174+
},
175+
169176
buildClassName: function(which, additional) {
170177
var className = CLASS_NAMES[which].base;
171178
if (this.state.afterOpen)

‎specs/Modal.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ describe('Modal', function () {
8686
});
8787
});
8888

89+
it('does not focus the modal content when a descendent is already focused', function() {
90+
var input = React.DOM.input({ className: 'focus_input', ref: function(el) { el && el.focus(); } });
91+
renderModal({isOpen: true}, input, function () {
92+
strictEqual(document.activeElement, document.querySelector('.focus_input'));
93+
unmountModal();
94+
});
95+
});
96+
8997
it('handles case when child has no tabbable elements', function() {
9098
var component = renderModal({isOpen: true}, 'hello');
9199
assert.doesNotThrow(function() {

0 commit comments

Comments
 (0)