DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

React Folder Structure Implementation - Interview Coding Round

Here’s a simple recursive approach to display your file structure. It uses a discriminated union type for safety, passes depth for indentation, and renders children recursively.

FileStructureComponent

import React, { useState } from 'react';
import '../css/file-structure.css'; // import the CSS

const FileStructureComponent = (props: any) => {
  let object = props.data;
  let [isExpanded, setIsExpanded] = useState(true);

  return (
    <div className="fs-node">
      <div
        className="fs-row"
        style={{ paddingLeft: 30 * props.depth + 'px' }}
        aria-expanded={object.type === 'folder' ? isExpanded : undefined}
      >
        <span
          className={`fs-caret ${object.type === 'folder' ? 'is-folder' : 'is-file'}`}
          onClick={() => setIsExpanded(!isExpanded)}
          title={object.type === 'folder' ? (isExpanded ? 'Collapse' : 'Expand') : ''}
          role="button"
        >
          {object.type == 'folder' ? isExpanded ? '-' : '+': ""}
        </span>

        <span className={`fs-icon ${object.type === 'folder' ? 'fs-icon-folder' : 'fs-icon-file'}`}>
          {object.type == 'folder' ? '📁' : '📄'}
        </span>

        <span className="fs-name">{object.name}</span>
      </div>

      {isExpanded ? (
        <div className="fs-children">
          {object.children?.map((eachChild: any, index: any) => (
            <FileStructureComponent
              key={index + '_' + object.name}
              data={eachChild}
              depth={props.depth + 1}
            />
          ))}
        </div>
      ) : (
        <></>
      )}
    </div>
  );
};

export default FileStructureComponent;
Enter fullscreen mode Exit fullscreen mode

CSS - by chatGpt :-)

/* Root theme variables */
:root {
  --fs-bg: #ffffff;
  --fs-row-hover: #f5f7fa;
  --fs-border: #e6e8eb;
  --fs-text: #1f2328;
  --fs-muted: #6a6f76;
  --fs-folder: #1a73e8;
  --fs-file: #6c5ce7;
  --fs-focus: #2b8aeb;
}

/* Optional dark mode */
@media (prefers-color-scheme: dark) {
  :root {
    --fs-bg: #0e1116;
    --fs-row-hover: #151a22;
    --fs-border: #242a33;
    --fs-text: #e8ecf2;
    --fs-muted: #9aa4b2;
    --fs-folder: #66a3ff;
    --fs-file: #b39dfd;
    --fs-focus: #5aa2ff;
  }
}

/* Container for each node */
.fs-node {
  background: var(--fs-bg);
}

/* One row per node (name line) */
.fs-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 28px;
  line-height: 1.2;
  color: var(--fs-text);
  border-left: 1px solid transparent; /* subtle alignment guide */
  transition: background-color 120ms ease-in-out;
}

/* Hover/active feedback */
.fs-row:hover {
  background-color: var(--fs-row-hover);
}

/* Caret (+/-) */
.fs-caret {
  width: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--fs-muted);
  cursor: default; /* default for files */
  user-select: none;
  font-weight: 600;
}

.fs-caret.is-folder {
  cursor: pointer;
}

.fs-caret.is-folder:hover {
  color: var(--fs-focus);
}

/* Icon spacing and color accents */
.fs-icon {
  width: 22px;
  text-align: center;
  display: inline-block;
  transform: translateY(1px); /* minor optical alignment */
}

.fs-icon-folder {
  color: var(--fs-folder);
}

.fs-icon-file {
  color: var(--fs-file);
}

/* File/folder name */
.fs-name {
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, "Apple Color Emoji",
    "Segoe UI Emoji";
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Children wrapper (helps apply a subtle guide line if desired) */
.fs-children {
  border-left: 1px dashed var(--fs-border);
  margin-left: 9px; /* align with caret/icon */
  padding-left: 6px;
}

/* Provide a visible focus ring when rows are focused via mouse or keyboard
   (applies if a parent container manages focus; safe even if not used) */
.fs-row:focus-visible,
.fs-caret:focus-visible {
  outline: 2px solid var(--fs-focus);
  outline-offset: -2px;
  border-radius: 4px;
}
Enter fullscreen mode Exit fullscreen mode

FileStructure

import React from "react";
import FileStructureComponent from "./FileStructureComponent";

const FileStructure = () => {
  let data = [
    {
      id: "root",
      name: "Root",
      type: "folder",
      children: [
        {
          id: "fld_1",
          name: "Documents",
          type: "folder",
          children: [
            { id: "fil_1", name: "Resume.pdf", type: "file", size: 245678 },
            { id: "fil_2", name: "Notes.txt", type: "file", size: 1024 },
          ],
        },
        {
          id: "fld_2",
          name: "Pictures",
          type: "folder",
          children: [
            {
              id: "fld_3",
              name: "Vacations",
              type: "folder",
              children: [
                { id: "fil_3", name: "beach.jpg", type: "file", size: 534567 },
                {
                  id: "fil_4",
                  name: "mountain.png",
                  type: "file",
                  size: 734221,
                },
              ],
            },
          ],
        },
        { id: "fil_5", name: "todo.md", type: "file", size: 512 },
      ],
    },
    {
      id: "root2",
      name: "Root2",
      type: "folder",
      children: [
        {
          id: "fld_1",
          name: "Documents",
          type: "folder",
          children: [
            { id: "fil_1", name: "Resume.pdf", type: "file", size: 245678 },
            { id: "fil_2", name: "Notes.txt", type: "file", size: 1024 },
          ],
        },
        {
          id: "fld_2",
          name: "Pictures",
          type: "folder",
          children: [
            {
              id: "fld_3",
              name: "Vacations",
              type: "folder",
              children: [
                { id: "fil_3", name: "beach.jpg", type: "file", size: 534567 },
                {
                  id: "fil_4",
                  name: "mountain.png",
                  type: "file",
                  size: 734221,
                },
              ],
            },
          ],
        },
        { id: "fil_5", name: "todo.md", type: "file", size: 512 },
      ],
    },
  ];

  return data.map((eachObject, index) => {
    return (
      <>
        <FileStructureComponent key={index} data={eachObject} depth={0}/>
      </>
    );
  });
};
export default FileStructure;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)