Skip to content

πŸ™Œ OpenHands: AI-Driven Development

refactor(files): express tree indentation in CSS (#18804)

Engel Nystf0254fd6a3e5
diff --git a/__tests__/components/features/files-tab/file-tree-view.test.tsx b/__tests__/components/features/files-tab/file-tree-view.test.tsx
index 44cb40af61..a4d5a78c60 100644
--- a/__tests__/components/features/files-tab/file-tree-view.test.tsx
+++ b/__tests__/components/features/files-tab/file-tree-view.test.tsx
@@ -1,3 +1,6 @@
+import { readFile } from "node:fs/promises";
+import { resolve } from "node:path";
+import { compile } from "@tailwindcss/node";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi } from "vitest";
@@ -20,7 +23,7 @@ describe("FileTreeView", () => {
expect(screen.queryByTestId("file-tree-view")).not.toBeInTheDocument();
});
- it("keeps a directory collapsed until clicked, then reveals its children", async () => {
+ it("opens and closes a directory with pointer and keyboard activation", async () => {
// Arrange
const user = userEvent.setup();
render(
@@ -38,15 +41,26 @@ describe("FileTreeView", () => {
).not.toBeInTheDocument();
// Act: expand the directory.
- await user.click(screen.getByTestId("file-tree-dir-src"));
+ const directory = screen.getByTestId("file-tree-dir-src");
+ await user.click(directory);
// Assert: the nested file is now visible.
expect(
screen.getByTestId("file-tree-file-src/main.ts"),
).toBeInTheDocument();
+ expect(directory).toHaveAttribute("aria-expanded", "true");
+
+ // The focused native button must also collapse with Space and reopen with Enter.
+ await user.keyboard(" ");
+ expect(directory).toHaveAttribute("aria-expanded", "false");
+ expect(
+ screen.queryByTestId("file-tree-file-src/main.ts"),
+ ).not.toBeInTheDocument();
+ await user.keyboard("{Enter}");
+ expect(directory).toHaveAttribute("aria-expanded", "true");
});
- it("calls onSelectFile with the file path when a file row is clicked", async () => {
+ it("calls onSelectFile with the file path when a file row is activated", async () => {
// Arrange
const user = userEvent.setup();
const onSelectFile = vi.fn();
@@ -59,10 +73,62 @@ describe("FileTreeView", () => {
);
// Act
- await user.click(screen.getByTestId("file-tree-file-README.md"));
+ const file = screen.getByTestId("file-tree-file-README.md");
+ file.focus();
+ await user.keyboard("{Enter}");
// Assert
expect(onSelectFile).toHaveBeenCalledTimes(1);
expect(onSelectFile).toHaveBeenCalledWith("README.md");
});
+
+ it("compiles each rendered row's indentation from its owned depth value", async () => {
+ // Arrange: root rows and three nested levels exercise both row types.
+ const user = userEvent.setup();
+ render(
+ <FileTreeView
+ paths={["README.md", "src/main.ts", "src/nested/deep/example.ts"]}
+ selectedPath="src/nested/deep/example.ts"
+ onSelectFile={vi.fn()}
+ />,
+ );
+ for (const path of ["src", "src/nested", "src/nested/deep"]) {
+ await user.click(screen.getByTestId(`file-tree-dir-${path}`));
+ }
+ const rows = [
+ [screen.getByTestId("file-tree-dir-src"), 8],
+ [screen.getByTestId("file-tree-dir-src/nested"), 20],
+ [screen.getByTestId("file-tree-dir-src/nested/deep"), 32],
+ [screen.getByTestId("file-tree-file-README.md"), 24],
+ [screen.getByTestId("file-tree-file-src/main.ts"), 36],
+ [screen.getByTestId("file-tree-file-src/nested/deep/example.ts"), 60],
+ ] as const;
+ const compiler = await compile(
+ await readFile(resolve("src/tailwind.css"), "utf8"),
+ { base: resolve("src"), onDependency: () => {} },
+ );
+
+ // Act: build only classes emitted by the real recursive tree.
+ const css = compiler.build(rows.flatMap(([row]) => [...row.classList]));
+ for (const [row, expectedIndent] of rows) {
+ const declarations = [
+ ...css.matchAll(/\.((?:\\.|[\w-])+)\s*\{([^}]+)\}/g),
+ ]
+ .filter(([, selector]) =>
+ row.classList.contains(selector.replace(/\\(.)/g, "$1")),
+ )
+ .map(([, , body]) => body)
+ .join("\n");
+ const padding = /(?:^|\n)\s*padding-left:\s*var\((--[\w-]+)\);/.exec(
+ declarations,
+ );
+
+ // Assert: the generated property reader consumes this row's real value.
+ // A missing utility or a mismatched reader/writer breaks the contract.
+ expect(padding).not.toBeNull();
+ expect(row.style.getPropertyValue(padding![1])).toBe(
+ `${expectedIndent}px`,
+ );
+ }
+ });
});
diff --git a/src/components/features/files-tab/tree-node.tsx b/src/components/features/files-tab/tree-node.tsx
index c45f9980c4..9fffde6ed5 100644
--- a/src/components/features/files-tab/tree-node.tsx
+++ b/src/components/features/files-tab/tree-node.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { type CSSProperties, useState } from "react";
import { ChevronDown, ChevronRight } from "lucide-react";
import FileIcon from "#/icons/file.svg?react";
@@ -31,11 +31,11 @@ export function TreeNode({
aria-expanded={isOpen}
data-testid={`file-tree-dir-${node.path}`}
className={cn(
- "flex w-full items-center gap-1.5 py-1 pr-2 text-left text-sm text-contrast",
+ "flex w-full items-center gap-1.5 py-1 pr-2 pl-(--file-tree-row-indent) text-left text-sm text-contrast",
"hover:bg-tertiary cursor-pointer",
)}
// per-row indentation computed from tree depth at runtime
- style={{ paddingLeft: `${indentPx}px` }}
+ style={{ "--file-tree-row-indent": `${indentPx}px` } as CSSProperties}
>
<span
aria-hidden
@@ -75,14 +75,16 @@ export function TreeNode({
onClick={() => onSelectFile(node.path)}
data-testid={`file-tree-file-${node.path}`}
className={cn(
- "flex w-full items-center gap-1.5 py-1 pr-2 text-left text-sm",
+ "flex w-full items-center gap-1.5 py-1 pr-2 pl-(--file-tree-row-indent) text-left text-sm",
"hover:bg-tertiary cursor-pointer",
isSelected
? "bg-interactive-hover text-contrast"
: "text-text-tertiary",
)}
// per-row indentation computed from tree depth at runtime
- style={{ paddingLeft: `${indentPx + 16}px` }}
+ style={
+ { "--file-tree-row-indent": `${indentPx + 16}px` } as CSSProperties
+ }
>
<FileIcon className="w-3.5 h-3.5 shrink-0" />
<span className="truncate">{node.name}</span>