()
.on('start', (event: ResizeDragEvent) => {
const node = store.getState().nodeInternals.get(nodeId);
- const pointerPos = getPointerPosition(event);
+ const { xSnapped, ySnapped } = getPointerPosition(event);
- initialDimensionsRef.current = {
+ startValues.current = {
width: node?.width ?? 0,
height: node?.height ?? 0,
nodeX: node?.position.x ?? 0,
nodeY: node?.position.y ?? 0,
- x: pointerPos.xSnapped,
- y: pointerPos.ySnapped,
+ x: xSnapped,
+ y: ySnapped,
};
- nodeElementRef.current = document.querySelector(`.react-flow__node[data-id="${nodeId}"]`) as HTMLDivElement;
})
.on('drag', (event: ResizeDragEvent) => {
const { updateNodePositions, nodeInternals, onNodesChange, hasDefaultNodes, nodeOrigin } = store.getState();
- const pointerPos = getPointerPosition(event);
- const nodeEl = nodeElementRef.current;
+ const { xSnapped, ySnapped } = getPointerPosition(event);
const node = nodeInternals.get(nodeId);
const enableX = position.includes('right') || position.includes('left');
const enableY = position.includes('bottom') || position.includes('top');
const invertX = position.includes('left');
const invertY = position.includes('top');
- if (nodeEl && node) {
+ if (node) {
const changes: NodeChange[] = [];
- const distX = enableX ? pointerPos.xSnapped - initialDimensionsRef.current.x : 0;
- const distY = enableY ? pointerPos.ySnapped - initialDimensionsRef.current.y : 0;
- const width = initialDimensionsRef.current.width + (invertX ? -distX : distX);
- const height = initialDimensionsRef.current.height + (invertY ? -distY : distY);
+ const {
+ x: startX,
+ y: startY,
+ width: startWidth,
+ height: startHeight,
+ nodeX: startNodeX,
+ nodeY: startNodeY,
+ } = startValues.current;
+ const distX = enableX ? xSnapped - startX : 0;
+ const distY = enableY ? ySnapped - startY : 0;
+ const width = startWidth + (invertX ? -distX : distX);
+ const height = startHeight + (invertY ? -distY : distY);
if (invertX || invertY) {
- const x = invertX ? initialDimensionsRef.current.nodeX + distX : initialDimensionsRef.current.nodeX;
- const y = invertY ? initialDimensionsRef.current.nodeY + distY : initialDimensionsRef.current.nodeY;
+ const x = invertX ? startNodeX + distX : startNodeX;
+ const y = invertY ? startNodeY + distY : startNodeY;
if (x !== node.position.x || y !== node.position.y) {
- const positionChanges: NodePositionChange[] | null = updateNodePositions(
+ const positionChanges = updateNodePositions(
[
{
id: nodeId,
@@ -129,12 +133,12 @@ function ResizeControl({
};
}, [nodeId, position, getPointerPosition]);
- const positionClassNames = position.split('-');
+ const positionClassNames = position?.split('-');
return (
{children}
@@ -143,7 +147,7 @@ function ResizeControl({
}
export function ResizeControlLine(props: ResizeControlLineProps) {
- return ;
+ return ;
}
-export default ResizeControl;
+export default memo(ResizeControl);
diff --git a/packages/node-resizer/src/types.ts b/packages/node-resizer/src/types.ts
index 7eb6b32a..9368e434 100644
--- a/packages/node-resizer/src/types.ts
+++ b/packages/node-resizer/src/types.ts
@@ -13,10 +13,15 @@ export type ControlLinePosition = 'top' | 'bottom' | 'left' | 'right';
export type ControlPosition = ControlLinePosition | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
+export enum ResizeControlVariant {
+ Line = 'line',
+ Handle = 'handle',
+}
+
export type ResizeControlProps = {
nodeId: string;
position: ControlPosition;
- variant?: 'line' | 'handle';
+ variant?: ResizeControlVariant;
className?: string;
style?: CSSProperties;
children?: ReactNode;