Skip to content

Commit 61f1172

Browse files
fix PageLayout resize not only if mouse is on resize gap
1 parent 0193614 commit 61f1172

3 files changed

Lines changed: 50 additions & 19 deletions

File tree

src/components/layout/page-layout/PageLayout.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface PageSidebarProps {
2525
split: number;
2626
resizeHandleSize: number;
2727
islandGap: number;
28+
onResizeStart: () => void;
2829
onResizeSplit: (delta: number) => void;
2930
}
3031

@@ -45,6 +46,10 @@ export function PageLayout({
4546
rightWidth,
4647
leftSplit,
4748
rightSplit,
49+
startResizeLeftSidebar,
50+
startResizeRightSidebar,
51+
startResizeLeftSidebarSplit,
52+
startResizeRightSidebarSplit,
4853
resizeLeftSidebar,
4954
resizeRightSidebar,
5055
resizeLeftSidebarSplit,
@@ -92,11 +97,13 @@ export function PageLayout({
9297
split={leftSplit}
9398
resizeHandleSize={RESIZE_HANDLE_SIZE}
9499
islandGap={ISLAND_GAP}
100+
onResizeStart={startResizeLeftSidebarSplit}
95101
onResizeSplit={resizeLeftSidebarSplit}
96102
/>
97103

98104
<ResizeHandle
99105
orientation="vertical"
106+
onResizeStart={startResizeLeftSidebar}
100107
onResize={resizeLeftSidebar}
101108
size={RESIZE_HANDLE_SIZE}
102109
gapSize={ISLAND_GAP}
@@ -137,6 +144,7 @@ export function PageLayout({
137144
>
138145
<ResizeHandle
139146
orientation="vertical"
147+
onResizeStart={startResizeRightSidebar}
140148
onResize={resizeRightSidebar}
141149
size={RESIZE_HANDLE_SIZE}
142150
gapSize={ISLAND_GAP}
@@ -153,6 +161,7 @@ export function PageLayout({
153161
split={rightSplit}
154162
resizeHandleSize={RESIZE_HANDLE_SIZE}
155163
islandGap={ISLAND_GAP}
164+
onResizeStart={startResizeRightSidebarSplit}
156165
onResizeSplit={resizeRightSidebarSplit}
157166
/>
158167
</Box>
@@ -170,6 +179,7 @@ function PageSidebar({
170179
split,
171180
resizeHandleSize,
172181
islandGap,
182+
onResizeStart,
173183
onResizeSplit,
174184
}: PageSidebarProps) {
175185
const hasTopContent = hasContent(topContent);
@@ -198,6 +208,7 @@ function PageSidebar({
198208

199209
<ResizeHandle
200210
orientation="horizontal"
211+
onResizeStart={onResizeStart}
201212
onResize={onResizeSplit}
202213
size={resizeHandleSize}
203214
gapSize={islandGap}

src/components/layout/page-layout/ResizeHandle.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PointerEvent, useRef } from "react";
33

44
interface ResizeHandleProps {
55
orientation: "horizontal" | "vertical";
6+
onResizeStart: () => void;
67
onResize: (delta: number) => void;
78
size: number;
89
gapSize: number;
@@ -13,6 +14,7 @@ interface ResizeHandleProps {
1314

1415
export function ResizeHandle({
1516
orientation,
17+
onResizeStart,
1618
onResize,
1719
size,
1820
gapSize,
@@ -30,6 +32,7 @@ export function ResizeHandle({
3032
event.preventDefault();
3133

3234
startPositionRef.current = getPointerPosition(event);
35+
onResizeStart();
3336
event.currentTarget.setPointerCapture(event.pointerId);
3437

3538
document.body.style.cursor = cursor;
@@ -43,7 +46,6 @@ export function ResizeHandle({
4346

4447
const currentPosition = getPointerPosition(event);
4548
const movement = currentPosition - startPositionRef.current;
46-
startPositionRef.current = currentPosition;
4749

4850
onResize(invertDelta ? -movement : movement);
4951
};

src/components/layout/page-layout/useResize.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect, useRef, useState } from "react";
22

33
const DEFAULT_SIDEBAR_WIDTH = 300;
44
const DEFAULT_SIDEBAR_SPLIT = 50;
@@ -28,6 +28,10 @@ export function useResize({
2828
const [leftSplit, setLeftSplit] = useState(DEFAULT_SIDEBAR_SPLIT);
2929
const [rightSplit, setRightSplit] = useState(DEFAULT_SIDEBAR_SPLIT);
3030
const [storageLoaded, setStorageLoaded] = useState(false);
31+
const leftWidthAtDragStart = useRef(leftWidth);
32+
const rightWidthAtDragStart = useRef(rightWidth);
33+
const leftSplitAtDragStart = useRef(leftSplit);
34+
const rightSplitAtDragStart = useRef(rightSplit);
3135

3236
useEffect(() => {
3337
// eslint-disable-next-line react-hooks/set-state-in-effect
@@ -83,50 +87,64 @@ export function useResize({
8387
};
8488

8589
const resizeLeftSidebar = (delta: number) => {
86-
setLeftWidth((currentWidth) => {
87-
const maximumWidth = getMaximumSidebarWidth(rightWidth, hasRightSidebar);
90+
const maximumWidth = getMaximumSidebarWidth(rightWidth, hasRightSidebar);
8891

89-
return Math.min(
92+
setLeftWidth(
93+
clamp(
94+
leftWidthAtDragStart.current + delta,
95+
MIN_SIDEBAR_WIDTH,
9096
maximumWidth,
91-
Math.max(MIN_SIDEBAR_WIDTH, currentWidth + delta),
92-
);
93-
});
97+
),
98+
);
9499
};
95100

96101
const resizeRightSidebar = (delta: number) => {
97-
setRightWidth((currentWidth) => {
98-
const maximumWidth = getMaximumSidebarWidth(leftWidth, hasLeftSidebar);
102+
const maximumWidth = getMaximumSidebarWidth(leftWidth, hasLeftSidebar);
99103

100-
return Math.min(
104+
setRightWidth(
105+
clamp(
106+
rightWidthAtDragStart.current + delta,
107+
MIN_SIDEBAR_WIDTH,
101108
maximumWidth,
102-
Math.max(MIN_SIDEBAR_WIDTH, currentWidth + delta),
103-
);
104-
});
109+
),
110+
);
105111
};
106112

107-
const getResizedSplit = (currentSplit: number, delta: number) => {
113+
const getResizedSplit = (splitAtDragStart: number, delta: number) => {
108114
const availableHeight = window.innerHeight - islandGap * 3;
109115

110116
if (availableHeight <= 0) {
111-
return currentSplit;
117+
return splitAtDragStart;
112118
}
113119

114-
return clamp(currentSplit + (delta / availableHeight) * 100, 0, 100);
120+
return clamp(splitAtDragStart + (delta / availableHeight) * 100, 0, 100);
115121
};
116122

117123
const resizeLeftSidebarSplit = (delta: number) => {
118-
setLeftSplit((currentSplit) => getResizedSplit(currentSplit, delta));
124+
setLeftSplit(getResizedSplit(leftSplitAtDragStart.current, delta));
119125
};
120126

121127
const resizeRightSidebarSplit = (delta: number) => {
122-
setRightSplit((currentSplit) => getResizedSplit(currentSplit, delta));
128+
setRightSplit(getResizedSplit(rightSplitAtDragStart.current, delta));
123129
};
124130

125131
return {
126132
leftWidth,
127133
rightWidth,
128134
leftSplit,
129135
rightSplit,
136+
startResizeLeftSidebar: () => {
137+
leftWidthAtDragStart.current = leftWidth;
138+
},
139+
startResizeRightSidebar: () => {
140+
rightWidthAtDragStart.current = rightWidth;
141+
},
142+
startResizeLeftSidebarSplit: () => {
143+
leftSplitAtDragStart.current = leftSplit;
144+
},
145+
startResizeRightSidebarSplit: () => {
146+
rightSplitAtDragStart.current = rightSplit;
147+
},
130148
resizeLeftSidebar,
131149
resizeRightSidebar,
132150
resizeLeftSidebarSplit,

0 commit comments

Comments
 (0)