From 04a3e014967d3b4a9d8fd115cfdc1a9a60ae1a21 Mon Sep 17 00:00:00 2001 From: Viktor Svertoka <115661003+ViktorSvertoka@users.noreply.github.com> Date: Sat, 6 Jan 2024 20:30:55 +0200 Subject: [PATCH] Feat(JS) Added close for Esc --- src/js/05-modal-exercises.js | 2 +- src/js/06-modal-rating.js | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/js/05-modal-exercises.js b/src/js/05-modal-exercises.js index bf42be7..dce7250 100644 --- a/src/js/05-modal-exercises.js +++ b/src/js/05-modal-exercises.js @@ -261,7 +261,7 @@ overlay.addEventListener('click', function (event) { } }); -window.addEventListener('keydown', function (event) { +document.addEventListener('keydown', function (event) { if (event.key === 'Escape' && !modalExercises.classList.contains('hidden')) { closeModalExercises(); } diff --git a/src/js/06-modal-rating.js b/src/js/06-modal-rating.js index 775e829..6585196 100644 --- a/src/js/06-modal-rating.js +++ b/src/js/06-modal-rating.js @@ -5,7 +5,6 @@ const btnIsClosed = document.querySelector('.rating-close-btn'); modalExercises.addEventListener('click', onExercisesCardClick); btnIsClosed.addEventListener('click', closeModal); modalIsOpen.addEventListener('click', closeOverlay); - document.addEventListener('keydown', onEscClick); function onExercisesCardClick(event) { @@ -24,14 +23,12 @@ function closeModal() { function closeOverlay(event) { if (event.target == modalIsOpen) { - modalIsOpen.classList.add('is-hidden'); - modalExercises.classList.remove('hidden'); + closeModal(); } } function onEscClick(event) { - if (event.code === 'Escape') { - modalIsOpen.classList.add('is-hidden'); - modalExercises.classList.remove('hidden'); + if (event.key === 'Escape') { + closeModal(); } }