mirror of
https://github.com/rowyio/rowy.git
synced 2025-12-28 16:06:41 +01:00
Modal: emphasize close button if backdrop click or escape key disabled
This commit is contained in:
@@ -24,6 +24,7 @@ import ScrollableDialogContent, {
|
||||
export interface IModalProps extends Partial<Omit<DialogProps, "title">> {
|
||||
onClose: () => void;
|
||||
disableBackdropClick?: boolean;
|
||||
disableEscapeKeyDown?: boolean;
|
||||
|
||||
title: ReactNode;
|
||||
header?: ReactNode;
|
||||
@@ -45,6 +46,7 @@ export interface IModalProps extends Partial<Omit<DialogProps, "title">> {
|
||||
export default function Modal({
|
||||
onClose,
|
||||
disableBackdropClick,
|
||||
disableEscapeKeyDown,
|
||||
title,
|
||||
header,
|
||||
footer,
|
||||
@@ -60,13 +62,22 @@ export default function Modal({
|
||||
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
|
||||
|
||||
const [open, setOpen] = useState(true);
|
||||
const handleClose = (_, reason?: string) => {
|
||||
if (disableBackdropClick && reason === "backdropClick") return;
|
||||
const handleClose: NonNullable<DialogProps["onClose"]> = (_, reason) => {
|
||||
if (
|
||||
(disableBackdropClick && reason === "backdropClick") ||
|
||||
(disableEscapeKeyDown && reason === "escapeKeyDown")
|
||||
) {
|
||||
setEmphasizeCloseButton(true);
|
||||
return;
|
||||
}
|
||||
|
||||
setOpen(false);
|
||||
setEmphasizeCloseButton(false);
|
||||
setTimeout(() => onClose(), 300);
|
||||
};
|
||||
|
||||
const [emphasizeCloseButton, setEmphasizeCloseButton] = useState(false);
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
@@ -99,11 +110,17 @@ export default function Modal({
|
||||
|
||||
{!hideCloseButton && (
|
||||
<IconButton
|
||||
onClick={handleClose}
|
||||
onClick={handleClose as any}
|
||||
aria-label="Close"
|
||||
sx={{
|
||||
m: { xs: 1, sm: 1.5 },
|
||||
ml: { xs: -1, sm: -1 },
|
||||
|
||||
bgcolor: emphasizeCloseButton ? "error.main" : undefined,
|
||||
color: emphasizeCloseButton ? "error.contrastText" : undefined,
|
||||
"&:hover": emphasizeCloseButton
|
||||
? { bgcolor: "error.dark" }
|
||||
: undefined,
|
||||
}}
|
||||
className="dialog-close"
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user