File tree 2 files changed +16
-1
lines changed
2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -99,7 +99,10 @@ var ModalPortal = module.exports = React.createClass({
99
99
} ,
100
100
101
101
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
+ }
103
106
} ,
104
107
105
108
closeWithTimeout : function ( ) {
@@ -166,6 +169,10 @@ var ModalPortal = module.exports = React.createClass({
166
169
return ! this . props . isOpen && ! this . state . beforeClose ;
167
170
} ,
168
171
172
+ contentHasFocus ( ) {
173
+ return document . activeElement === this . refs . content || this . refs . content . contains ( document . activeElement ) ;
174
+ } ,
175
+
169
176
buildClassName : function ( which , additional ) {
170
177
var className = CLASS_NAMES [ which ] . base ;
171
178
if ( this . state . afterOpen )
Original file line number Diff line number Diff line change @@ -86,6 +86,14 @@ describe('Modal', function () {
86
86
} ) ;
87
87
} ) ;
88
88
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
+
89
97
it ( 'handles case when child has no tabbable elements' , function ( ) {
90
98
var component = renderModal ( { isOpen : true } , 'hello' ) ;
91
99
assert . doesNotThrow ( function ( ) {
You can’t perform that action at this time.
0 commit comments