Files
yaak-mountain-loop/packages/ui/src/components/SplitLayout.tsx
T
Gregory SchierandClaude Opus 5.5 c78732ecaf Polish Yaak Proxy's flow list, sidebar and header
Columns keep fixed widths, Path included, and the list scrolls sideways
with the header in step instead of dropping columns or capping Host.
Resize handles sit on every column boundary, a double-click resets one,
and a third click on a sorted header goes back to start time, with empty
values sorting lowest. The search box takes up to 44rem and wraps over
the list while focused, its completions as wide as it. The list/detail
handle fills the gutter. Native title tooltips become Yaak's tooltip,
shared buttons gain an opt-in for it, and folder and program rows in the
sidebar get Reveal in Finder and Copy path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:45:46 -07:00

188 lines
6.0 KiB
TypeScript

import classNames from "classnames";
import type { CSSProperties, ReactNode } from "react";
import { useCallback, useLayoutEffect, useMemo, useRef } from "react";
import { useLocalStorage } from "react-use";
import { useContainerSize } from "../hooks/useContainerSize";
import type { ResizeHandleEvent } from "./ResizeHandle";
import { ResizeHandle } from "./ResizeHandle";
import type { SplitDrag } from "./resizeDrag";
import { clampSplitRatio, createSplitDrag, splitGridTemplate } from "./resizeDrag";
export type SplitLayoutLayout = "responsive" | "horizontal" | "vertical";
export interface SlotProps {
orientation: "horizontal" | "vertical";
style: CSSProperties;
}
interface Props {
storageKey: string;
firstSlot: (props: SlotProps) => ReactNode;
secondSlot: null | ((props: SlotProps) => ReactNode);
style?: CSSProperties;
className?: string;
defaultRatio?: number;
minHeightPx?: number;
minWidthPx?: number;
layout?: SplitLayoutLayout;
resizeHandleClassName?: string;
/**
* Pixels between the panes, all of it the handle, its bar centred. Without it the panes meet
* and the handle straddles their edge.
*/
gutter?: number;
}
const baseProperties = { minHeight: 0, minWidth: 0 };
const areaL = { ...baseProperties, gridArea: "left" };
const areaR = { ...baseProperties, gridArea: "right" };
const areaD = { ...baseProperties, gridArea: "drag" };
const STACK_VERTICAL_WIDTH = 500;
export function SplitLayout({
style,
firstSlot,
secondSlot,
className,
storageKey,
layout = "responsive",
resizeHandleClassName,
gutter = 0,
defaultRatio = 0.5,
minHeightPx = 10,
minWidthPx = 10,
}: Props) {
const containerRef = useRef<HTMLDivElement>(null);
const [widthRaw, setWidth] = useLocalStorage<number>(`${storageKey}_width`);
const [heightRaw, setHeight] = useLocalStorage<number>(`${storageKey}_height`);
const width = widthRaw ?? defaultRatio;
let height = heightRaw ?? defaultRatio;
if (!secondSlot) {
height = 0;
minHeightPx = 0;
gutter = 0;
}
const size = useContainerSize(containerRef);
const verticalBasedOnSize = size.width !== 0 && size.width < STACK_VERTICAL_WIDTH;
const vertical = layout !== "horizontal" && (layout === "vertical" || verticalBasedOnSize);
const renderedWidth = clampSplitRatio(width, minWidthPx, size.width);
const renderedHeight = secondSlot ? clampSplitRatio(height, minHeightPx, size.height) : 0;
const styles = useMemo<CSSProperties>(
() => ({
...style,
gridTemplate: splitGridTemplate(vertical, vertical ? renderedHeight : renderedWidth, gutter),
}),
[style, vertical, renderedHeight, renderedWidth, gutter],
);
// While dragging, the split is written straight to the DOM and only saved (and rendered) when
// the drag ends, so the panes are not re-rendered on every move.
const liveDrag = useRef<SplitDrag | null>(null);
const applySplit = useCallback(
(vertical: boolean, ratio: number) => {
if (containerRef.current == null) return;
containerRef.current.style.gridTemplate = splitGridTemplate(vertical, ratio, gutter);
},
[gutter],
);
// Something else re-rendering mid-drag (a response arriving, say) may write the saved split
// back; put the live one back before it is painted.
useLayoutEffect(() => {
const d = liveDrag.current;
if (d?.ratio != null) applySplit(d.vertical, d.ratio);
});
const handleReset = useCallback(() => {
if (vertical) setHeight(defaultRatio);
else setWidth(defaultRatio);
}, [vertical, setHeight, defaultRatio, setWidth]);
const handleResizeStart = useCallback(() => {
liveDrag.current = createSplitDrag({
vertical,
startRatio: vertical ? renderedHeight : renderedWidth,
minPx: vertical ? minHeightPx : minWidthPx,
measure: () => {
const $c = containerRef.current;
if ($c == null) return 0;
const { paddingLeft, paddingRight, paddingTop, paddingBottom } = getComputedStyle($c);
// The panes share what the gutter leaves
return vertical
? $c.clientHeight -
Number.parseFloat(paddingTop) -
Number.parseFloat(paddingBottom) -
gutter
: $c.clientWidth -
Number.parseFloat(paddingLeft) -
Number.parseFloat(paddingRight) -
gutter;
},
apply: (ratio) => applySplit(vertical, ratio),
commit: (ratio) => (vertical ? setHeight(ratio) : setWidth(ratio)),
});
}, [
vertical,
renderedHeight,
renderedWidth,
minHeightPx,
minWidthPx,
gutter,
applySplit,
setHeight,
setWidth,
]);
const handleResizeMove = useCallback((e: ResizeHandleEvent) => liveDrag.current?.move(e), []);
const handleResizeEnd = useCallback(() => {
const d = liveDrag.current;
liveDrag.current = null;
d?.end();
}, []);
// The container's size changes often (another splitter being dragged, the window resizing) and
// re-renders this component; reuse the slots then, so the panes are only re-rendered when the
// owner re-renders or the orientation flips.
const orientation = vertical ? "vertical" : "horizontal";
const first = useMemo(() => firstSlot({ style: areaL, orientation }), [firstSlot, orientation]);
const second = useMemo(
() => secondSlot?.({ style: areaR, orientation }) ?? null,
[secondSlot, orientation],
);
return (
<div
ref={containerRef}
style={styles}
className={classNames(className, "grid w-full h-full overflow-hidden")}
>
{first}
{secondSlot && (
<>
<ResizeHandle
style={areaD}
className={classNames(
resizeHandleClassName,
gutter === 0 && (vertical ? "-translate-y-1" : "-translate-x-1"),
)}
fill={gutter > 0}
onResizeStart={handleResizeStart}
onResizeMove={handleResizeMove}
onResizeEnd={handleResizeEnd}
onReset={handleReset}
side={vertical ? "top" : "left"}
justify="center"
/>
{second}
</>
)}
</div>
);
}