@@ -27,8 +27,8 @@ import {
27
27
WARNING ,
28
28
YES
29
29
} from '../../i18n/i18n-defaults.js' ;
30
- import { stopPropagation } from '../../internal/stopPropagation .js' ;
31
- import type { ButtonPropTypes , DialogDomRef , DialogPropTypes } from '../../webComponents/index.js' ;
30
+ import type { Ui5CustomEvent } from '../../types/index .js' ;
31
+ import type { ButtonDomRef , ButtonPropTypes , DialogDomRef , DialogPropTypes } from '../../webComponents/index.js' ;
32
32
import { Button , Dialog , Icon , Title } from '../../webComponents/index.js' ;
33
33
import { Text } from '../Text/index.js' ;
34
34
import { classNames , styleData } from './MessageBox.module.css.js' ;
@@ -90,9 +90,17 @@ export interface MessageBoxPropTypes
90
90
*/
91
91
initialFocus ?: MessageBoxActionType ;
92
92
/**
93
- * Callback to be executed when the `MessageBox` is closed (either by pressing on one of the `actions` or by pressing the `ESC` key). `event.detail.action` contains the pressed action button.
93
+ * Callback to be executed when the `MessageBox` is closed (either by pressing on one of the `actions` or by pressing the `ESC` key).
94
+ * `event.detail.action` contains the pressed action button.
95
+ *
96
+ * __Note:__ The target of the event differs according to how the user closed the dialog.
94
97
*/
95
- onClose ?: ( event : CustomEvent < { action : MessageBoxActionType } > ) => void ;
98
+ onClose ?: (
99
+ //todo adjust this once enrichEventWithDetails forwards the native `detail`
100
+ event :
101
+ | Ui5CustomEvent < DialogDomRef , { action : undefined } >
102
+ | ( MouseEvent & ButtonDomRef & { detail : { action : MessageBoxActionType } } )
103
+ ) => void ;
96
104
}
97
105
98
106
const getIcon = ( icon , type , classes ) => {
@@ -188,9 +196,18 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
188
196
}
189
197
} ;
190
198
191
- const handleOnClose = ( e ) => {
192
- const { action } = e . target . dataset ;
193
- stopPropagation ( e ) ;
199
+ const handleDialogClose : DialogPropTypes [ 'onBeforeClose' ] = ( e ) => {
200
+ if ( typeof props . onBeforeClose === 'function' ) {
201
+ props . onBeforeClose ( e ) ;
202
+ }
203
+ if ( e . detail . escPressed ) {
204
+ // @ts -expect-error: todo check type
205
+ onClose ( enrichEventWithDetails ( e , { action : undefined } ) ) ;
206
+ }
207
+ } ;
208
+
209
+ const handleOnClose : ButtonPropTypes [ 'onClick' ] = ( e ) => {
210
+ const { action } = e . currentTarget . dataset ;
194
211
onClose ( enrichEventWithDetails ( e , { action } ) ) ;
195
212
} ;
196
213
@@ -206,7 +223,7 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
206
223
} ;
207
224
208
225
// @ts -expect-error: footer, headerText and onClose are already omitted via prop types
209
- const { footer : _0 , headerText : _1 , onClose : _2 , ...restWithoutOmitted } = rest ;
226
+ const { footer : _0 , headerText : _1 , onClose : _2 , onBeforeClose : _3 , ...restWithoutOmitted } = rest ;
210
227
211
228
const iconToRender = getIcon ( icon , type , classNames ) ;
212
229
const needsCustomHeader = ! props . header && ! ! iconToRender ;
@@ -216,7 +233,7 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
216
233
open = { open }
217
234
ref = { ref }
218
235
className = { clsx ( classNames . messageBox , className ) }
219
- onClose = { open ? handleOnClose : stopPropagation }
236
+ onBeforeClose = { handleDialogClose }
220
237
accessibleNameRef = { needsCustomHeader ? `${ messageBoxId } -title ${ messageBoxId } -text` : undefined }
221
238
accessibleRole = { PopupAccessibleRole . AlertDialog }
222
239
{ ...restWithoutOmitted }
0 commit comments