File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 ( ) {
You can’t perform that action at this time.
0 commit comments