The platform for continuously improving AI agents.
import { type ReactNode, useRef, useState } from "react";
import { cn } from "@/lib/utils";
export function SplitPane({
start,
end,
defaultWidth = 360,
minWidth = 240,
maxWidth = 880,
storageKey,
className,
}: {
start: ReactNode;
end: ReactNode;
defaultWidth?: number;
minWidth?: number;
maxWidth?: number;
storageKey?: string;
className?: string;
}) {
const frame = useRef<HTMLDivElement>(null);
const drag = useRef(false);
const [width, setWidth] = useState(() => readWidth(storageKey, defaultWidth, minWidth, maxWidth));
const [dragging, setDragging] = useState(false);
function resize(clientX: number) {
const bounds = frame.current?.getBoundingClientRect();
if (!bounds) return;
const limit = Math.max(minWidth, Math.min(maxWidth, bounds.width - 280));
const next = Math.min(limit, Math.max(minWidth, clientX - bounds.left));
setWidth(next);
return next;
}
function remember(next: number) {
if (!storageKey) return;
try {
window.localStorage.setItem(storageKey, String(Math.round(next)));
} catch {
// Private mode can reject storage writes. The drag still applies for this view.
}
}
return (
<div className={cn("flex min-h-0 min-w-0", dragging && "select-none", className)} ref={frame}>
<div className="flex min-h-0 shrink-0 flex-col overflow-hidden" style={{ width }}>
{start}
</div>
<div
aria-label="Resize panes"
aria-orientation="vertical"
aria-valuemax={maxWidth}
aria-valuemin={minWidth}
aria-valuenow={Math.round(width)}
className="group relative w-3 shrink-0 cursor-col-resize outline-none"
onKeyDown={(event) => {
const step = event.shiftKey ? 48 : 16;
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
event.preventDefault();
const bounds = frame.current?.getBoundingClientRect();
const limit = bounds
? Math.max(minWidth, Math.min(maxWidth, bounds.width - 280))
: maxWidth;
const next = Math.min(
limit,
Math.max(minWidth, width + (event.key === "ArrowRight" ? step : -step))
);
setWidth(next);
remember(next);
}}
onPointerCancel={() => {
drag.current = false;
setDragging(false);
}}
onPointerDown={(event) => {
try {
event.currentTarget.setPointerCapture(event.pointerId);
} catch {
// Capture is unavailable for a pointer the browser did not start.
}
drag.current = true;
setDragging(true);
resize(event.clientX);
}}
onPointerMove={(event) => {
if (!drag.current) return;
resize(event.clientX);
}}
onPointerUp={(event) => {
drag.current = false;
setDragging(false);
const next = resize(event.clientX);
if (next !== undefined) remember(next);
}}
role="separator"
tabIndex={0}
>
<span
className={cn(
"absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-border transition-colors",
"group-hover:bg-foreground/45 group-focus-visible:bg-foreground/70",
dragging && "bg-foreground/70"
)}
/>
</div>
<div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">{end}</div>
</div>
);
}
function readWidth(key: string | undefined, fallback: number, min: number, max: number): number {
if (!key || typeof window === "undefined") return fallback;
try {
const raw = window.localStorage.getItem(key);
if (raw === null) return fallback;
const value = Number(raw);
if (!Number.isFinite(value)) return fallback;
return Math.min(max, Math.max(min, value));
} catch {
return fallback;
}
}