Open History to see the last commit
1import { type CSSProperties, type ReactNode, useState } from "react";
2
3import { RightPanelResizeHandle } from "~/components/preview/RightPanelResizeHandle";
4import { RESIZABLE_WIDTH_PROPERTY, useResizableWidth } from "~/hooks/useResizableWidth";
5import { observeResize } from "~/lib/observeResize";
6import { cn } from "~/lib/utils";
7
8const MIN_WIDTH = 160;
9const MAX_WIDTH = 560;
10const FILE_CONTENT_MIN_WIDTH = 256;
11
12export 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}