Skip to content

Commit

Permalink
added esc button and x button to exit (#183)
Browse files Browse the repository at this point in the history
* added esc button and x button to exit

* oops

* fixed

* Minor css cleanup

---------

Co-authored-by: Michael Zhao <michaelzhao314@gmail.com>
  • Loading branch information
kellyxzhou and MichaelZhao21 authored Oct 27, 2024
1 parent 72c6217 commit 895d9a4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions client/src/components/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React, { useEffect } from 'react';
import { twMerge } from 'tailwind-merge';

interface PopupProps {
Expand All @@ -19,6 +20,21 @@ interface PopupProps {
* Clicking on the backdrop will close the popup.
*/
const Popup = (props: PopupProps) => {
useEffect(() => {
// handle esc key press
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
props.setEnabled(false);
}
};

if (props.enabled) {
window.addEventListener('keydown', handleKeyDown);
}

return () => window.removeEventListener('keydown', handleKeyDown);
}, [props.enabled, props.setEnabled]);

if (!props.enabled) {
return null;
}
Expand All @@ -35,6 +51,14 @@ const Popup = (props: PopupProps) => {
props.className
)}
>
{/* x to close the popup */}
<button
onClick={() => props.setEnabled(false)}
className="absolute top-1 left-3 text-3xl text-lightest hover:text-black duration-150"
aria-label="Close popup"
>
&times;
</button>
{props.children}
</div>
</>
Expand Down

0 comments on commit 895d9a4

Please sign in to comment.