File tree 4 files changed +17
-4
lines changed
4 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ import ReactModal from 'react-modal';
38
38
*/
39
39
closeTimeoutMS= {0 }
40
40
/*
41
- Object indicating styles to be used for the modal.
41
+ Object indicating styles to be used for the modal.
42
42
It has two keys, `overlay` and `content`. See the `Styles` section for more details.
43
43
*/
44
44
style= {{ overlay: {}, content: {} }}
@@ -70,6 +70,10 @@ import ReactModal from 'react-modal';
70
70
Boolean indicating if the appElement should be hidden
71
71
*/
72
72
ariaHideApp= {true }
73
+ /*
74
+ Boolean indicating if the modal should be focused after render
75
+ */
76
+ shouldFocusAfterRender= {true }
73
77
/*
74
78
Boolean indicating if the overlay should close the modal
75
79
*/
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ describe('State', () => {
43
43
expect ( props . isOpen ) . toBe ( false ) ;
44
44
expect ( props . ariaHideApp ) . toBe ( true ) ;
45
45
expect ( props . closeTimeoutMS ) . toBe ( 0 ) ;
46
+ expect ( props . shouldFocusAfterRender ) . toBe ( true ) ;
46
47
expect ( props . shouldCloseOnOverlayClick ) . toBe ( true ) ;
47
48
ReactDOM . unmountComponentAtNode ( node ) ;
48
49
ariaAppHider . resetForTesting ( ) ;
@@ -131,11 +132,16 @@ describe('State', () => {
131
132
} , closeTimeoutMS ) ;
132
133
} ) ;
133
134
134
- it ( 'focuses the modal content' , ( ) => {
135
+ it ( 'focuses the modal content by default ' , ( ) => {
135
136
const modal = renderModal ( { isOpen : true } , null ) ;
136
137
expect ( document . activeElement ) . toBe ( mcontent ( modal ) ) ;
137
138
} ) ;
138
139
140
+ it ( 'does not focus the modal content when shouldFocusAfterRender is false' , ( ) => {
141
+ const modal = renderModal ( { isOpen : true , shouldFocusAfterRender : false } , null ) ;
142
+ expect ( document . activeElement ) . toNotBe ( mcontent ( modal ) ) ;
143
+ } ) ;
144
+
139
145
it ( 'give back focus to previous element or modal.' , done => {
140
146
function cleanup ( ) {
141
147
unmountModal ( ) ;
Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ export default class Modal extends Component {
51
51
onRequestClose : PropTypes . func ,
52
52
closeTimeoutMS : PropTypes . number ,
53
53
ariaHideApp : PropTypes . bool ,
54
+ shouldFocusAfter : PropTypes . bool ,
54
55
shouldCloseOnOverlayClick : PropTypes . bool ,
55
56
parentSelector : PropTypes . func ,
56
57
aria : PropTypes . object ,
@@ -65,6 +66,7 @@ export default class Modal extends Component {
65
66
bodyOpenClassName,
66
67
ariaHideApp : true ,
67
68
closeTimeoutMS : 0 ,
69
+ shouldFocusAfterRender : true ,
68
70
shouldCloseOnOverlayClick : true ,
69
71
parentSelector ( ) { return document . body ; }
70
72
} ;
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ export default class ModalPortal extends Component {
48
48
onAfterOpen : PropTypes . func ,
49
49
onRequestClose : PropTypes . func ,
50
50
closeTimeoutMS : PropTypes . number ,
51
+ shouldFocusAfterRender : PropTypes . bool ,
51
52
shouldCloseOnOverlayClick : PropTypes . bool ,
52
53
role : PropTypes . string ,
53
54
contentLabel : PropTypes . string ,
@@ -105,8 +106,8 @@ export default class ModalPortal extends Component {
105
106
clearTimeout ( this . closeTimer ) ;
106
107
}
107
108
108
- setFocusAfterRender = focus => {
109
- this . focusAfterRender = focus ;
109
+ setFocusAfterRender = ( focus ) => {
110
+ this . focusAfterRender = this . props . shouldFocusAfterRender && focus ;
110
111
}
111
112
112
113
setOverlayRef = ( overlay ) => {
You can’t perform that action at this time.
0 commit comments