File tree Expand file tree Collapse file tree 4 files changed +17
-4
lines changed
Expand file tree Collapse file tree 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';
3838 */
3939 closeTimeoutMS= {0 }
4040 /*
41- Object indicating styles to be used for the modal.
41+ Object indicating styles to be used for the modal.
4242 It has two keys, `overlay` and `content`. See the `Styles` section for more details.
4343 */
4444 style= {{ overlay: {}, content: {} }}
@@ -70,6 +70,10 @@ import ReactModal from 'react-modal';
7070 Boolean indicating if the appElement should be hidden
7171 */
7272 ariaHideApp= {true }
73+ /*
74+ Boolean indicating if the modal should be focused after render
75+ */
76+ shouldFocusAfterRender= {true }
7377 /*
7478 Boolean indicating if the overlay should close the modal
7579 */
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ describe('State', () => {
4343 expect ( props . isOpen ) . toBe ( false ) ;
4444 expect ( props . ariaHideApp ) . toBe ( true ) ;
4545 expect ( props . closeTimeoutMS ) . toBe ( 0 ) ;
46+ expect ( props . shouldFocusAfterRender ) . toBe ( true ) ;
4647 expect ( props . shouldCloseOnOverlayClick ) . toBe ( true ) ;
4748 ReactDOM . unmountComponentAtNode ( node ) ;
4849 ariaAppHider . resetForTesting ( ) ;
@@ -131,11 +132,16 @@ describe('State', () => {
131132 } , closeTimeoutMS ) ;
132133 } ) ;
133134
134- it ( 'focuses the modal content' , ( ) => {
135+ it ( 'focuses the modal content by default ' , ( ) => {
135136 const modal = renderModal ( { isOpen : true } , null ) ;
136137 expect ( document . activeElement ) . toBe ( mcontent ( modal ) ) ;
137138 } ) ;
138139
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+
139145 it ( 'give back focus to previous element or modal.' , done => {
140146 function cleanup ( ) {
141147 unmountModal ( ) ;
Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ export default class Modal extends Component {
5151 onRequestClose : PropTypes . func ,
5252 closeTimeoutMS : PropTypes . number ,
5353 ariaHideApp : PropTypes . bool ,
54+ shouldFocusAfter : PropTypes . bool ,
5455 shouldCloseOnOverlayClick : PropTypes . bool ,
5556 parentSelector : PropTypes . func ,
5657 aria : PropTypes . object ,
@@ -65,6 +66,7 @@ export default class Modal extends Component {
6566 bodyOpenClassName,
6667 ariaHideApp : true ,
6768 closeTimeoutMS : 0 ,
69+ shouldFocusAfterRender : true ,
6870 shouldCloseOnOverlayClick : true ,
6971 parentSelector ( ) { return document . body ; }
7072 } ;
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ export default class ModalPortal extends Component {
4848 onAfterOpen : PropTypes . func ,
4949 onRequestClose : PropTypes . func ,
5050 closeTimeoutMS : PropTypes . number ,
51+ shouldFocusAfterRender : PropTypes . bool ,
5152 shouldCloseOnOverlayClick : PropTypes . bool ,
5253 role : PropTypes . string ,
5354 contentLabel : PropTypes . string ,
@@ -105,8 +106,8 @@ export default class ModalPortal extends Component {
105106 clearTimeout ( this . closeTimer ) ;
106107 }
107108
108- setFocusAfterRender = focus => {
109- this . focusAfterRender = focus ;
109+ setFocusAfterRender = ( focus ) => {
110+ this . focusAfterRender = this . props . shouldFocusAfterRender && focus ;
110111 }
111112
112113 setOverlayRef = ( overlay ) => {
You can’t perform that action at this time.
0 commit comments