Skip to content

Commit 06f2471

Browse files
committed
better ux for no more usages modal
1 parent c7f422d commit 06f2471

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/components/NoUsagesModal.jsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { useEffect, useState } from "react";
1+
import React, { useEffect, useRef, useState } from "react";
22
import { useAuth } from "../contexts/AuthContext";
33
import { doc, getDoc } from "firebase/firestore";
44
import { db } from "../config/firebase";
55

66
function NoUsagesModal({ isOpen, onClose }) {
77
const { currentUser } = useAuth();
88
const [timeLeft, setTimeLeft] = useState(null);
9+
const modalRef = useRef(null);
910

1011
useEffect(() => {
1112
const fetchResetTime = async () => {
@@ -41,10 +42,30 @@ function NoUsagesModal({ isOpen, onClose }) {
4142
fetchResetTime();
4243
}, [currentUser.uid]);
4344

45+
useEffect(() => {
46+
// Close modal if clicked outside
47+
const handleClickOutside = (event) => {
48+
if (modalRef.current && !modalRef.current.contains(event.target)) {
49+
onClose();
50+
}
51+
};
52+
53+
if (isOpen) {
54+
document.addEventListener("mousedown", handleClickOutside);
55+
}
56+
57+
return () => {
58+
document.removeEventListener("mousedown", handleClickOutside);
59+
};
60+
}, [isOpen, onClose]);
61+
4462
if (!isOpen) return null;
4563
return (
4664
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-40">
47-
<div className="bg-white border border-gray rounded-2xl shadow-md p-8 max-w-md w-[90%] text-center">
65+
<div
66+
ref={modalRef}
67+
className="bg-white border border-gray rounded-2xl shadow-md p-8 max-w-md w-[90%] text-center"
68+
>
4869
<h2 className="text-2xl font-bold text-text mb-3">
4970
No more free uses today
5071
</h2>

0 commit comments

Comments
 (0)