|
1 | | -import React from 'react'; |
| 1 | +import React, { useRef } from 'react'; |
2 | 2 | import PropTypes from 'prop-types'; |
3 | 3 | import ReactDOM from 'react-dom'; |
4 | 4 | import cx from 'classnames'; |
@@ -34,6 +34,8 @@ const Modal = React.forwardRef((props, ref) => { |
34 | 34 | } = props; |
35 | 35 |
|
36 | 36 | const modal = useModal(visible, onToggle); |
| 37 | + const waitingForMouseUp = useRef(false); |
| 38 | + const ignoreBackdropClick = useRef(false); |
37 | 39 |
|
38 | 40 | // Return null if not mounted. |
39 | 41 | if (!modal.mounted || !modal.visible) { |
@@ -67,25 +69,43 @@ const Modal = React.forwardRef((props, ref) => { |
67 | 69 | return; |
68 | 70 | } |
69 | 71 |
|
70 | | - if (event.target === modal.ref.current) { |
71 | | - modal.setVisible(false); |
| 72 | + if ( |
| 73 | + ignoreBackdropClick.current || |
| 74 | + event.target !== event.currentTarget |
| 75 | + ) { |
| 76 | + ignoreBackdropClick.current = false; |
| 77 | + return; |
72 | 78 | } |
| 79 | + |
| 80 | + modal.setVisible(false); |
| 81 | + }} |
| 82 | + onMouseUp={(event) => { |
| 83 | + if (waitingForMouseUp.current && event.target === modal.ref.current) { |
| 84 | + ignoreBackdropClick.current = true; |
| 85 | + } |
| 86 | + |
| 87 | + waitingForMouseUp.current = false; |
73 | 88 | }} |
74 | 89 | onKeyUp={(event) => { |
75 | | - if (event.key === 'Escape') { |
76 | | - event.preventDefault(); |
| 90 | + if (event.key !== 'Escape') { |
| 91 | + return; |
| 92 | + } |
77 | 93 |
|
78 | | - if (backdrop === 'static') { |
79 | | - return; |
80 | | - } |
| 94 | + event.preventDefault(); |
81 | 95 |
|
82 | | - modal.setVisible(false); |
| 96 | + if (backdrop === 'static') { |
| 97 | + return; |
83 | 98 | } |
| 99 | + |
| 100 | + modal.setVisible(false); |
84 | 101 | }} |
85 | 102 | essentials={{ className: 'modal show' }} |
86 | 103 | > |
87 | 104 | <BaseView |
88 | 105 | accessibilityRole="document" |
| 106 | + onMouseDown={() => { |
| 107 | + waitingForMouseUp.current = true; |
| 108 | + }} |
89 | 109 | essentials={{ className: dialogClasses }} |
90 | 110 | > |
91 | 111 | <ModalContext.Provider value={modal}> |
|
0 commit comments