Skip to content

Commit f06dd4b

Browse files
maria-rcks07rjain
andauthored
feat(web): allow resizing the file explorer (pingdotgg#25737)
Co-authored-by: Rishabh Bothra <[email protected]>
1 parent d796063 commit f06dd4b

4 files changed

Lines changed: 116 additions & 18 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { type CSSProperties, type ReactNode, 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 [maxWidth, setMaxWidth] = useState(MAX_WIDTH);
20+
const { width, setWidth, handlers } = useResizableWidth({
21+
storageKey: "t3code.fileExplorerWidth",
22+
defaultWidth: 256,
23+
minWidth: MIN_WIDTH,
24+
maxWidth,
25+
edge: "left",
26+
});
27+
28+
return (
29+
<aside
30+
ref={(host) => {
31+
const row = host?.parentElement;
32+
if (!row || !resizable) return;
33+
const measure = () =>
34+
setMaxWidth(
35+
Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, row.clientWidth - FILE_CONTENT_MIN_WIDTH)),
36+
);
37+
measure();
38+
return observeResize(row, measure);
39+
}}
40+
aria-label="File explorer"
41+
className={cn(
42+
"relative flex min-h-0 min-w-0 shrink-0 bg-background",
43+
resizable ? "border-l border-border/60" : "flex-1",
44+
)}
45+
style={
46+
resizable
47+
? ({
48+
[RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
49+
width: `var(${RESIZABLE_WIDTH_PROPERTY})`,
50+
} as CSSProperties)
51+
: undefined
52+
}
53+
>
54+
{resizable ? (
55+
<RightPanelResizeHandle
56+
handlers={handlers}
57+
label="Resize file explorer"
58+
tabIndex={0}
59+
aria-valuenow={width}
60+
aria-valuemin={MIN_WIDTH}
61+
aria-valuemax={maxWidth}
62+
onKeyDown={(event) => {
63+
if (event.altKey || event.ctrlKey || event.metaKey) return;
64+
let nextWidth: number;
65+
switch (event.key) {
66+
case "ArrowLeft":
67+
nextWidth = width + 16;
68+
break;
69+
case "ArrowRight":
70+
nextWidth = width - 16;
71+
break;
72+
case "Home":
73+
nextWidth = MIN_WIDTH;
74+
break;
75+
case "End":
76+
nextWidth = maxWidth;
77+
break;
78+
default:
79+
return;
80+
}
81+
event.preventDefault();
82+
event.stopPropagation();
83+
setWidth(nextWidth);
84+
}}
85+
/>
86+
) : null}
87+
{children}
88+
</aside>
89+
);
90+
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import { BrowserDocumentFrame, isPdfPreviewFile } from "./BrowserDocumentFrame";
8484
import { DelimitedTablePreview } from "./DelimitedTablePreview";
8585
import { PreviewImage } from "./PreviewImage";
8686
import FileBrowserPanel from "./FileBrowserPanel";
87+
import { FileExplorerPane } from "./FileExplorerPane";
8788
import { FileBreadcrumbs } from "./FileBreadcrumbs";
8889
import { breadcrumbPathOf, retainedBreadcrumbTrail, trailChildOf } from "./filePath";
8990
import { FileMarkdownPreview } from "./FileMarkdownPreview";
@@ -1513,14 +1514,7 @@ export default function FilePreviewPanel({
15131514
) : null}
15141515
</div>
15151516
{showExplorer ? (
1516-
<aside
1517-
className={cn(
1518-
"flex min-h-0 shrink-0 bg-background",
1519-
previewPath
1520-
? "w-[min(22rem,46%)] min-w-64 border-l border-border/60"
1521-
: "min-w-0 flex-1",
1522-
)}
1523-
>
1517+
<FileExplorerPane resizable={Boolean(previewPath)}>
15241518
<FileBrowserPanel
15251519
key={`${environmentId}:${hostFolderPath ?? cwd}`}
15261520
environmentId={environmentId}
@@ -1544,7 +1538,7 @@ export default function FilePreviewPanel({
15441538
}
15451539
: {})}
15461540
/>
1547-
</aside>
1541+
</FileExplorerPane>
15481542
) : null}
15491543
</div>
15501544
</div>

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
import type { ComponentProps } from "react";
12
import type { ResizableWidthHandlers } from "~/hooks/useResizableWidth";
23
import { cn } from "~/lib/utils";
34

