@@ -919,6 +919,64 @@ describe('state with presence context', () => {
919919 expect ( siblingModal ) . not . toBeInTheDocument ( ) ;
920920 expect ( screen . queryByTestId ( 'modal' ) ) . toBeInTheDocument ( ) ;
921921 } ) ;
922+
923+ it ( 'should close only the topmost modal when Escape is pressed' , async ( ) => {
924+ const ModalExample = ( ) => {
925+ const [ isSiblingOpen , setIsSiblingOpen ] = useState ( false ) ;
926+ const [ isChildOpen , setIsChildOpen ] = useState ( false ) ;
927+
928+ return (
929+ < ComposedModalPresence open >
930+ < ComposedModal data-testid = "modal" >
931+ < ModalHeader > Modal Header</ ModalHeader >
932+ < ModalBody >
933+ < button
934+ type = "button"
935+ data-testid = "launch-sibling-modal"
936+ onClick = { ( ) => setIsSiblingOpen ( true ) } >
937+ Launch sibling modal
938+ </ button >
939+ </ ModalBody >
940+ </ ComposedModal >
941+ < ComposedModal
942+ data-testid = "sibling-modal"
943+ open = { isSiblingOpen }
944+ onClose = { ( ) => setIsSiblingOpen ( false ) } >
945+ < ModalHeader > Modal Header</ ModalHeader >
946+ < ModalBody >
947+ < button
948+ type = "button"
949+ data-testid = "launch-child-modal"
950+ onClick = { ( ) => setIsChildOpen ( true ) } >
951+ Launch child modal
952+ </ button >
953+ < ComposedModal
954+ data-testid = "child-modal"
955+ open = { isChildOpen }
956+ onClose = { ( ) => setIsChildOpen ( false ) } >
957+ < ModalHeader > Modal Header</ ModalHeader >
958+ </ ComposedModal >
959+ </ ModalBody >
960+ </ ComposedModal >
961+ </ ComposedModalPresence >
962+ ) ;
963+ } ;
964+
965+ render ( < ModalExample /> ) ;
966+
967+ await userEvent . click ( screen . getByTestId ( 'launch-sibling-modal' ) ) ;
968+ await userEvent . click ( screen . getByTestId ( 'launch-child-modal' ) ) ;
969+
970+ expect ( screen . queryByTestId ( 'modal' ) ) . toBeInTheDocument ( ) ;
971+ expect ( screen . queryByTestId ( 'sibling-modal' ) ) . toBeInTheDocument ( ) ;
972+ expect ( screen . queryByTestId ( 'child-modal' ) ) . toBeInTheDocument ( ) ;
973+
974+ await userEvent . keyboard ( '{Escape}' ) ;
975+
976+ expect ( screen . queryByTestId ( 'modal' ) ) . toBeInTheDocument ( ) ;
977+ expect ( screen . queryByTestId ( 'sibling-modal' ) ) . toBeInTheDocument ( ) ;
978+ expect ( screen . queryByTestId ( 'child-modal' ) ) . not . toBeInTheDocument ( ) ;
979+ } ) ;
922980} ) ;
923981
924982const ComposedModalWithPresenceHof = withComposedModalPresence ( ( props ) => {
0 commit comments