Skip to content

refactor(web): modularize main.js into focused ES6 modules (#874)#946

Merged
steam-bell-92 merged 2 commits into
steam-bell-92:mainfrom
nishtha-agarwal-211:refactor/874-modular-mainjs
Jun 3, 2026
Merged

refactor(web): modularize main.js into focused ES6 modules (#874)#946
steam-bell-92 merged 2 commits into
steam-bell-92:mainfrom
nishtha-agarwal-211:refactor/874-modular-mainjs

Conversation

@nishtha-agarwal-211
Copy link
Copy Markdown
Contributor

@nishtha-agarwal-211 nishtha-agarwal-211 commented Jun 1, 2026

Fixes #874

Description

This PR resolves issue #874 by refactoring the monolithic and highly complex web-app/js/main.js file (~2,100 lines) into modern, focused, and maintainable ES6 Modules.

Previously, main.js suffered from overlapping responsibilities, duplicate/unnecessary event listeners, and duplicate helper definitions (like duplicate prefersReducedMotion or debounce declarations).

By extracting these concerns into modular single-responsibility components and upgrading our pages to load modules natively, we have drastically improved code readability, maintainability, and load times.


Architecture & Modules Introduced

A new web-app/js/modules/ directory was created to host the following decoupled components:

  1. utils.js:
    • Contains core helpers (prefersReducedMotion, debounce, safeRun, escapeHtml).
    • Introduces a new, highly optimized, and DRY helper updateProjectVisibility(currentCategory, currentSearchQuery) which solves search query and category filtering combinations in a single pass.
  2. theme.js:
    • Manages unified site theme switches, local storage persistence, and browser metadata color-scheme updates.
    • Synchronizes the main sidebar toggle and the hero section theme toggle seamlessly.
  3. search.js:
    • Handles recent search history, search input events, and dynamic dropdown autocomplete suggestion lists.
    • Adds full support for Ctrl+K keybindings.
    • Integrates Arrow keys navigation and Enter key selection within search suggestions.
  4. modal.js:
    • Manages project modals, safe loading logic, dynamic inline instructions helper icons, and focus trapping (trapFocus).
    • Attaches openProjectSafe and closeProjectSafe globally to the window object to ensure backward compatibility for other scripts or inline HTML references.
  5. sidebar.js:
    • Coordinates category selection navigation, dynamic stats counters (games, math, utilities), and mobile drawer toggle menus with safe click-outside dismissal.

Entry Point and HTML Upgrades

  • main.js: Rewritten as a clean and beautiful orchestrator that imports and initializes each of these modules. It handlesTimeline Serpentine SVG winding path calculations, back-to-top buttons, and sharing toast alerts.
  • Index Pages: Upgraded <script> tags to type="module" in all page files to natively support ES6 imports:
    • index.html
    • games.html
    • math.html
    • utilities.html

Copilot AI review requested due to automatic review settings June 1, 2026 16:22
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

@nishtha-agarwal-211 is attempting to deploy a commit to the Anuj's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR refactors the web app’s front-end bootstrapping by converting the monolithic main.js into ES modules and normalizing the HTML structure/formatting across category pages.

Changes:

  • Converted js/main.js into an ES module and split functionality into js/modules/* (theme, sidebar, search, modal, utils).
  • Updated multiple HTML pages to use type="module" for main.js and applied consistent HTML formatting/structure.
  • Added shared utilities for filtering project cards and handling reduced-motion behavior.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
web-app/js/main.js Replaced large inline script with a module-based orchestrator that composes new feature modules.
web-app/js/modules/utils.js Introduces shared helpers (debounce, reduced motion detection, unified card filtering).
web-app/js/modules/theme.js Centralizes theme persistence, toggle UI updates, and theme-color meta syncing.
web-app/js/modules/sidebar.js Moves sidebar + category navigation + stats logic into a dedicated module.
web-app/js/modules/search.js Adds modular search dropdown, suggestions, history, and keyboard navigation.
web-app/js/modules/modal.js Modularizes modal open/close, focus trap, inert handling, and info modal wiring.
web-app/index.html Loads main.js as an ES module.
web-app/games.html HTML formatting cleanup + loads main.js as an ES module.
web-app/math.html HTML formatting cleanup + loads main.js as an ES module.
web-app/utilities.html HTML formatting cleanup + loads main.js as an ES module.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +28 to +39
document.addEventListener("click", (e) => {
if (
mainSidebar.classList.contains("open") &&
!mainSidebar.contains(e.target) &&
e.target !== mobileSidebarToggle
) {
mainSidebar.classList.remove("open");
mobileSidebarToggle.setAttribute("aria-expanded", "false");
const icon = mobileSidebarToggle.querySelector("i");
if (icon) icon.className = "fas fa-bars";
}
});
sidebar.js - Sidebar layout, category tabs, and mobile drawers.
*/

import { debounce } from "./utils.js";
if (!overlay || !titleEl || !listEl) return;

titleEl.textContent = title;
listEl.innerHTML = steps.map((step) => "<li>" + step + "</li>").join("");
Comment on lines +245 to +264
function triggerSearch(query) {
if (query) {
recentSearches = recentSearches.filter((s) => s !== query);
recentSearches.unshift(query);
recentSearches = recentSearches.slice(0, 10);
localStorage.setItem("recentSearches", JSON.stringify(recentSearches));
}
onSearchChange(query);
}

const debouncedSearch = debounce((query) => {
renderSuggestions(query);
}, 200);

searchInput.addEventListener("input", (e) => {
const query = e.target.value.trim();
if (searchLoader) searchLoader.style.display = query ? "block" : "none";
debouncedSearch(query);
triggerSearch(query);
});
Comment thread web-app/js/main.js
Comment on lines +370 to +377
window.addEventListener(
"scroll",
() => {
const navH = navbar ? navbar.getBoundingClientRect().height : 72;
stickyFilterBar.style.top = `${navH + 16}px`;
},
{ passive: true }
);
@steam-bell-92 steam-bell-92 merged commit 764c0f0 into steam-bell-92:main Jun 3, 2026
8 of 9 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

🎉 Thank you for your contribution!

Your Pull Request has been merged successfully.

We appreciate the time and effort you put into improving this project. Contributions like yours help the repository grow and stay useful for everyone.

If you'd like to contribute again, please check the open issues and make sure you are assigned before opening another Pull Request.

Thanks again for your support! 🙌

@nishtha-agarwal-211 nishtha-agarwal-211 deleted the refactor/874-modular-mainjs branch June 3, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Web: Refactor main.js into Modular ES6 Modules

3 participants