File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed
Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change 77
88import React , { createRef } from 'react' ;
99import userEvent from '@testing-library/user-event' ;
10- import { render , fireEvent } from '@testing-library/react' ;
10+ import { render , fireEvent , waitFor } from '@testing-library/react' ;
1111import { KEYS } from '@zendeskgarden/container-utilities' ;
1212import { ModalContainer , IUseModalReturnValue } from './' ;
1313
@@ -105,6 +105,16 @@ describe('FocusJailContainer', () => {
105105 expect ( onCloseSpy ) . not . toHaveBeenCalled ( ) ;
106106 } ) ;
107107 } ) ;
108+
109+ it ( 'closes modal on blur' , async ( ) => {
110+ render ( < BasicExample onClose = { onCloseSpy } /> ) ;
111+
112+ await waitFor ( async ( ) => {
113+ await user . click ( document . body ) ;
114+ } ) ;
115+
116+ expect ( onCloseSpy ) . toHaveBeenCalled ( ) ;
117+ } ) ;
108118 } ) ;
109119
110120 describe ( 'getTitleProps()' , ( ) => {
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ export const useModal = <T extends Element = Element>({
5454
5555 const getModalProps : IUseModalReturnValue [ 'getModalProps' ] = ( {
5656 role = 'dialog' ,
57+ onBlur,
5758 onKeyDown,
5859 onMouseDown,
5960 ...other
@@ -63,6 +64,18 @@ export const useModal = <T extends Element = Element>({
6364 'aria-modal' : true ,
6465 'aria-labelledby' : titleId ,
6566 'aria-describedby' : contentId ,
67+ onBlur : composeEventHandlers ( onBlur , event => {
68+ const doc = environment || document ;
69+
70+ // Timeout is required to ensure blur is handled after focus
71+ setTimeout ( ( ) => {
72+ const activeElement = doc . activeElement ;
73+
74+ if ( ! modalRef . current ?. contains ( activeElement ) ) {
75+ closeModal ( event ) ;
76+ }
77+ } ) ;
78+ } ) ,
6679 onMouseDown : composeEventHandlers ( onMouseDown , ( ) => {
6780 isModalMousedDownRef . current = true ;
6881 } ) ,
You can’t perform that action at this time.
0 commit comments