From b66a6cd968970f2f8e8889e5c6754c89826b3598 Mon Sep 17 00:00:00 2001 From: Viktor Svertoka <115661003+ViktorSvertoka@users.noreply.github.com> Date: Sat, 6 Jan 2024 18:34:10 +0200 Subject: [PATCH] Fix(JS) Update modal code --- src/js/06-modal-rating.js | 1 + src/js/07-modal-team.js | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/js/06-modal-rating.js b/src/js/06-modal-rating.js index 55e7dca..d18675b 100644 --- a/src/js/06-modal-rating.js +++ b/src/js/06-modal-rating.js @@ -4,6 +4,7 @@ const btnIsClosed = document.querySelector('.rating-close-btn'); modalExercises.addEventListener('click', onExercisesCardClick); btnIsClosed.addEventListener('click', closeModalExercises); +modalIsOpen.addEventListener('click', closeModalExercises); function onExercisesCardClick(event) { if (!event.target.closest('.modal-exercises__btn-rating')) { diff --git a/src/js/07-modal-team.js b/src/js/07-modal-team.js index 7265d4a..bc22eed 100644 --- a/src/js/07-modal-team.js +++ b/src/js/07-modal-team.js @@ -5,34 +5,25 @@ const teamCloseBtn = document.querySelector('.team__modal-close-btn'); function onLinkClick(event) { event.preventDefault(); - teamBackdrop.classList.remove('is-hidden'); - document.body.classList.add('modal-open'); - + openModalStaff(); addAllEventListeners(); } function onEscClick(event) { - event.preventDefault(); - - if (event.code !== 'Escape') { - return; + if (event.code === 'Escape') { + closeAndRemoveListeners(); } - - closingModalStaff(); } function onBackdropClick(event) { - if (event.target.closest('.team__wrapper')) { - return; + if (!event.target.closest('.team__wrapper')) { + closeAndRemoveListeners(); } - - closingModalStaff(); } function onCloseBtnClick(event) { event.preventDefault(); - - closingModalStaff(); + closeAndRemoveListeners(); } function addAllEventListeners() { @@ -41,11 +32,20 @@ function addAllEventListeners() { teamCloseBtn.addEventListener('click', onCloseBtnClick); } -function closingModalStaff() { +function closeAndRemoveListeners() { document.removeEventListener('keydown', onEscClick); teamBackdrop.removeEventListener('click', onBackdropClick); teamCloseBtn.removeEventListener('click', onCloseBtnClick); + closeModalStaff(); +} + +function openModalStaff() { + teamBackdrop.classList.remove('is-hidden'); + document.body.classList.add('modal-open'); +} + +function closeModalStaff() { teamBackdrop.classList.add('is-hidden'); document.body.classList.remove('modal-open'); }