Skip to content

Commit cc95ab1

Browse files
author
Kunwartejpal Sidhu
committed
fix(modal): Remove redundant modal fix scripts that interfered with focus restoration
- Removed an inline MODAL FIX script in index.html that was prematurely clearing the modal body on Escape, destroying the focused element and breaking the focus restoration flow in js/main.js. - Removed a duplicate, broken openProject event listener from the bottom of js/main.js that was causing ReferenceErrors. - Updated Playwright test locator to specifically target a button inside #projectsSection so that it isn't confused by dynamically injected 'Recently Viewed' cards.
1 parent 1fe2877 commit cc95ab1

3 files changed

Lines changed: 2 additions & 164 deletions

File tree

web-app/index.html

Lines changed: 0 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,142 +1429,7 @@ <h3>${proj.title}</h3>
14291429
})();
14301430
</script>
14311431

1432-
<!-- MODAL FIX - Load projects properly -->
1433-
<script>
1434-
(function () {
1435-
const modal = document.getElementById("projectModal");
1436-
const modalBody = document.getElementById("modalBody");
1437-
const modalClose = document.getElementById("modalClose");
1438-
let previouslyFocusedElement = null;
1439-
1440-
if (!modal || !modalBody) {
1441-
console.error("Modal elements not found!");
1442-
return;
1443-
}
1444-
1445-
// Function to close modal
1446-
function closeModal() {
1447-
modal.classList.remove("active");
1448-
modal.setAttribute("aria-hidden", "true");
1449-
document.body.style.overflow = "";
1450-
document.body.style.paddingRight = "";
1451-
1452-
// Clear modal body
1453-
modalBody.innerHTML = "";
1454-
1455-
if (previouslyFocusedElement && typeof previouslyFocusedElement.focus === "function") {
1456-
previouslyFocusedElement.focus();
1457-
previouslyFocusedElement = null;
1458-
}
1459-
1460-
console.log("Modal closed");
1461-
}
1462-
1463-
// Function to open modal with project
1464-
window.openProjectModal = function (projectName, triggerElement) {
1465-
console.log("Opening project:", projectName);
1466-
previouslyFocusedElement = triggerElement || document.activeElement;
1467-
1468-
// Clear previous content
1469-
modalBody.innerHTML =
1470-
'<div style="text-align: center; padding: 2rem;">Loading project...</div>';
1471-
1472-
// Show modal
1473-
modal.classList.add("active");
1474-
modal.setAttribute("aria-hidden", "false");
1475-
document.body.style.overflow = "hidden";
1476-
1477-
// Get project HTML
1478-
let projectHTML = "";
1479-
1480-
// Try to get HTML from the project's function
1481-
const functionName =
1482-
"get" +
1483-
projectName
1484-
.split("-")
1485-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
1486-
.join("") +
1487-
"HTML";
1488-
1489-
if (typeof window[functionName] === "function") {
1490-
try {
1491-
projectHTML = window[functionName]();
1492-
console.log("Loaded via", functionName);
1493-
} catch (e) {
1494-
console.error("Error loading project:", e);
1495-
projectHTML = `<div style="text-align: center; padding: 2rem; color: red;">
1496-
<h3>Error loading project</h3>
1497-
<p>${e.message}</p>
1498-
</div>`;
1499-
}
1500-
} else {
1501-
// Fallback: Try to find by looking at loaded scripts
1502-
console.warn("No loader found for:", projectName);
1503-
projectHTML = `<div style="text-align: center; padding: 2rem;">
1504-
<h3>${projectName.replace(/-/g, " ").toUpperCase()}</h3>
1505-
<p>Project content will be available soon.</p>
1506-
<p style="color: var(--accent);">Check back later!</p>
1507-
</div>`;
1508-
}
1509-
1510-
modalBody.innerHTML = `
1511-
<div class="modal-project-container">
1512-
<div class="modal-project-header">
1513-
<h2>${projectName.replace(/-/g, " ").toUpperCase()}</h2>
1514-
</div>
1515-
<div class="modal-project-content">
1516-
${projectHTML}
1517-
</div>
1518-
</div>
1519-
`;
15201432

1521-
// Try to initialize project if function exists
1522-
const initName =
1523-
"init" +
1524-
projectName
1525-
.split("-")
1526-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
1527-
.join("");
1528-
1529-
if (typeof window[initName] === "function") {
1530-
setTimeout(() => {
1531-
try {
1532-
window[initName]();
1533-
console.log("Initialized:", projectName);
1534-
} catch (e) {
1535-
console.error("Init error:", e);
1536-
}
1537-
}, 100);
1538-
}
1539-
};
1540-
1541-
// Close button handler
1542-
if (modalClose) {
1543-
modalClose.addEventListener("click", closeModal);
1544-
}
1545-
1546-
// Click outside to close
1547-
modal.addEventListener("click", (e) => {
1548-
if (e.target === modal) {
1549-
closeModal();
1550-
}
1551-
});
1552-
1553-
// Escape key to close
1554-
document.addEventListener("keydown", (e) => {
1555-
if (e.key === "Escape" && modal.classList.contains("active")) {
1556-
closeModal();
1557-
}
1558-
});
1559-
1560-
// Override the existing openProjectSafe if needed
1561-
if (typeof window.openProjectSafe !== "function") {
1562-
window.openProjectSafe = window.openProjectModal;
1563-
}
1564-
1565-
console.log('✅ Modal fix loaded');
1566-
})();
1567-
</script>
15681433

15691434
<script>
15701435
// FIX 1: Add missing toPascalCase function

web-app/js/main.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,35 +1649,7 @@ document.addEventListener("DOMContentLoaded", function () {
16491649
}
16501650
}
16511651

1652-
// Open Project Modal
1653-
projectCards.forEach(card => {
1654-
card.tabIndex = 0;
1655-
card.setAttribute('role', 'button');
1656-
card.setAttribute('aria-label', `Open ${card.querySelector('h3')?.textContent || 'project'}`);
16571652

1658-
const playButton = card.querySelector('.btn-play');
1659-
1660-
if (playButton) {
1661-
playButton.addEventListener('click', (e) => {
1662-
e.stopPropagation();
1663-
const projectName = card.getAttribute('data-project');
1664-
openProject(projectName);
1665-
});
1666-
}
1667-
1668-
card.addEventListener('click', () => {
1669-
const projectName = card.getAttribute('data-project');
1670-
openProject(projectName);
1671-
});
1672-
1673-
card.addEventListener('keydown', (e) => {
1674-
if (e.key === 'Enter' || e.key === ' ') {
1675-
e.preventDefault();
1676-
const projectName = card.getAttribute('data-project');
1677-
openProject(projectName);
1678-
}
1679-
});
1680-
});
16811653
/* ── Activate item based on viewport center crossing timeline dots ── */
16821654
var activeIdx = -1;
16831655
var dots = document.querySelectorAll(".timeline-dot");

web-app/tests-e2e/modal.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ test.describe('Modal Lifecycle & Focus Trapping', () => {
1212
await expect(page.locator('body')).toHaveClass(/sidebar-active/);
1313

1414
// Get the first project card's "Try It" button
15-
const firstCardPlayBtn = page.locator('.project-card .btn-play').first();
15+
// Target #projectsSection specifically so that dynamic 'Recently Viewed' additions don't shift the locator
16+
const firstCardPlayBtn = page.locator('#projectsSection .project-card .btn-play').first();
1617
await expect(firstCardPlayBtn).toBeVisible();
1718

1819
// Focus and click the play button

0 commit comments

Comments
 (0)