Skip to content

Commit f0dfa02

Browse files
committed
Added ability to pass custom resize handler
1 parent e3c4412 commit f0dfa02

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed

demo/src/ui-demo/Windows.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const WindowInner: React.FC<{ onClick?: () => void }> = (props) => {
6262
className="title-bar"
6363
onMouseDown={(e) => {
6464
props.onClick && props.onClick();
65-
parentSpace.startDrag(e);
65+
parentSpace.startMouseDrag(e);
6666
}}
6767
size={40}>
6868
{Description(`Window title`)}

react-spaces/src/Globals/Dragging.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,13 @@ const onMove = (parentContext: ISpaceContext | undefined, space: ISpace, origina
1616
}
1717
};
1818

19-
export const startDrag = (
19+
export const startMouseDrag = (
2020
e: React.MouseEvent<HTMLElement, MouseEvent>,
2121
parentContext: ISpaceContext | undefined,
2222
space: ISpace,
2323
element: HTMLElement | undefined,
2424
) => {
2525
if (parentContext && element) {
26-
// if (props.onResizeStart) {
27-
// const result = props.onResizeStart();
28-
// if (typeof result === "boolean" && !result) {
29-
// return;
30-
// }
31-
// }
32-
33-
//parentContext.updateResizing(true);
34-
3526
const originalMouseX = e.pageX - space.adjustedLeft;
3627
const originalMouseY = e.pageY - space.adjustedTop;
3728
let lastX = 0;
@@ -54,9 +45,6 @@ export const startDrag = (
5445
}
5546
window.removeEventListener("mousemove", withPreventDefault);
5647
window.removeEventListener("mouseup", removeListener);
57-
58-
//parentContext.updateResizing(false);
59-
//onResizeEnd();
6048
};
6149
window.addEventListener("mousemove", withPreventDefault);
6250
window.addEventListener("mouseup", removeListener);

react-spaces/src/Globals/ISpaceContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface ISpaceContext {
66
children: ISpace[];
77
updateChildren: (children: ISpace[]) => void;
88
updateResizing: (state: boolean) => void;
9-
startDrag: (e: React.MouseEvent<HTMLElement, MouseEvent>) => void;
9+
startMouseDrag: (e: React.MouseEvent<HTMLElement, MouseEvent>) => void;
1010
}
1111

1212
const recalcSpaces = (spaces: ISpace[]) => {
@@ -117,7 +117,7 @@ export const createSpaceContext = (
117117
children: children,
118118
updateChildren: updateChildren,
119119
updateResizing: updateResizing,
120-
startDrag: startDrag,
120+
startMouseDrag: startDrag,
121121
};
122122

123123
return context;

react-spaces/src/Globals/Resizing.ts

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,33 @@ enum EndEvent {
1616
Touch = "touchend",
1717
}
1818

19+
interface IResizeChange {
20+
x: number;
21+
y: number;
22+
}
23+
1924
export function startTouchResize(
2025
e: React.TouchEvent<HTMLElement>,
2126
parentContext: ISpaceContext | undefined,
2227
space: ISpace,
2328
props: AllProps,
2429
element: HTMLElement | undefined,
30+
customResizeHandler?: (resizeDelta: IResizeChange) => void,
2531
) {
26-
return startResize(e, parentContext, space, props, element, EndEvent.Touch, MoveEvent.Touch, (e) => ({
27-
x: e.touches[0].pageX,
28-
y: e.touches[0].pageY,
29-
}));
32+
return startResize(
33+
e,
34+
parentContext,
35+
space,
36+
props,
37+
element,
38+
EndEvent.Touch,
39+
MoveEvent.Touch,
40+
(e) => ({
41+
x: e.touches[0].pageX,
42+
y: e.touches[0].pageY,
43+
}),
44+
customResizeHandler,
45+
);
3046
}
3147

3248
export function startMouseResize(
@@ -35,11 +51,22 @@ export function startMouseResize(
3551
space: ISpace,
3652
props: AllProps,
3753
element: HTMLElement | undefined,
54+
customResizeHandler?: (resizeDelta: IResizeChange) => void,
3855
) {
39-
return startResize(e, parentContext, space, props, element, EndEvent.Mouse, MoveEvent.Mouse, (e) => ({
40-
x: e.pageX,
41-
y: e.pageY,
42-
}));
56+
return startResize(
57+
e,
58+
parentContext,
59+
space,
60+
props,
61+
element,
62+
EndEvent.Mouse,
63+
MoveEvent.Mouse,
64+
(e) => ({
65+
x: e.pageX,
66+
y: e.pageY,
67+
}),
68+
customResizeHandler,
69+
);
4370
}
4471

4572
function onResizeEnd(props: AllProps, resizeType: ResizeType, element: HTMLElement) {
@@ -59,6 +86,7 @@ function onResize(
5986
y: number,
6087
minimumAdjust: number,
6188
maximumAdjust: number | undefined,
89+
customResizeHandler?: (resizeDelta: IResizeChange) => void,
6290
) {
6391
const adjustmentX = Math.min(
6492
Math.max(resizeType === ResizeType.Left ? originalX - x : x - originalX, minimumAdjust),
@@ -69,10 +97,14 @@ function onResize(
6997
maximumAdjust === undefined ? 999999 : maximumAdjust,
7098
);
7199

72-
const adjustment = isHorizontalSpace(props.anchor) ? adjustmentX : adjustmentY;
100+
if (customResizeHandler) {
101+
customResizeHandler({ x: adjustmentX, y: adjustmentY });
102+
} else {
103+
const adjustment = isHorizontalSpace(props.anchor) ? adjustmentX : adjustmentY;
73104

74-
if (adjustment !== space.adjustedSize) {
75-
updateSpace(parentContext, space.id, { adjustedSize: adjustment });
105+
if (adjustment !== space.adjustedSize) {
106+
updateSpace(parentContext, space.id, { adjustedSize: adjustment });
107+
}
76108
}
77109
}
78110

@@ -85,6 +117,7 @@ function startResize<T extends SyntheticEvent<HTMLElement> | MouseEvent | TouchE
85117
endEvent: EndEvent,
86118
moveEvent: MoveEvent,
87119
getCoords: (event: T) => { x: number; y: number },
120+
customResizeHandler?: (resizeDelta: IResizeChange) => void,
88121
) {
89122
if (element && props.resizable && props.anchor && parentContext) {
90123
const resizeType: ResizeType | undefined = AnchorToResizeTypeMap[props.anchor];
@@ -112,7 +145,19 @@ function startResize<T extends SyntheticEvent<HTMLElement> | MouseEvent | TouchE
112145
let moved = false;
113146

114147
const resize = (x: number, y: number) =>
115-
onResize(props, parentContext, space, resizeType, originalMouseX, originalMouseY, x, y, minimumAdjust, maximumAdjust);
148+
onResize(
149+
props,
150+
parentContext,
151+
space,
152+
resizeType,
153+
originalMouseX,
154+
originalMouseY,
155+
x,
156+
y,
157+
minimumAdjust,
158+
maximumAdjust,
159+
customResizeHandler,
160+
);
116161

117162
const withPreventDefault = (e: T) => {
118163
moved = true;

react-spaces/src/Hooks/useParentSpace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export const useParentSpace = () => {
55
const parentSpace = React.useContext(SpaceContext);
66

77
return {
8-
startDrag: !parentSpace ? (e: React.MouseEvent<HTMLElement, MouseEvent>) => null : parentSpace.startDrag,
8+
startMouseDrag: !parentSpace ? (e: React.MouseEvent<HTMLElement, MouseEvent>) => null : parentSpace.startMouseDrag,
99
};
1010
};

react-spaces/src/Hooks/useSpace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { initialState, isHorizontalSpace, isVerticalSpace, coalesce } from "src/
44
import { ISpaceContext, updateSpace, removeSpace, registerSpace, createSpaceContext } from "src/Globals/ISpaceContext";
55
import { SpaceLayerContext, SpaceContext } from "src/Contexts";
66
import { ResizeSensor } from "css-element-queries";
7-
import { startDrag } from "src/Globals/Dragging";
7+
import { startMouseDrag } from "src/Globals/Dragging";
88

99
const calcProp = (props: AllProps, positionedFn: (p: AllProps) => SizeUnit, elseFn: (p: AllProps) => SizeUnit) =>
1010
props.isPositioned ? positionedFn(props) : elseFn(props);
@@ -156,7 +156,7 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
156156
state.children,
157157
(children) => setState({ children: children }),
158158
(resizing) => setState({ resizing: resizing }),
159-
(e) => startDrag(e, parentContext, space, divElementRef.current),
159+
(e) => startMouseDrag(e, parentContext, space, divElementRef.current),
160160
parentContext,
161161
);
162162

0 commit comments

Comments
 (0)