Skip to content

Commit

Permalink
[feat] added escape to close functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-dalby committed Jan 5, 2025
1 parent ad57bec commit 8adc608
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client/src/components/common/modals/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ const Modal: React.FC<ModalProps> = ({
}
};

const handleEscapeKey = (event: KeyboardEvent) => {
if (event.key === 'Escape' && isOpen) {
onClose();
}
};

if (isOpen) {
document.body.style.overflow = 'hidden';
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleEscapeKey);
if (contentRef.current) {
contentRef.current.scrollTop = 0;
}
Expand All @@ -47,6 +54,7 @@ const Modal: React.FC<ModalProps> = ({
return () => {
document.body.style.overflow = 'unset';
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('keydown', handleEscapeKey);
};
}, [isOpen, onClose]);

Expand Down

0 comments on commit 8adc608

Please sign in to comment.