Skip to content

Commit 8ebf991

Browse files
committed
close Image modal on ESC and outside of modal
1 parent 933cb12 commit 8ebf991

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

components/QuizForm.tsx

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ const QuizForm: FC<Props> = ({
4242
alt: string;
4343
} | null>(null);
4444

45+
useEffect(() => {
46+
const handleEsc = (event) => {
47+
if (event.key === "Escape") {
48+
setSelectedImage(null);
49+
}
50+
};
51+
document.addEventListener("keydown", handleEsc);
52+
return () => {
53+
document.removeEventListener("keydown", handleEsc);
54+
};
55+
}, [setSelectedImage]);
56+
57+
const handleClickOutside = (event) => {
58+
if (event.target === event.currentTarget) {
59+
setSelectedImage(null);
60+
}
61+
};
62+
4563
useEffect(() => {
4664
const checkOllamaStatus = async () => {
4765
try {
@@ -136,11 +154,7 @@ const QuizForm: FC<Props> = ({
136154
...prev,
137155
[currentQuestionIndex]: watchInput,
138156
}));
139-
if (currentQuestionIndex === totalQuestions) {
140-
handleNextQuestion(1);
141-
} else {
142-
handleNextQuestion(currentQuestionIndex + 1);
143-
}
157+
handleNextQuestion(currentQuestionIndex + 1);
144158
};
145159

146160
const isOptionCheckedWithoutReveal = (
@@ -260,7 +274,10 @@ const QuizForm: FC<Props> = ({
260274
</ul>
261275
)}
262276
{selectedImage && (
263-
<div className="fixed top-0 left-0 z-50 w-full h-full flex justify-center items-center bg-black bg-opacity-50">
277+
<div
278+
onClick={handleClickOutside}
279+
className="fixed top-0 left-0 z-50 w-full h-full flex justify-center items-center bg-black bg-opacity-50"
280+
>
264281
<img
265282
src={link + selectedImage.url}
266283
alt={selectedImage.alt}
@@ -293,7 +310,7 @@ const QuizForm: FC<Props> = ({
293310
isOptionCheckedWithoutReveal(option.text)
294311
}
295312
handleChange={(e) => {
296-
handleRadioCheckboxClick(e, noOfAnswers > 1 ? true : false);
313+
handleRadioCheckboxClick(e, noOfAnswers > 1);
297314
}}
298315
/>
299316
</li>
@@ -330,7 +347,7 @@ const QuizForm: FC<Props> = ({
330347
type="button"
331348
intent="primary"
332349
size="medium"
333-
// disabled={currentQuestionIndex < lastIndex}
350+
disabled={currentQuestionIndex == totalQuestions}
334351
onClick={handleNextQueClick}
335352
>
336353
Next Question

0 commit comments

Comments
 (0)