mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-06 06:46:23 +01:00
Smooth splitter dragging (#765)
This commit is contained in:
1 parent
8a85fb3dcd
commit
1248112fe0
5 files changed
+641
-131
No files matched your search
@@ -1,11 +1,12 @@
|
||||
import classNames from "classnames";
|
||||
import type { CSSProperties, ReactNode } from "react";
|
||||
import { useCallback, useMemo, useRef } from "react";
|
||||
import { useCallback, useLayoutEffect, useMemo, useRef } from "react";
|
||||
import { useLocalStorage } from "react-use";
|
||||
import { useContainerSize } from "../hooks/useContainerSize";
|
||||
import { clamp } from "../lib/clamp";
|
||||
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";
|
||||
|
||||
@@ -63,71 +64,78 @@ export function SplitLayout({
|
||||
const renderedWidth = clampSplitRatio(width, minWidthPx, size.width);
|
||||
const renderedHeight = secondSlot ? clampSplitRatio(height, minHeightPx, size.height) : 0;
|
||||
|
||||
const styles = useMemo<CSSProperties>(() => {
|
||||
return {
|
||||
const styles = useMemo<CSSProperties>(
|
||||
() => ({
|
||||
...style,
|
||||
gridTemplate: vertical
|
||||
? `
|
||||
' ${areaL.gridArea}' minmax(0,${1 - renderedHeight}fr)
|
||||
' ${areaD.gridArea}' 0
|
||||
' ${areaR.gridArea}' minmax(0,${renderedHeight}fr)
|
||||
/ 1fr
|
||||
`
|
||||
: `
|
||||
' ${areaL.gridArea} ${areaD.gridArea} ${areaR.gridArea}' minmax(0,1fr)
|
||||
/ ${1 - renderedWidth}fr 0 ${renderedWidth}fr
|
||||
`,
|
||||
};
|
||||
}, [style, vertical, renderedHeight, renderedWidth]);
|
||||
gridTemplate: splitGridTemplate(vertical, vertical ? renderedHeight : renderedWidth),
|
||||
}),
|
||||
[style, vertical, renderedHeight, renderedWidth],
|
||||
);
|
||||
|
||||
// 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);
|
||||
}, []);
|
||||
|
||||
// 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 handleResizeMove = useCallback(
|
||||
(e: ResizeHandleEvent) => {
|
||||
if (containerRef.current === null) return;
|
||||
|
||||
const { paddingLeft, paddingRight, paddingTop, paddingBottom } = getComputedStyle(
|
||||
containerRef.current,
|
||||
);
|
||||
const $c = containerRef.current;
|
||||
const containerWidth =
|
||||
$c.clientWidth - Number.parseFloat(paddingLeft) - Number.parseFloat(paddingRight);
|
||||
const containerHeight =
|
||||
$c.clientHeight - Number.parseFloat(paddingTop) - Number.parseFloat(paddingBottom);
|
||||
|
||||
if ((vertical && containerHeight <= 0) || (!vertical && containerWidth <= 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mouseStartX = e.xStart;
|
||||
const mouseStartY = e.yStart;
|
||||
const startWidth = containerWidth * renderedWidth;
|
||||
const startHeight = containerHeight * renderedHeight;
|
||||
|
||||
if (vertical) {
|
||||
const minHeight = Math.min(minHeightPx, containerHeight);
|
||||
const maxHeightPx = Math.max(minHeight, containerHeight - minHeightPx);
|
||||
const newHeightPx = clamp(startHeight - (e.y - mouseStartY), minHeight, maxHeightPx);
|
||||
setHeight(newHeightPx / containerHeight);
|
||||
} else {
|
||||
const minWidth = Math.min(minWidthPx, containerWidth);
|
||||
const maxWidthPx = Math.max(minWidth, containerWidth - minWidthPx);
|
||||
const newWidthPx = clamp(startWidth - (e.x - mouseStartX), minWidth, maxWidthPx);
|
||||
setWidth(newWidthPx / containerWidth);
|
||||
}
|
||||
},
|
||||
[
|
||||
renderedWidth,
|
||||
renderedHeight,
|
||||
const handleResizeStart = useCallback(() => {
|
||||
liveDrag.current = createSplitDrag({
|
||||
vertical,
|
||||
minHeightPx,
|
||||
setHeight,
|
||||
minWidthPx,
|
||||
setWidth,
|
||||
],
|
||||
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);
|
||||
return vertical
|
||||
? $c.clientHeight - Number.parseFloat(paddingTop) - Number.parseFloat(paddingBottom)
|
||||
: $c.clientWidth - Number.parseFloat(paddingLeft) - Number.parseFloat(paddingRight);
|
||||
},
|
||||
apply: (ratio) => applySplit(vertical, ratio),
|
||||
commit: (ratio) => (vertical ? setHeight(ratio) : setWidth(ratio)),
|
||||
});
|
||||
}, [
|
||||
vertical,
|
||||
renderedHeight,
|
||||
renderedWidth,
|
||||
minHeightPx,
|
||||
minWidthPx,
|
||||
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 (
|
||||
@@ -136,7 +144,7 @@ export function SplitLayout({
|
||||
style={styles}
|
||||
className={classNames(className, "grid w-full h-full overflow-hidden")}
|
||||
>
|
||||
{firstSlot({ style: areaL, orientation: vertical ? "vertical" : "horizontal" })}
|
||||
{first}
|
||||
{secondSlot && (
|
||||
<>
|
||||
<ResizeHandle
|
||||
@@ -145,24 +153,16 @@ export function SplitLayout({
|
||||
resizeHandleClassName,
|
||||
vertical ? "-translate-y-1" : "-translate-x-1",
|
||||
)}
|
||||
onResizeStart={handleResizeStart}
|
||||
onResizeMove={handleResizeMove}
|
||||
onResizeEnd={handleResizeEnd}
|
||||
onReset={handleReset}
|
||||
side={vertical ? "top" : "left"}
|
||||
justify="center"
|
||||
/>
|
||||
{secondSlot({ style: areaR, orientation: vertical ? "vertical" : "horizontal" })}
|
||||
{second}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function clampSplitRatio(ratio: number, minPx: number, containerPx: number): number {
|
||||
if (containerPx <= 0 || minPx <= 0) {
|
||||
return ratio;
|
||||
}
|
||||
|
||||
const minRatio = Math.min(1, minPx / containerPx);
|
||||
const maxRatio = minRatio >= 0.5 ? minRatio : 1 - minRatio;
|
||||
return clamp(ratio, minRatio, maxRatio);
|
||||
}
|
||||
Reference in new issue
Block a user