feat(challenge modal): close with escape key

This commit is contained in:
Tom (plebeius.eth)
2025-01-24 13:03:07 +01:00
parent ced1189c49
commit 515007cbf6
@@ -1,4 +1,4 @@
import { useRef, useState } from 'react';
import { useRef, useState, useEffect } from 'react';
import Draggable from 'react-draggable';
import { useTranslation } from 'react-i18next';
import { Challenge as ChallengeType } from '@plebbit/plebbit-react-hooks';
@@ -52,6 +52,16 @@ const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
}
};
useEffect(() => {
const onEscapeKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
closeModal();
}
};
document.addEventListener('keydown', onEscapeKey);
return () => document.removeEventListener('keydown', onEscapeKey);
}, [closeModal]);
// react-draggable requires a ref to the modal node
const nodeRef = useRef(null);
const isMobile = useIsMobile();