users can click outside of modal to close the modal for info only modals. Take away that ability for important modals

This commit is contained in:
jackiettran
2025-12-23 18:43:17 -05:00
parent 347f709f72
commit 426f974ed3
6 changed files with 50 additions and 80 deletions

View File

@@ -24,6 +24,16 @@ const ConditionCheckViewerModal: React.FC<ConditionCheckViewerModalProps> = ({
const [selectedImage, setSelectedImage] = useState(0); const [selectedImage, setSelectedImage] = useState(0);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") onHide();
};
if (show) {
document.addEventListener("keydown", handleKeyDown);
}
return () => document.removeEventListener("keydown", handleKeyDown);
}, [show, onHide]);
useEffect(() => { useEffect(() => {
const fetchImageUrls = async () => { const fetchImageUrls = async () => {
if (!conditionCheck?.imageFilenames?.length) return; if (!conditionCheck?.imageFilenames?.length) return;

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect, useCallback } from "react"; import React, { useState, useEffect } from "react";
import { rentalAPI } from "../services/api"; import { rentalAPI } from "../services/api";
import { Rental } from "../types"; import { Rental } from "../types";
@@ -65,38 +65,17 @@ const DeclineRentalModal: React.FC<DeclineRentalModalProps> = ({
onHide(); onHide();
}; };
const handleBackdropClick = useCallback(
(e: React.MouseEvent) => {
if (e.target === e.currentTarget && !processing) {
handleClose();
}
},
[handleClose, processing]
);
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape" && !processing) {
handleClose();
}
},
[handleClose, processing]
);
useEffect(() => { useEffect(() => {
if (show) { if (show) {
document.addEventListener("keydown", handleKeyDown);
document.body.style.overflow = "hidden"; document.body.style.overflow = "hidden";
} else { } else {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "unset"; document.body.style.overflow = "unset";
} }
return () => { return () => {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "unset"; document.body.style.overflow = "unset";
}; };
}, [show, handleKeyDown]); }, [show]);
if (!show) return null; if (!show) return null;
@@ -104,7 +83,6 @@ const DeclineRentalModal: React.FC<DeclineRentalModalProps> = ({
<div <div
className="modal d-block" className="modal d-block"
style={{ backgroundColor: "rgba(0,0,0,0.5)" }} style={{ backgroundColor: "rgba(0,0,0,0.5)" }}
onClick={handleBackdropClick}
> >
<div className="modal-dialog modal-dialog-centered"> <div className="modal-dialog modal-dialog-centered">
<div className="modal-content"> <div className="modal-content">

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect, useCallback } from "react"; import React, { useState, useEffect } from "react";
import { rentalAPI } from "../services/api"; import { rentalAPI } from "../services/api";
import { RefundPreview, Rental } from "../types"; import { RefundPreview, Rental } from "../types";
@@ -129,38 +129,17 @@ const RentalCancellationModal: React.FC<RentalCancellationModalProps> = ({
return "success"; return "success";
}; };
const handleBackdropClick = useCallback(
(e: React.MouseEvent) => {
if (e.target === e.currentTarget) {
handleClose();
}
},
[handleClose]
);
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape") {
handleClose();
}
},
[handleClose]
);
useEffect(() => { useEffect(() => {
if (show) { if (show) {
document.addEventListener("keydown", handleKeyDown);
document.body.style.overflow = "hidden"; document.body.style.overflow = "hidden";
} else { } else {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "unset"; document.body.style.overflow = "unset";
} }
return () => { return () => {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "unset"; document.body.style.overflow = "unset";
}; };
}, [show, handleKeyDown]); }, [show]);
if (!show) return null; if (!show) return null;
@@ -168,7 +147,6 @@ const RentalCancellationModal: React.FC<RentalCancellationModalProps> = ({
<div <div
className="modal d-block" className="modal d-block"
style={{ backgroundColor: "rgba(0,0,0,0.5)" }} style={{ backgroundColor: "rgba(0,0,0,0.5)" }}
onClick={handleBackdropClick}
> >
<div className="modal-dialog modal-lg modal-dialog-centered"> <div className="modal-dialog modal-lg modal-dialog-centered">
<div className="modal-content"> <div className="modal-content">

View File

@@ -353,38 +353,17 @@ const ReturnStatusModal: React.FC<ReturnStatusModalProps> = ({
onHide(); onHide();
}, [onHide]); }, [onHide]);
const handleBackdropClick = useCallback(
(e: React.MouseEvent) => {
if (e.target === e.currentTarget) {
handleClose();
}
},
[handleClose]
);
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape") {
handleClose();
}
},
[handleClose]
);
useEffect(() => { useEffect(() => {
if (show) { if (show) {
document.addEventListener("keydown", handleKeyDown);
document.body.style.overflow = "hidden"; document.body.style.overflow = "hidden";
} else { } else {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "unset"; document.body.style.overflow = "unset";
} }
return () => { return () => {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "unset"; document.body.style.overflow = "unset";
}; };
}, [show, handleKeyDown]); }, [show]);
if (!show) return null; if (!show) return null;
@@ -392,7 +371,6 @@ const ReturnStatusModal: React.FC<ReturnStatusModalProps> = ({
<div <div
className="modal d-block" className="modal d-block"
style={{ backgroundColor: "rgba(0,0,0,0.5)" }} style={{ backgroundColor: "rgba(0,0,0,0.5)" }}
onClick={handleBackdropClick}
> >
<div className="modal-dialog modal-lg modal-dialog-centered"> <div className="modal-dialog modal-lg modal-dialog-centered">
<div className="modal-content"> <div className="modal-content">

View File

@@ -1,4 +1,4 @@
import React from "react"; import React, { useEffect } from "react";
import { Rental } from "../types"; import { Rental } from "../types";
import StarRating from "./StarRating"; import StarRating from "./StarRating";
@@ -15,6 +15,16 @@ const ReviewDetailsModal: React.FC<ReviewDetailsModalProps> = ({
rental, rental,
userType, userType,
}) => { }) => {
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
if (show) {
document.addEventListener("keydown", handleKeyDown);
}
return () => document.removeEventListener("keydown", handleKeyDown);
}, [show, onClose]);
if (!show) return null; if (!show) return null;
const formatDateTime = (dateString: string) => { const formatDateTime = (dateString: string) => {
@@ -29,6 +39,9 @@ const ReviewDetailsModal: React.FC<ReviewDetailsModalProps> = ({
className="modal d-block" className="modal d-block"
tabIndex={-1} tabIndex={-1}
style={{ backgroundColor: "rgba(0,0,0,0.5)" }} style={{ backgroundColor: "rgba(0,0,0,0.5)" }}
onClick={(e) => {
if (e.target === e.currentTarget) onClose();
}}
> >
<div className="modal-dialog modal-dialog-centered modal-lg"> <div className="modal-dialog modal-dialog-centered modal-lg">
<div className="modal-content"> <div className="modal-content">

View File

@@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect } from 'react';
interface SuccessModalProps { interface SuccessModalProps {
show: boolean; show: boolean;
@@ -7,19 +7,32 @@ interface SuccessModalProps {
message: string; message: string;
} }
const SuccessModal: React.FC<SuccessModalProps> = ({ const SuccessModal: React.FC<SuccessModalProps> = ({
show, show,
onClose, onClose,
title = "Success!", title = "Success!",
message message
}) => { }) => {
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') onClose();
};
if (show) {
document.addEventListener('keydown', handleKeyDown);
}
return () => document.removeEventListener('keydown', handleKeyDown);
}, [show, onClose]);
if (!show) return null; if (!show) return null;
return ( return (
<div <div
className="modal d-block" className="modal d-block"
tabIndex={-1} tabIndex={-1}
style={{ backgroundColor: 'rgba(0,0,0,0.5)' }} style={{ backgroundColor: 'rgba(0,0,0,0.5)' }}
onClick={(e) => {
if (e.target === e.currentTarget) onClose();
}}
> >
<div className="modal-dialog modal-dialog-centered"> <div className="modal-dialog modal-dialog-centered">
<div className="modal-content"> <div className="modal-content">