@@ -225,24 +225,30 @@ export default class Modal extends Component {
225
225
}
226
226
}
227
227
228
- focusButton = focusContainerElement => {
228
+ initialFocus = focusContainerElement => {
229
229
const primaryFocusElement = focusContainerElement
230
230
? focusContainerElement . querySelector ( this . props . selectorPrimaryFocus )
231
231
: null ;
232
232
if ( primaryFocusElement ) {
233
- primaryFocusElement . focus ( ) ;
234
- return ;
233
+ return primaryFocusElement ;
235
234
}
236
- if ( this . button && this . button . current ) {
237
- this . button . current . focus ( ) ;
235
+ return this . button && this . button . current ;
236
+ } ;
237
+
238
+ focusButton = focusContainerElement => {
239
+ const target = this . initialFocus ( focusContainerElement ) ;
240
+ if ( target ) {
241
+ target . focus ( ) ;
238
242
}
239
243
} ;
240
244
241
245
componentDidMount ( ) {
242
246
if ( ! this . props . open ) {
243
247
return ;
244
248
}
245
- this . focusButton ( this . innerModal . current ) ;
249
+ if ( ! this . props . focusTrap ) {
250
+ this . focusButton ( this . innerModal . current ) ;
251
+ }
246
252
}
247
253
248
254
handleTransitionEnd = evt => {
@@ -253,7 +259,9 @@ export default class Modal extends Component {
253
259
this . outerModal . current . offsetHeight &&
254
260
this . beingOpen
255
261
) {
256
- this . focusButton ( evt . currentTarget ) ;
262
+ if ( ! this . props . focusTrap ) {
263
+ this . focusButton ( evt . currentTarget ) ;
264
+ }
257
265
this . beingOpen = false ;
258
266
}
259
267
} ;
@@ -349,22 +357,29 @@ export default class Modal extends Component {
349
357
</ div >
350
358
) ;
351
359
352
- return (
353
- < FocusTrap active = { open && focusTrap } >
354
- < div
355
- { ...other }
356
- onKeyDown = { this . handleKeyDown }
357
- onClick = { this . handleClick }
358
- onBlur = { this . handleBlur }
359
- className = { modalClasses }
360
- role = "presentation"
361
- tabIndex = { - 1 }
362
- onTransitionEnd = {
363
- this . props . open ? this . handleTransitionEnd : undefined
364
- }
365
- ref = { this . outerModal } >
366
- { modalBody }
367
- </ div >
360
+ const modal = (
361
+ < div
362
+ { ...other }
363
+ onKeyDown = { this . handleKeyDown }
364
+ onClick = { this . handleClick }
365
+ onBlur = { this . handleBlur }
366
+ className = { modalClasses }
367
+ role = "presentation"
368
+ tabIndex = { - 1 }
369
+ onTransitionEnd = { this . props . open ? this . handleTransitionEnd : undefined }
370
+ ref = { this . outerModal } >
371
+ { modalBody }
372
+ </ div >
373
+ ) ;
374
+
375
+ return ! focusTrap ? (
376
+ modal
377
+ ) : (
378
+ // `<FocusTrap>` has `active: true` in its `defaultProps`
379
+ < FocusTrap
380
+ active = { ! ! open }
381
+ focusTrapOptions = { { initialFocus : this . initialFocus } } >
382
+ { modal }
368
383
</ FocusTrap >
369
384
) ;
370
385
}
0 commit comments