Skip to content

Commit

Permalink
Merge pull request #90 from EugeneYaremenko/add-esc-button-support
Browse files Browse the repository at this point in the history
fix handleEsc
  • Loading branch information
max-mykhailenko authored Oct 25, 2024
2 parents 309846b + 38d8497 commit bce343b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/react-pure-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,25 @@ function PureModal(props: Props) {
};
}, [isOpen]);

const handleEsc = useCallback(event => {
const allModals = document.querySelectorAll('.pure-modal');
const handleEsc = useCallback(
event => {
const allModals = document.querySelectorAll('.pure-modal');
const isManyModalsOpen = allModals.length > 1; // open modal in modal
const firstModalData = allModals[allModals.length - 1];

if (isManyModalsOpen && !firstModalData.className.includes(hash)) {
return false; // closing only current modal
}

if (allModals.length && allModals[allModals.length - 1].classList.contains(hash)) return false;
if (event.key === 'Escape' && document.activeElement) {
close(event);
return true;
}

if (event.key === 'Escape' && document.activeElement) {
close(event);
}
}, []);
return false;
},
[close, hash],
);

if (!isOpen) {
return null;
Expand Down

0 comments on commit bce343b

Please sign in to comment.