Skip to content
TypeScript

feat(web): allow resizing the file explorer (#25737)

mariaf06dd4bbf5ad
diff --git a/apps/web/src/components/files/FileExplorerPane.tsx b/apps/web/src/components/files/FileExplorerPane.tsx
new file mode 100644
index 00000000000..c13a814f630
--- /dev/null
+++ b/apps/web/src/components/files/FileExplorerPane.tsx
@@ -0,0 +1,90 @@
+import { type CSSProperties, type ReactNode, useState } from "react";
+
+import { RightPanelResizeHandle } from "~/components/preview/RightPanelResizeHandle";
+import { RESIZABLE_WIDTH_PROPERTY, useResizableWidth } from "~/hooks/useResizableWidth";
+import { observeResize } from "~/lib/observeResize";
+import { cn } from "~/lib/utils";
+
+const MIN_WIDTH = 160;
+const MAX_WIDTH = 560;
+const FILE_CONTENT_MIN_WIDTH = 256;
+
+export function FileExplorerPane({
+ resizable,
+ children,
+}: {
+ resizable: boolean;
+ children: ReactNode;
+}) {
+ const [maxWidth, setMaxWidth] = useState(MAX_WIDTH);
+ const { width, setWidth, handlers } = useResizableWidth({
+ storageKey: "t3code.fileExplorerWidth",
+ defaultWidth: 256,
+ minWidth: MIN_WIDTH,
+ maxWidth,
+ edge: "left",
+ });
+
+ return (
+ <aside
+ ref={(host) => {
+ const row = host?.parentElement;
+ if (!row || !resizable) return;
+ const measure = () =>
+ setMaxWidth(
+ Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, row.clientWidth - FILE_CONTENT_MIN_WIDTH)),
+ );
+ measure();
+ return observeResize(row, measure);
+ }}
+ aria-label="File explorer"
+ className={cn(
+ "relative flex min-h-0 min-w-0 shrink-0 bg-background",
+ resizable ? "border-l border-border/60" : "flex-1",
+ )}
+ style={
+ resizable
+ ? ({
+ [RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
+ width: `var(${RESIZABLE_WIDTH_PROPERTY})`,
+ } as CSSProperties)
+ : undefined
+ }
+ >
+ {resizable ? (
+ <RightPanelResizeHandle
+ handlers={handlers}
+ label="Resize file explorer"
+ tabIndex={0}
+ aria-valuenow={width}
+ aria-valuemin={MIN_WIDTH}
+ aria-valuemax={maxWidth}
+ onKeyDown={(event) => {
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
+ let nextWidth: number;
+ switch (event.key) {
+ case "ArrowLeft":
+ nextWidth = width + 16;
+ break;
+ case "ArrowRight":
+ nextWidth = width - 16;
+ break;
+ case "Home":
+ nextWidth = MIN_WIDTH;
+ break;
+ case "End":
+ nextWidth = maxWidth;
+ break;
+ default:
+ return;
+ }
+ event.preventDefault();
+ event.stopPropagation();
+ setWidth(nextWidth);
+ }}
+ />
+ ) : null}
+ {children}
+ </aside>
+ );
+}
diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsx
index 03e682758e3..e9b257e3e31 100644
--- a/apps/web/src/components/files/FilePreviewPanel.tsx
+++ b/apps/web/src/components/files/FilePreviewPanel.tsx
@@ -84,6 +84,7 @@ import { BrowserDocumentFrame, isPdfPreviewFile } from "./BrowserDocumentFrame";
import { DelimitedTablePreview } from "./DelimitedTablePreview";
import { PreviewImage } from "./PreviewImage";
import FileBrowserPanel from "./FileBrowserPanel";
+import { FileExplorerPane } from "./FileExplorerPane";
import { FileBreadcrumbs } from "./FileBreadcrumbs";
import { breadcrumbPathOf, retainedBreadcrumbTrail, trailChildOf } from "./filePath";
import { FileMarkdownPreview } from "./FileMarkdownPreview";
@@ -1513,14 +1514,7 @@ export default function FilePreviewPanel({
) : null}
</div>
{showExplorer ? (
- <aside
- className={cn(
- "flex min-h-0 shrink-0 bg-background",
- previewPath
- ? "w-[min(22rem,46%)] min-w-64 border-l border-border/60"
- : "min-w-0 flex-1",
- )}
- >
+ <FileExplorerPane resizable={Boolean(previewPath)}>
<FileBrowserPanel
key={`${environmentId}:${hostFolderPath ?? cwd}`}
environmentId={environmentId}
@@ -1544,7 +1538,7 @@ export default function FilePreviewPanel({
}
: {})}
/>
- </aside>
+ </FileExplorerPane>
) : null}
</div>
</div>
diff --git a/apps/web/src/components/preview/RightPanelResizeHandle.tsx b/apps/web/src/components/preview/RightPanelResizeHandle.tsx
index dd644ccf0c8..851daaebe5e 100644
--- a/apps/web/src/components/preview/RightPanelResizeHandle.tsx
+++ b/apps/web/src/components/preview/RightPanelResizeHandle.tsx
@@ -1,16 +1,23 @@
+import type { ComponentProps } from "react";
import type { ResizableWidthHandlers } from "~/hooks/useResizableWidth";
import { cn } from "~/lib/utils";
-interface Props {
+interface Props extends Pick<
+ ComponentProps<"div">,
+ "tabIndex" | "onKeyDown" | "aria-valuenow" | "aria-valuemin" | "aria-valuemax"
+> {
handlers: ResizableWidthHandlers;
className?: string;
+ label?: string;
}
-export function RightPanelResizeHandle({ handlers, className }: Props) {
+export function RightPanelResizeHandle({ handlers, className, label, ...separatorProps }: Props) {
return (
<div
role="separator"
+ aria-label={label}
aria-orientation="vertical"
+ {...separatorProps}
className={cn(
"group absolute inset-y-0 -left-1 z-20 w-2 cursor-col-resize select-none",
className,
@@ -19,7 +26,7 @@ export function RightPanelResizeHandle({ handlers, className }: Props) {
>
<span
aria-hidden
- 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"
+ 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"
/>
</div>
);
diff --git a/apps/web/src/hooks/useResizableWidth.ts b/apps/web/src/hooks/useResizableWidth.ts
index 16bb1982913..50fdba38a9f 100644
--- a/apps/web/src/hooks/useResizableWidth.ts
+++ b/apps/web/src/hooks/useResizableWidth.ts
@@ -29,6 +29,7 @@ export interface ResizableWidthHandlers {
/** Persist on drag end to avoid a storage write on every animation frame. */
export function useResizableWidth(options: UseResizableWidthOptions): {
readonly width: number;
+ readonly setWidth: (width: number) => void;
readonly handlers: ResizableWidthHandlers;
} {
const { storageKey, defaultWidth, minWidth, maxWidth, edge } = options;
@@ -55,6 +56,16 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
const clampedWidth = clamp(widthState.width);
+ const commitWidth = (nextStorageKey: string, nextWidth: number) => {
+ // Resizing remains usable for this session when browser storage is unavailable.
+ setWidthState({ storageKey: nextStorageKey, width: nextWidth });
+ try {
+ setLocalStorageItem(nextStorageKey, nextWidth, WidthSchema);
+ } catch (error) {
+ console.error("Could not persist panel width.", error);
+ }
+ };
+
const drag = useResizeDrag<HTMLElement>((event) => {
const host = event.currentTarget.parentElement;
// Disable drag transitions through transition-property; the host renders transition-duration.
@@ -72,12 +83,7 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
return nextWidth;
},
finish(finalWidth) {
- setWidthState({ storageKey: startStorageKey, width: finalWidth });
- try {
- setLocalStorageItem(startStorageKey, finalWidth, WidthSchema);
- } catch (error) {
- console.error("Could not persist panel width.", error);
- }
+ commitWidth(startStorageKey, finalWidth);
},
cleanup(committed) {
host?.style.removeProperty("transition-property");
@@ -94,6 +100,7 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
return {
width: clampedWidth,
+ setWidth: (value) => commitWidth(storageKey, clamp(value)),
handlers: {
ref: drag.ref,
onPointerDown: drag.onPointerDown,