File Explorer Expand/Collapse Implementation
How to implement expand/collapse with event handling and state management.
File Explorer Expand/Collapse Implementation
Expand/collapse is the core interaction of a file explorer. Here is how to implement it.
Basic Toggle
div.addEventListener("click", (e) => { e.stopPropagation(); const childrenContainer = div.nextElementSibling; const isVisible = childrenContainer.style.display !== "none"; childrenContainer.style.display = isVisible ? "none" : "block"; });
Icon Toggle
const icon = div.querySelector(".icon"); icon.textContent = isVisible ? "📁" : "📂"; // closed/open folder
Expand All / Collapse All
function expandAll() { document.querySelectorAll(".children-container").forEach((c) => c.style.display = "block"); } function collapseAll() { document.querySelectorAll(".children-container").forEach((c) => c.style.display = "none"); }
State Persistence
Track expanded folders in a Set:
const expanded = new Set(); div.addEventListener("click", (e) => { e.stopPropagation(); if (expanded.has(node.path)) { expanded.delete(node.path); childrenContainer.style.display = "none"; } else { expanded.add(node.path); childrenContainer.style.display = "block"; } });
Keyboard Support
div.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); div.click(); } if (e.key === "ArrowRight" && !isExpanded) expand(); if (e.key === "ArrowLeft" && isExpanded) collapse(); });
The Takeaway
Expand/collapse: toggle children display on click, update icon (open/closed), stopPropagation to prevent parent toggling, expand/collapse all buttons, state persistence with a Set, and keyboard support (Enter, Arrow keys). These make the file explorer feel like VS Code.
Add a click handler to folder elements. Toggle the children container's display between 'none' and 'block'. Update the folder icon. Use e.stopPropagation() to prevent parent folders from toggling when a child is clicked.
Select all children containers and set their display: expandAll sets all to 'block', collapseAll sets all to 'none'. Also update all folder icons accordingly.
Use a Set to track expanded folder paths. Add to the Set on expand, delete on collapse. On re-render, check if a path is in the Set and set the display accordingly. Optionally save to localStorage.
Add tabindex='0' to folder elements. Handle keydown: Enter/Space to toggle, ArrowRight to expand, ArrowLeft to collapse, ArrowUp/Down to navigate between nodes. This makes the file explorer accessible.
Because folders are nested. Clicking a child folder would also trigger the parent folder's click handler (event bubbling). stopPropagation prevents this, so only the clicked folder toggles.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

