feat(xy-resizer): add resize direction #2986

This commit is contained in:
moklick
2025-04-17 12:54:47 +02:00
parent af1c85281e
commit bee2f4aa05
6 changed files with 59 additions and 3 deletions
@@ -0,0 +1,21 @@
import { memo, FC } from 'react';
import { Handle, Position, NodeProps, NodeResizeControl, ResizeControlVariant } from '@xyflow/react';
const CustomResizerNode: FC<NodeProps> = ({ data }) => {
return (
<>
<NodeResizeControl
variant={ResizeControlVariant.Handle}
position="bottom-right"
resizeDirection="horizontal"
minWidth={100}
maxWidth={500}
/>
<Handle type="target" position={Position.Left} />
<div>{data.label}</div>
<Handle type="source" position={Position.Right} />
</>
);
};
export default memo(CustomResizerNode);
@@ -15,12 +15,14 @@ import DefaultResizer from './DefaultResizer';
import CustomResizer from './CustomResizer'; import CustomResizer from './CustomResizer';
import VerticalResizer from './VerticalResizer'; import VerticalResizer from './VerticalResizer';
import HorizontalResizer from './HorizontalResizer'; import HorizontalResizer from './HorizontalResizer';
import BottomRightResizer from './BottomRightResizer';
const nodeTypes = { const nodeTypes = {
defaultResizer: DefaultResizer, defaultResizer: DefaultResizer,
customResizer: CustomResizer, customResizer: CustomResizer,
verticalResizer: VerticalResizer, verticalResizer: VerticalResizer,
horizontalResizer: HorizontalResizer, horizontalResizer: HorizontalResizer,
bottomRightResizer: BottomRightResizer,
}; };
const nodeStyle = { const nodeStyle = {
@@ -166,6 +168,13 @@ const initialNodes: Node[] = [
expandParent: true, expandParent: true,
style: { ...nodeStyle }, style: { ...nodeStyle },
}, },
{
id: '6',
type: 'bottomRightResizer',
data: { label: 'Bottom Right with horizontal direction' },
position: { x: 500, y: 500 },
style: { ...nodeStyle },
},
]; ];
const CustomNodeFlow = () => { const CustomNodeFlow = () => {
@@ -32,6 +32,7 @@ function ResizeControl({
maxWidth = Number.MAX_VALUE, maxWidth = Number.MAX_VALUE,
maxHeight = Number.MAX_VALUE, maxHeight = Number.MAX_VALUE,
keepAspectRatio = false, keepAspectRatio = false,
resizeDirection,
shouldResize, shouldResize,
onResizeStart, onResizeStart,
onResize, onResize,
@@ -166,6 +167,7 @@ function ResizeControl({
maxHeight, maxHeight,
}, },
keepAspectRatio, keepAspectRatio,
resizeDirection,
onResizeStart, onResizeStart,
onResize, onResize,
onResizeEnd, onResizeEnd,
@@ -3,6 +3,7 @@ import type {
ControlPosition, ControlPosition,
ControlLinePosition, ControlLinePosition,
ResizeControlVariant, ResizeControlVariant,
ResizeControlDirection,
ShouldResize, ShouldResize,
OnResizeStart, OnResizeStart,
OnResize, OnResize,
@@ -97,6 +98,11 @@ export type ResizeControlProps = Pick<
* @example ResizeControlVariant.Handle, ResizeControlVariant.Line * @example ResizeControlVariant.Handle, ResizeControlVariant.Line
*/ */
variant?: ResizeControlVariant; variant?: ResizeControlVariant;
/**
* The direction the user can resize the node.
* If not provided, the user can resize in any direction.
*/
resizeDirection?: ResizeControlDirection;
className?: string; className?: string;
style?: CSSProperties; style?: CSSProperties;
children?: ReactNode; children?: ReactNode;
+15 -3
View File
@@ -12,7 +12,15 @@ import type {
Transform, Transform,
XYPosition, XYPosition,
} from '../types'; } from '../types';
import type { OnResize, OnResizeEnd, OnResizeStart, ResizeDragEvent, ShouldResize, ControlPosition } from './types'; import type {
OnResize,
OnResizeEnd,
OnResizeStart,
ResizeDragEvent,
ShouldResize,
ControlPosition,
ResizeControlDirection,
} from './types';
const initPrevValues = { width: 0, height: 0, x: 0, y: 0 }; const initPrevValues = { width: 0, height: 0, x: 0, y: 0 };
@@ -60,6 +68,7 @@ type XYResizerUpdateParams = {
maxHeight: number; maxHeight: number;
}; };
keepAspectRatio: boolean; keepAspectRatio: boolean;
resizeDirection?: ResizeControlDirection;
onResizeStart: OnResizeStart | undefined; onResizeStart: OnResizeStart | undefined;
onResize: OnResize | undefined; onResize: OnResize | undefined;
onResizeEnd: OnResizeEnd | undefined; onResizeEnd: OnResizeEnd | undefined;
@@ -99,6 +108,7 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: X
controlPosition, controlPosition,
boundaries, boundaries,
keepAspectRatio, keepAspectRatio,
resizeDirection,
onResizeStart, onResizeStart,
onResize, onResize,
onResizeEnd, onResizeEnd,
@@ -251,8 +261,10 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: X
} }
if (isWidthChange || isHeightChange) { if (isWidthChange || isHeightChange) {
change.width = isWidthChange ? width : prevValues.width; change.width =
change.height = isHeightChange ? height : prevValues.height; isWidthChange && (!resizeDirection || resizeDirection === 'horizontal') ? width : prevValues.width;
change.height =
isHeightChange && (!resizeDirection || resizeDirection === 'vertical') ? height : prevValues.height;
prevValues.width = change.width; prevValues.width = change.width;
prevValues.height = change.height; prevValues.height = change.height;
} }
+6
View File
@@ -35,6 +35,12 @@ export enum ResizeControlVariant {
Handle = 'handle', Handle = 'handle',
} }
/**
* The direction the user can resize the node.
* @public
*/
export type ResizeControlDirection = 'horizontal' | 'vertical';
export const XY_RESIZER_HANDLE_POSITIONS: ControlPosition[] = ['top-left', 'top-right', 'bottom-left', 'bottom-right']; export const XY_RESIZER_HANDLE_POSITIONS: ControlPosition[] = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
export const XY_RESIZER_LINE_POSITIONS: ControlLinePosition[] = ['top', 'right', 'bottom', 'left']; export const XY_RESIZER_LINE_POSITIONS: ControlLinePosition[] = ['top', 'right', 'bottom', 'left'];