File tree 2 files changed +35
-4
lines changed
packages/react-native-web/src/exports/Modal
2 files changed +35
-4
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ function ModalAnimation(props: ModalAnimationProps): React.Node {
37
37
38
38
const [ isRendering , setIsRendering ] = React . useState ( false ) ;
39
39
const wasVisible = React . useRef ( false ) ;
40
+ const wasRendering = React . useRef ( false ) ;
40
41
41
42
const isAnimated = animationType && animationType !== 'none' ;
42
43
@@ -54,14 +55,18 @@ function ModalAnimation(props: ModalAnimationProps): React.Node {
54
55
}
55
56
} else {
56
57
setIsRendering ( false ) ;
57
- if ( onDismiss ) {
58
- onDismiss ( ) ;
59
- }
60
58
}
61
59
} ,
62
- [ onDismiss , onShow , visible ]
60
+ [ onShow , visible ]
63
61
) ;
64
62
63
+ React . useEffect ( ( ) => {
64
+ if ( wasRendering . current && ! isRendering && onDismiss ) {
65
+ onDismiss ( ) ;
66
+ }
67
+ wasRendering . current = isRendering ;
68
+ } , [ isRendering , onDismiss ] ) ;
69
+
65
70
React . useEffect ( ( ) => {
66
71
if ( visible ) {
67
72
setIsRendering ( true ) ;
Original file line number Diff line number Diff line change @@ -309,6 +309,32 @@ describe('components/Modal', () => {
309
309
expect ( document . activeElement ) . toBe ( insideElement ) ;
310
310
} ) ;
311
311
312
+ test ( 'focus is not trapped after closing modal' , ( ) => {
313
+ const { rerender } = render (
314
+ < >
315
+ < a data-testid = { 'outside' } href = { '#outside' } >
316
+ Outside
317
+ </ a >
318
+ < Modal visible = { true } />
319
+ </ >
320
+ ) ;
321
+
322
+ const outsideElement = document . querySelector ( '[data-testid="outside"]' ) ;
323
+ const onDismissCallback = jest . fn ( ( ) => outsideElement . focus ( ) ) ;
324
+
325
+ rerender (
326
+ < >
327
+ < a data-testid = { 'outside' } href = { '#outside' } >
328
+ Outside
329
+ </ a >
330
+ < Modal onDismiss = { onDismissCallback } visible = { false } />
331
+ </ >
332
+ ) ;
333
+
334
+ expect ( onDismissCallback ) . toBeCalledTimes ( 1 ) ;
335
+ expect ( document . activeElement ) . toBe ( outsideElement ) ;
336
+ } ) ;
337
+
312
338
test ( 'focus is brought back to the element that triggered modal after closing' , ( ) => {
313
339
const { rerender } = render (
314
340
< >
You can’t perform that action at this time.
0 commit comments