Skip to content

Commit 2e24072

Browse files
authored
Merge pull request #931 from vansh-codes/fix/926-close-modal-on-esc
PR Modal close on Escape key press
2 parents dad1069 + 2c6ed26 commit 2e24072

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/components/dashboard/LeaderBoard/PRListModal.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// src/components/dashboard/LeaderBoard/PRListModal.tsx
2-
import React from "react";
2+
import React, { useEffect } from "react";
33
import { motion, AnimatePresence } from "framer-motion";
44
import { FaTimes, FaExternalLinkAlt, FaGithub } from "react-icons/fa";
55
import { useColorMode } from "@docusaurus/theme-common";
@@ -64,12 +64,6 @@ export default function PRListModal({
6464
}
6565
};
6666

67-
const handleKeyDown = (e: React.KeyboardEvent) => {
68-
if (e.key === "Escape") {
69-
onClose();
70-
}
71-
};
72-
7367
// Helper function to get filter display text
7468
const getFilterDisplayText = (filter: string) => {
7569
switch (filter) {
@@ -94,6 +88,21 @@ export default function PRListModal({
9488
return "#6b7280"; // Gray for no points
9589
};
9690

91+
// Close modal on Escape key press
92+
useEffect(() => {
93+
if (!isOpen) return;
94+
const handleEsc = (event: KeyboardEvent) => {
95+
if (event.key === "Escape") {
96+
onClose();
97+
}
98+
};
99+
100+
window.addEventListener("keydown", handleEsc);
101+
return () => {
102+
window.removeEventListener("keydown", handleEsc);
103+
};
104+
}, [isOpen])
105+
97106
return (
98107
<AnimatePresence>
99108
{isOpen && (
@@ -103,7 +112,6 @@ export default function PRListModal({
103112
animate={{ opacity: 1 }}
104113
exit={{ opacity: 0 }}
105114
onClick={handleBackdropClick}
106-
onKeyDown={handleKeyDown}
107115
tabIndex={0}
108116
role="dialog"
109117
aria-modal="true"

0 commit comments

Comments
 (0)