@@ -44,6 +44,7 @@ export default class ModalPortal extends Component {
44
44
closeTimeoutMS : PropTypes . number ,
45
45
shouldFocusAfterRender : PropTypes . bool ,
46
46
shouldCloseOnOverlayClick : PropTypes . bool ,
47
+ shouldReturnFocusAfterClose : PropTypes . bool ,
47
48
role : PropTypes . string ,
48
49
contentLabel : PropTypes . string ,
49
50
aria : PropTypes . object ,
@@ -137,8 +138,23 @@ export default class ModalPortal extends Component {
137
138
afterClose = ( ) => {
138
139
// Remove body class
139
140
bodyClassList . remove ( this . props . bodyOpenClassName ) ;
140
- focusManager . returnFocus ( ) ;
141
- focusManager . teardownScopedFocus ( ) ;
141
+
142
+ if ( this . shouldReturnFocus ( ) ) {
143
+ focusManager . returnFocus ( ) ;
144
+ focusManager . teardownScopedFocus ( ) ;
145
+ }
146
+ } ;
147
+
148
+ shouldReturnFocus = ( ) => {
149
+ // Don't restore focus to the element that had focus prior to
150
+ // the modal's display if:
151
+ // 1. Focus was never shifted to the modal in the first place
152
+ // (shouldFocusAfterRender = false)
153
+ // 2. Explicit direction to not restore focus
154
+ return (
155
+ this . props . shouldFocusAfterRender ||
156
+ this . props . shouldReturnFocusAfterClose
157
+ ) ;
142
158
} ;
143
159
144
160
open = ( ) => {
@@ -147,8 +163,11 @@ export default class ModalPortal extends Component {
147
163
clearTimeout ( this . closeTimer ) ;
148
164
this . setState ( { beforeClose : false } ) ;
149
165
} else {
150
- focusManager . setupScopedFocus ( this . node ) ;
151
- focusManager . markForFocusLater ( ) ;
166
+ if ( this . shouldReturnFocus ( ) ) {
167
+ focusManager . setupScopedFocus ( this . node ) ;
168
+ focusManager . markForFocusLater ( ) ;
169
+ }
170
+
152
171
this . setState ( { isOpen : true } , ( ) => {
153
172
this . setState ( { afterOpen : true } ) ;
154
173
0 commit comments