4-
interface Props {
5+
interface Props extends Pick<
6+
ComponentProps<"div">,
7+
"tabIndex" | "onKeyDown" | "aria-valuenow" | "aria-valuemin" | "aria-valuemax"
8+
> {
59
handlers: ResizableWidthHandlers;
610
className?: string;
11+
label?: string;
712
}
813

9-
export function RightPanelResizeHandle({ handlers, className }: Props) {
14+
export function RightPanelResizeHandle({ handlers, className, label, ...separatorProps }: Props) {
1015
return (
1116
<div
1217
role="separator"
18+
aria-label={label}
1319
aria-orientation="vertical"
20+
{...separatorProps}
1421
className={cn(
1522
"group absolute inset-y-0 -left-1 z-20 w-2 cursor-col-resize select-none",
1623
className,
@@ -19,7 +26,7 @@ export function RightPanelResizeHandle({ handlers, className }: Props) {
1926
>
2027
<span
2128
aria-hidden
22-
className="pointer-events-none absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-transparent transition-colors duration-150 group-hover:bg-border group-active:bg-primary/60"
29+
className="pointer-events-none absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-transparent transition-colors duration-150 group-hover:bg-border group-focus-visible:bg-primary/60 group-active:bg-primary/60"
2330
/>
2431
</div>
2532
);

‎apps/web/src/hooks/useResizableWidth.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ResizableWidthHandlers {
2929
/** Persist on drag end to avoid a storage write on every animation frame. */
3030
export function useResizableWidth(options: UseResizableWidthOptions): {
3131
readonly width: number;
32+
readonly setWidth: (width: number) => void;
3233
readonly handlers: ResizableWidthHandlers;
3334
} {
3435
const { storageKey, defaultWidth, minWidth, maxWidth, edge } = options;
@@ -55,6 +56,16 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
5556

5657
const clampedWidth = clamp(widthState.width);
5758

59+
const commitWidth = (nextStorageKey: string, nextWidth: number) => {
60+
// Resizing remains usable for this session when browser storage is unavailable.
61+
setWidthState({ storageKey: nextStorageKey, width: nextWidth });
62+
try {
63+
setLocalStorageItem(nextStorageKey, nextWidth, WidthSchema);
64+
} catch (error) {
65+
console.error("Could not persist panel width.", error);
66+
}
67+
};
68+
5869
const drag = useResizeDrag<HTMLElement>((event) => {
5970
const host = event.currentTarget.parentElement;
6071
// Disable drag transitions through transition-property; the host renders transition-duration.
@@ -72,12 +83,7 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
7283
return nextWidth;
7384
},
7485
finish(finalWidth) {
75-
setWidthState({ storageKey: startStorageKey, width: finalWidth });
76-
try {
77-
setLocalStorageItem(startStorageKey, finalWidth, WidthSchema);
78-
} catch (error) {
79-
console.error("Could not persist panel width.", error);
80-
}
86+
commitWidth(startStorageKey, finalWidth);
8187
},
8288
cleanup(committed) {
8389
host?.style.removeProperty("transition-property");
@@ -94,6 +100,7 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
94100

95101
return {
96102
width: clampedWidth,
103+
setWidth: (value) => commitWidth(storageKey, clamp(value)),
97104
handlers: {
98105
ref: drag.ref,
99106
onPointerDown: drag.onPointerDown,

0 commit comments

Comments
 (0)