Skip to content

Commit b95096c

Browse files
committed
feat(web): allow resizing the file explorer
1 parent b16728a commit b95096c

4 files changed

Lines changed: 179 additions & 10 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
import type { Locator } from "vite-plus/test/browser";
3+
4+
import { render } from "../../../test/render";
5+
import { FileExplorerPane } from "./FileExplorerPane";
6+
7+
const STORAGE_KEY = "t3code.fileExplorerWidth";
8+
9+
function PreviewRow({ width = 1000, resizable = true }: { width?: number; resizable?: boolean }) {
10+
return (
11+
<div style={{ display: "flex", width, height: 300 }}>
12+
<div style={{ flex: 1, minWidth: 0 }}>File contents</div>
13+
<FileExplorerPane resizable={resizable}>Files</FileExplorerPane>
14+
</div>
15+
);
16+
}
17+
18+
function drag(handle: HTMLElement, delta: number, finish = true) {
19+
const init = { bubbles: true, button: 0, pointerId: 1, clientX: 600 };
20+
handle.dispatchEvent(new PointerEvent("pointerdown", init));
21+
handle.dispatchEvent(new PointerEvent("pointermove", { ...init, clientX: 600 + delta }));
22+
if (finish) {
23+
handle.dispatchEvent(new PointerEvent("pointerup", { ...init, clientX: 600 + delta }));
24+
}
25+
}
26+
27+
async function expectWidth(explorer: Locator, width: number) {
28+
await vi.waitFor(() => expect(explorer.element().getBoundingClientRect().width).toBe(width));
29+
}
30+
31+
function storedWidth(width: number) {
32+
const previous = localStorage.getItem(STORAGE_KEY);
33+
localStorage.setItem(STORAGE_KEY, JSON.stringify(width));
34+
return {
35+
[Symbol.dispose]() {
36+
if (previous === null) localStorage.removeItem(STORAGE_KEY);
37+
else localStorage.setItem(STORAGE_KEY, previous);
38+
},
39+
};
40+
}
41+
42+
describe("FileExplorerPane", () => {
43+
it("restores the saved width after narrowing and widening the preview row", async () => {
44+
using _storage = storedWidth(480);
45+
await using screen = await render(<PreviewRow />);
46+
const explorer = screen.getByRole("complementary", { name: "File explorer" });
47+
await expectWidth(explorer, 480);
48+
49+
await screen.rerender(<PreviewRow width={600} />);
50+
await expectWidth(explorer, 344);
51+
expect(localStorage.getItem(STORAGE_KEY)).toBe("480");
52+
53+
await screen.rerender(<PreviewRow width={360} />);
54+
await expectWidth(explorer, 160);
55+
56+
await screen.rerender(<PreviewRow />);
57+
await expectWidth(explorer, 480);
58+
});
59+
60+
it("clamps dragging to the row and persists the visible width on release", async () => {
61+
using _storage = storedWidth(256);
62+
await using screen = await render(<PreviewRow width={600} />);
63+
// Synthetic pointer ids are not active browser pointers.
64+
using _capture = vi
65+
.spyOn(HTMLElement.prototype, "setPointerCapture")
66+
.mockImplementation(() => {});
67+
const handle = screen
68+
.getByRole("separator", { name: "Resize file explorer" })
69+
.element() as HTMLElement;
70+
const explorer = screen.getByRole("complementary", { name: "File explorer" });
71+
72+
drag(handle, -1000);
73+
await expectWidth(explorer, 344);
74+
expect(localStorage.getItem(STORAGE_KEY)).toBe("344");
75+
76+
drag(handle, 40);
77+
await expectWidth(explorer, 304);
78+
expect(localStorage.getItem(STORAGE_KEY)).toBe("304");
79+
80+
drag(handle, 1000);
81+
await expectWidth(explorer, 160);
82+
expect(localStorage.getItem(STORAGE_KEY)).toBe("160");
83+
});
84+
85+
it("cleans up an active drag when the explorer is hidden", async () => {
86+
using _storage = storedWidth(256);
87+
await using screen = await render(<PreviewRow />);
88+
using _capture = vi
89+
.spyOn(HTMLElement.prototype, "setPointerCapture")
90+
.mockImplementation(() => {});
91+
const handle = screen
92+
.getByRole("separator", { name: "Resize file explorer" })
93+
.element() as HTMLElement;
94+
drag(handle, -80, false);
95+
expect(document.body.style.cursor).toBe("col-resize");
96+
97+
await screen.unmount();
98+
expect(document.body.style.cursor).toBe("");
99+
expect(document.body.style.userSelect).toBe("");
100+
expect(localStorage.getItem(STORAGE_KEY)).toBe("256");
101+
});
102+
103+
it("does not constrain the explorer or show a splitter when no file is open", async () => {
104+
using _storage = storedWidth(480);
105+
await using screen = await render(<PreviewRow resizable={false} />);
106+
await expect.element(screen.getByRole("separator")).not.toBeInTheDocument();
107+
expect((screen.getByRole("complementary").element() as HTMLElement).style.width).toBe("");
108+
});
109+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { type CSSProperties, type ReactNode, useLayoutEffect, useRef, useState } from "react";
2+
3+
import { RightPanelResizeHandle } from "~/components/preview/RightPanelResizeHandle";
4+
import { RESIZABLE_WIDTH_PROPERTY, useResizableWidth } from "~/hooks/useResizableWidth";
5+
import { observeResize } from "~/lib/observeResize";
6+
import { cn } from "~/lib/utils";
7+
8+
const MIN_WIDTH = 160;
9+
const MAX_WIDTH = 560;
10+
const FILE_CONTENT_MIN_WIDTH = 256;
11+
12+
export function FileExplorerPane({
13+
resizable,
14+
children,
15+
}: {
16+
resizable: boolean;
17+
children: ReactNode;
18+
}) {
19+
const hostRef = useRef<HTMLElement>(null);
20+
const [maxWidth, setMaxWidth] = useState(MAX_WIDTH);
21+
// oxlint-disable-next-line t3code/react-effect-budget -- Synchronize the drag bounds with the preview row's measured width.
22+
useLayoutEffect(() => {
23+
const row = hostRef.current?.parentElement;
24+
if (!row || !resizable) return;
25+
const measure = () => {
26+
setMaxWidth(
27+
Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, row.clientWidth - FILE_CONTENT_MIN_WIDTH)),
28+
);
29+
};
30+
measure();
31+
return observeResize(row, measure);
32+
}, [resizable]);
33+
const { width, handlers } = useResizableWidth({
34+
storageKey: "t3code.fileExplorerWidth",
35+
defaultWidth: 256,
36+
minWidth: MIN_WIDTH,
37+
maxWidth,
38+
edge: "left",
39+
});
40+
41+
return (
42+
<aside
43+
ref={hostRef}
44+
aria-label="File explorer"
45+
className={cn(
46+
"relative flex min-h-0 min-w-0 shrink-0 bg-background",
47+
resizable ? "border-l border-border/60" : "flex-1",
48+
)}
49+
style={
50+
resizable
51+
? ({
52+
[RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
53+
width: `var(${RESIZABLE_WIDTH_PROPERTY})`,
54+
} as CSSProperties)
55+
: undefined
56+
}
57+
>
58+
{resizable ? (
59+
<RightPanelResizeHandle handlers={handlers} label="Resize file explorer" />
60+
) : null}
61+
{children}
62+
</aside>
63+
);
64+
}

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import { AudioPreview } from "./AudioPreview";
7878
import { BrowserDocumentFrame, isPdfPreviewFile } from "./BrowserDocumentFrame";
7979
import { DelimitedTablePreview } from "./DelimitedTablePreview";
8080
import FileBrowserPanel from "./FileBrowserPanel";
81+
import { FileExplorerPane } from "./FileExplorerPane";
8182
import { FileBreadcrumbs } from "./FileBreadcrumbs";
8283
import { breadcrumbPathOf, retainedBreadcrumbTrail, trailChildOf } from "./filePath";
8384
import { FileMarkdownPreview } from "./FileMarkdownPreview";
@@ -1449,14 +1450,7 @@ export default function FilePreviewPanel({
14491450
) : null}
14501451
</div>
14511452
{showExplorer ? (
1452-
<aside
1453-
className={cn(
1454-
"flex min-h-0 shrink-0 bg-background",
1455-
previewPath
1456-
? "w-[min(22rem,46%)] min-w-64 border-l border-border/60"
1457-
: "min-w-0 flex-1",
1458-
)}
1459-
>
1453+
<FileExplorerPane resizable={previewPath !== null}>
14601454
<FileBrowserPanel
14611455
key={`${environmentId}:${hostFolderPath ?? cwd}`}
14621456
environmentId={environmentId}
@@ -1475,7 +1469,7 @@ export default function FilePreviewPanel({
14751469
? { onRefreshSelectedFile: file.refresh }
14761470
: {})}
14771471
/>
1478-
</aside>
1472+
</FileExplorerPane>
14791473
) : null}
14801474
</div>
14811475
</div>

‎apps/web/src/components/preview/RightPanelResizeHandle.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { cn } from "~/lib/utils";
44
interface Props {
55
handlers: ResizableWidthHandlers;
66
className?: string;
7+
label?: string;
78
}
89

9-
export function RightPanelResizeHandle({ handlers, className }: Props) {
10+
export function RightPanelResizeHandle({ handlers, className, label }: Props) {
1011
return (
1112
<div
1213
role="separator"
14+
aria-label={label}
1315
aria-orientation="vertical"
1416
className={cn(
1517
"group absolute inset-y-0 -left-1 z-20 w-2 cursor-col-resize select-none",

0 commit comments

Comments
 (0)