Skip to content

Commit 4fe2e54

Browse files
author
David Connelly
committed
Improved modal and slide nav functionality in app.js
- Refactored element selection function for better consistency - Enhanced modal opening to prevent immediate closure - Implemented smooth animation for modal opening using requestAnimationFrame - Added click-outside-to-close functionality for modals - Reorganized code into separate, modular functions for improved maintainability - Moved slide nav auto-population into a dedicated function - Centralized event listeners for better event handling - Preserved all existing functionality while improving code structure
1 parent 161f90f commit 4fe2e54

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

public/js/app.js

+58-19
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ const body = document.querySelector("body");
22
const slideNav = document.getElementById("slide-nav");
33
const main = document.querySelector("main");
44
let slideNavOpen = false;
5+
let openingModal = false;
56

6-
function _(elRef) {
7+
function getElement(elRef) {
78
const firstChar = elRef.substring(0, 1);
89
if (firstChar === ".") {
9-
return document.getElementsByClassName(elRef.replace(/\./g, ""));
10+
return document.querySelector(elRef); // Changed to querySelector for consistency
1011
} else {
1112
return document.getElementById(elRef);
1213
}
@@ -29,6 +30,12 @@ function closeSlideNav() {
2930
}
3031

3132
function openModal(modalId) {
33+
openingModal = true;
34+
35+
setTimeout(() => {
36+
openingModal = false;
37+
}, 100);
38+
3239
let pageOverlay = document.getElementById("overlay");
3340
if (!pageOverlay) {
3441
const modalContainer = document.createElement("div");
@@ -41,7 +48,9 @@ function openModal(modalId) {
4148
pageOverlay.setAttribute("style", "z-index: 2");
4249
body.prepend(pageOverlay);
4350

44-
const targetModal = _(modalId);
51+
const targetModal = getElement(modalId);
52+
if (!targetModal) return;
53+
4554
const targetModalContent = targetModal.innerHTML;
4655
targetModal.remove();
4756

@@ -52,11 +61,16 @@ function openModal(modalId) {
5261
newModal.innerHTML = targetModalContent;
5362
modalContainer.appendChild(newModal);
5463

55-
setTimeout(() => {
64+
// Use requestAnimationFrame to ensure the modal is in the DOM before we try to show it
65+
requestAnimationFrame(() => {
66+
newModal.style.display = 'block';
5667
newModal.style.opacity = 1;
5768
newModal.style.marginTop = "12vh";
58-
}, 0);
69+
});
70+
71+
return newModal;
5972
}
73+
return null;
6074
}
6175

6276
function closeModal() {
@@ -81,27 +95,52 @@ function closeModal() {
8195
}
8296
}
8397

84-
const slideNavLinks = document.querySelector("#slide-nav ul");
85-
if (slideNavLinks) {
86-
const autoPopulateSlideNav = slideNavLinks.getAttribute("auto-populate");
87-
if (autoPopulateSlideNav === "true") {
98+
function autoPopulateSlideNav() {
99+
const slideNavLinks = document.querySelector("#slide-nav ul");
100+
if (slideNavLinks && slideNavLinks.getAttribute("auto-populate") === "true") {
88101
const navLinks = document.querySelector("#top-nav");
89102
if (navLinks) {
90103
slideNavLinks.innerHTML = navLinks.innerHTML;
91104
}
92105
}
106+
}
93107

94-
body.addEventListener("click", (ev) => {
95-
if (slideNavOpen && ev.target.id !== "open-btn") {
96-
if (!slideNav.contains(ev.target)) {
97-
closeSlideNav();
98-
}
99-
}
100-
});
108+
function handleSlideNavClick(event) {
109+
if (slideNavOpen && event.target.id !== "open-btn" && !slideNav.contains(event.target)) {
110+
closeSlideNav();
111+
}
101112
}
102113

103-
document.addEventListener('keydown', (event) => {
114+
function handleEscapeKey(event) {
104115
if (event.key === 'Escape') {
105-
closeModal();
116+
const modalContainer = document.getElementById("modal-container");
117+
if (modalContainer) {
118+
closeModal();
119+
}
106120
}
107-
});
121+
}
122+
123+
function handleModalClick(event) {
124+
if (openingModal === true) {
125+
return;
126+
}
127+
128+
const modalContainer = document.getElementById("modal-container");
129+
if (modalContainer) {
130+
const modal = modalContainer.querySelector('.modal');
131+
if (modal && !modal.contains(event.target)) {
132+
closeModal();
133+
}
134+
}
135+
}
136+
137+
// Initialize
138+
autoPopulateSlideNav();
139+
140+
// Add event listeners
141+
body.addEventListener("click", (event) => {
142+
handleSlideNavClick(event);
143+
handleModalClick(event);
144+
});
145+
146+
document.addEventListener('keydown', handleEscapeKey);

0 commit comments

Comments
 (0)