Files
yaak-mountain-loop/packages/ui/src/components/ResizeHandle.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

130 lines
3.5 KiB
TypeScript

import classNames from "classnames";
import type { CSSProperties, MouseEvent as ReactMouseEvent } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { createHandleDrag } from "./resizeDrag";
export interface ResizeHandleEvent {
x: number;
y: number;
xStart: number;
yStart: number;
}
interface Props {
style?: CSSProperties;
className?: string;
onResizeStart?: () => void;
onResizeEnd?: () => void;
onResizeMove?: (e: ResizeHandleEvent) => void;
onReset?: () => void;
side: "left" | "right" | "top";
justify: "center" | "end" | "start";
/** Fills the space it is given, all of it taking the pointer, with its bar centred in it */
fill?: boolean;
}
export function ResizeHandle({
style,
justify,
className,
onResizeStart,
onResizeEnd,
onResizeMove,
onReset,
side,
fill = false,
}: Props) {
const vertical = side === "top";
const [isResizing, setIsResizing] = useState<boolean>(false);
const stopDrag = useRef<(() => void) | null>(null);
// A handle that goes away mid-drag ends the drag, so its owner is not left resizing
useEffect(() => () => stopDrag.current?.(), []);
const handlePointerDown = useCallback(
(e: ReactMouseEvent<HTMLDivElement>) => {
stopDrag.current?.();
const drag = createHandleDrag({
vertical,
xStart: e.clientX,
yStart: e.clientY,
onResizeStart: () => {
onResizeStart?.();
setIsResizing(true);
},
onResizeMove,
onResizeEnd,
});
function move(e: MouseEvent) {
drag.move(e);
}
function removeListeners() {
stopDrag.current = null;
document.documentElement.removeEventListener("mousemove", move);
document.documentElement.removeEventListener("mouseup", up);
}
function up() {
removeListeners();
setIsResizing(false);
drag.up();
}
stopDrag.current = () => {
removeListeners();
drag.abort();
};
document.documentElement.addEventListener("mousemove", move);
document.documentElement.addEventListener("mouseup", up);
},
[onResizeEnd, onResizeMove, onResizeStart, vertical],
);
return (
<div
aria-hidden
style={style}
onDoubleClick={onReset}
onPointerDown={handlePointerDown}
className={classNames(
className,
"group z-10 flex select-none",
vertical ? "cursor-row-resize" : "cursor-col-resize",
fill
? "w-full h-full items-center"
: "transition-colors hover:bg-surface-active rounded-full",
!fill && (vertical ? "w-full h-1.5" : "h-full w-1.5"),
justify === "center" && "justify-center",
justify === "end" && "justify-end",
justify === "start" && "justify-start",
side === "right" && "right-0",
side === "left" && "left-0",
side === "top" && "top-0",
)}
>
{fill && (
<div
className={classNames(
"rounded-full transition-colors group-hover:bg-surface-active",
vertical ? "w-full h-1.5" : "h-full w-1.5",
isResizing && "bg-surface-active",
)}
/>
)}
{isResizing && (
<div
className={classNames(
"fixed left-[-100vw] right-[-100vw] top-[-100vh] bottom-[-100vh]",
vertical && "cursor-row-resize",
!vertical && "cursor-col-resize",
)}
/>
)}
</div>
);
}