From 8ad540a68241b0dbc27ff1cbc28734977416d175 Mon Sep 17 00:00:00 2001 From: Viktor Svertoka <115661003+ViktorSvertoka@users.noreply.github.com> Date: Sat, 6 Jan 2024 19:34:50 +0200 Subject: [PATCH] Update 06-modal-rating.js --- src/js/06-modal-rating.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/js/06-modal-rating.js b/src/js/06-modal-rating.js index d18675b..775e829 100644 --- a/src/js/06-modal-rating.js +++ b/src/js/06-modal-rating.js @@ -3,8 +3,10 @@ const modalIsOpen = document.querySelector('.modal-rating'); const btnIsClosed = document.querySelector('.rating-close-btn'); modalExercises.addEventListener('click', onExercisesCardClick); -btnIsClosed.addEventListener('click', closeModalExercises); -modalIsOpen.addEventListener('click', closeModalExercises); +btnIsClosed.addEventListener('click', closeModal); +modalIsOpen.addEventListener('click', closeOverlay); + +document.addEventListener('keydown', onEscClick); function onExercisesCardClick(event) { if (!event.target.closest('.modal-exercises__btn-rating')) { @@ -15,7 +17,21 @@ function onExercisesCardClick(event) { modalExercises.classList.add('hidden'); } -function closeModalExercises() { +function closeModal() { modalIsOpen.classList.add('is-hidden'); modalExercises.classList.remove('hidden'); } + +function closeOverlay(event) { + if (event.target == modalIsOpen) { + modalIsOpen.classList.add('is-hidden'); + modalExercises.classList.remove('hidden'); + } +} + +function onEscClick(event) { + if (event.code === 'Escape') { + modalIsOpen.classList.add('is-hidden'); + modalExercises.classList.remove('hidden'); + } +}