Skip to content

refactor(node-resizer): add auto-scale prop & scale resize controls with zoom level #1872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-items-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/node-resizer": minor
---

Add auto-scale prop to node resizer (default `true`) that forces node resizer controls to scale with the viewport zoom level.
5 changes: 4 additions & 1 deletion packages/node-resizer/src/NodeResizer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ResizeControlVariant } from './types'

const props = withDefaults(defineProps<NodeResizerProps>(), {
isVisible: true,
autoScale: true,
})

const emits = defineEmits<NodeResizerEmits>()
Expand Down Expand Up @@ -92,13 +93,14 @@ export default {
:node-id="nodeId"
:position="c"
:variant="ResizeControlVariant.Line"
:keep-aspect-ratio="keepAspectRatio"
:color="color"
:min-width="minWidth"
:min-height="minHeight"
:max-width="maxWidth"
:max-height="maxHeight"
:should-resize="shouldResize"
:keep-aspect-ratio="keepAspectRatio"
:auto-scale="autoScale"
@resize-start="emits('resizeStart', $event)"
@resize="emits('resize', $event)"
@resize-end="emits('resizeEnd', $event)"
Expand All @@ -118,6 +120,7 @@ export default {
:max-height="maxHeight"
:should-resize="shouldResize"
:keep-aspect-ratio="keepAspectRatio"
:auto-scale="autoScale"
@resize-start="emits('resizeStart', $event)"
@resize="emits('resize', $event)"
@resize-end="emits('resizeEnd', $event)"
Expand Down
19 changes: 12 additions & 7 deletions packages/node-resizer/src/ResizeControl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type { NodeChange, NodeDimensionChange, NodePositionChange } from '@vue-f
import { clamp, useGetPointerPosition, useVueFlow } from '@vue-flow/core'
import { select } from 'd3-selection'
import { drag } from 'd3-drag'
import { ref, toRef, watchEffect } from 'vue'
import type { NodeResizerEmits, ResizeControlProps, ResizeControlVariant, ResizeDragEvent } from './types'
import { computed, ref, toRef, watchEffect } from 'vue'
import type { NodeResizerEmits, ResizeControlProps, ResizeDragEvent } from './types'
import { ResizeControlVariant } from './types'
import { DefaultPositions, StylingProperty, getDirection } from './utils'

const props = withDefaults(defineProps<ResizeControlProps>(), {
Expand All @@ -14,6 +15,7 @@ const props = withDefaults(defineProps<ResizeControlProps>(), {
maxWidth: Number.MAX_VALUE,
maxHeight: Number.MAX_VALUE,
keepAspectRatio: false,
autoScale: true,
})

const emits = defineEmits<NodeResizerEmits>()
Expand All @@ -27,7 +29,7 @@ const initStartValues = {
aspectRatio: 1,
}

const { findNode, emits: triggerEmits } = useVueFlow()
const { findNode, emits: triggerEmits, viewport, noDragClassName } = useVueFlow()

const getPointerPosition = useGetPointerPosition()

Expand All @@ -39,7 +41,7 @@ let prevValues: typeof initPrevValues = initPrevValues

const controlPosition = toRef(() => props.position ?? DefaultPositions[props.variant])

const positionClassNames = toRef(() => controlPosition.value.split('-'))
const positionClassNames = computed(() => controlPosition.value.split('-'))

const controlStyle = toRef(() => (props.color ? { [StylingProperty[props.variant]]: props.color } : {}))

Expand Down Expand Up @@ -237,9 +239,12 @@ export default {
<template>
<div
ref="resizeControlRef"
class="vue-flow__resize-control nodrag"
:class="[...positionClassNames, variant]"
:style="controlStyle"
class="vue-flow__resize-control"
:class="[...positionClassNames, variant, noDragClassName]"
:style="{
...controlStyle,
scale: variant === ResizeControlVariant.Handle ? `${Math.max(1 / viewport.zoom, 1)}` : undefined,
}"
>
<slot />
</div>
Expand Down
4 changes: 2 additions & 2 deletions packages/node-resizer/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

/* handle styles */
.vue-flow__resize-control.handle {
width: 4px;
height: 4px;
width: 5px;
height: 5px;
border: 1px solid #fff;
border-radius: 1px;
background-color: #3367d9;
Expand Down
14 changes: 12 additions & 2 deletions packages/node-resizer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export interface NodeResizerProps {
maxHeight?: number
shouldResize?: ShouldResize
keepAspectRatio?: boolean | number
/**
* Scale the controls with the zoom level.
* @default true
*/
autoScale?: boolean
}

export interface NodeResizerEmits {
Expand All @@ -62,8 +67,8 @@ export enum ResizeControlVariant {
Handle = 'handle',
}

export interface ResizeControlProps {
nodeId?: string | null
export interface ResizeControlProps extends NodeResizerProps {
nodeId?: string
color?: string
minWidth?: number
minHeight?: number
Expand All @@ -73,6 +78,11 @@ export interface ResizeControlProps {
variant?: ResizeControlVariant
shouldResize?: ShouldResize
keepAspectRatio?: boolean | number
/**
* Scale the controls with the zoom level.
* @default true
*/
autoScale?: boolean
}

export interface ResizeControlLineProps extends ResizeControlProps {
Expand Down
Loading