|
1 |
| -import React, {useCallback, useEffect, useRef, useState} from 'react' |
| 1 | +import React, {useCallback, useEffect, useRef, useState, type SyntheticEvent} from 'react' |
2 | 2 | import styled from 'styled-components'
|
3 | 3 | import type {ButtonProps} from '../Button'
|
4 | 4 | import {Button} from '../Button'
|
@@ -98,9 +98,9 @@ export interface DialogProps extends SxProp {
|
98 | 98 |
|
99 | 99 | /**
|
100 | 100 | * This method is invoked when a gesture to close the dialog is used (either
|
101 |
| - * an Escape key press or clicking the "X" in the top-right corner). The |
| 101 | + * an Escape key press, clicking the backdrop, or clicking the "X" in the top-right corner). The |
102 | 102 | * gesture argument indicates the gesture that was used to close the dialog
|
103 |
| - * (either 'close-button' or 'escape'). |
| 103 | + * ('close-button' or 'escape'). |
104 | 104 | */
|
105 | 105 | onClose: (gesture: 'close-button' | 'escape') => void
|
106 | 106 |
|
@@ -414,6 +414,14 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
|
414 | 414 | }
|
415 | 415 | }
|
416 | 416 | const defaultedProps = {...props, title, subtitle, role, dialogLabelId, dialogDescriptionId}
|
| 417 | + const onBackdropClick = useCallback( |
| 418 | + (e: SyntheticEvent) => { |
| 419 | + if (e.target === e.currentTarget) { |
| 420 | + onClose('escape') |
| 421 | + } |
| 422 | + }, |
| 423 | + [onClose], |
| 424 | + ) |
417 | 425 |
|
418 | 426 | const dialogRef = useRef<HTMLDivElement>(null)
|
419 | 427 | useRefObjectAsForwardedRef(forwardedRef, dialogRef)
|
@@ -465,7 +473,7 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
|
465 | 473 | return (
|
466 | 474 | <>
|
467 | 475 | <Portal>
|
468 |
| - <Backdrop ref={backdropRef} {...positionDataAttributes}> |
| 476 | + <Backdrop ref={backdropRef} {...positionDataAttributes} onClick={onBackdropClick}> |
469 | 477 | <StyledDialog
|
470 | 478 | width={width}
|
471 | 479 | height={height}
|
|
0 commit comments