Skip to content

Commit ce0f42d

Browse files
authored
refactor(node-resizer): add auto-scale prop & scale resize controls with zoom level (#1872)
* refactor(node-resizer): add auto-scale prop & scale resize controls with zoom Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com> * chore(changeset): add --------- Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
1 parent e7dec13 commit ce0f42d

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

.changeset/three-items-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/node-resizer": minor
3+
---
4+
5+
Add auto-scale prop to node resizer (default `true`) that forces node resizer controls to scale with the viewport zoom level.

packages/node-resizer/src/NodeResizer.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ResizeControlVariant } from './types'
88
99
const props = withDefaults(defineProps<NodeResizerProps>(), {
1010
isVisible: true,
11+
autoScale: true,
1112
})
1213
1314
const emits = defineEmits<NodeResizerEmits>()
@@ -92,13 +93,14 @@ export default {
9293
:node-id="nodeId"
9394
:position="c"
9495
:variant="ResizeControlVariant.Line"
95-
:keep-aspect-ratio="keepAspectRatio"
9696
:color="color"
9797
:min-width="minWidth"
9898
:min-height="minHeight"
9999
:max-width="maxWidth"
100100
:max-height="maxHeight"
101101
:should-resize="shouldResize"
102+
:keep-aspect-ratio="keepAspectRatio"
103+
:auto-scale="autoScale"
102104
@resize-start="emits('resizeStart', $event)"
103105
@resize="emits('resize', $event)"
104106
@resize-end="emits('resizeEnd', $event)"
@@ -118,6 +120,7 @@ export default {
118120
:max-height="maxHeight"
119121
:should-resize="shouldResize"
120122
:keep-aspect-ratio="keepAspectRatio"
123+
:auto-scale="autoScale"
121124
@resize-start="emits('resizeStart', $event)"
122125
@resize="emits('resize', $event)"
123126
@resize-end="emits('resizeEnd', $event)"

packages/node-resizer/src/ResizeControl.vue

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import type { NodeChange, NodeDimensionChange, NodePositionChange } from '@vue-f
33
import { clamp, useGetPointerPosition, useVueFlow } from '@vue-flow/core'
44
import { select } from 'd3-selection'
55
import { drag } from 'd3-drag'
6-
import { ref, toRef, watchEffect } from 'vue'
7-
import type { NodeResizerEmits, ResizeControlProps, ResizeControlVariant, ResizeDragEvent } from './types'
6+
import { computed, ref, toRef, watchEffect } from 'vue'
7+
import type { NodeResizerEmits, ResizeControlProps, ResizeDragEvent } from './types'
8+
import { ResizeControlVariant } from './types'
89
import { DefaultPositions, StylingProperty, getDirection } from './utils'
910
1011
const props = withDefaults(defineProps<ResizeControlProps>(), {
@@ -14,6 +15,7 @@ const props = withDefaults(defineProps<ResizeControlProps>(), {
1415
maxWidth: Number.MAX_VALUE,
1516
maxHeight: Number.MAX_VALUE,
1617
keepAspectRatio: false,
18+
autoScale: true,
1719
})
1820
1921
const emits = defineEmits<NodeResizerEmits>()
@@ -27,7 +29,7 @@ const initStartValues = {
2729
aspectRatio: 1,
2830
}
2931
30-
const { findNode, emits: triggerEmits } = useVueFlow()
32+
const { findNode, emits: triggerEmits, viewport, noDragClassName } = useVueFlow()
3133
3234
const getPointerPosition = useGetPointerPosition()
3335
@@ -39,7 +41,7 @@ let prevValues: typeof initPrevValues = initPrevValues
3941
4042
const controlPosition = toRef(() => props.position ?? DefaultPositions[props.variant])
4143
42-
const positionClassNames = toRef(() => controlPosition.value.split('-'))
44+
const positionClassNames = computed(() => controlPosition.value.split('-'))
4345
4446
const controlStyle = toRef(() => (props.color ? { [StylingProperty[props.variant]]: props.color } : {}))
4547
@@ -237,9 +239,12 @@ export default {
237239
<template>
238240
<div
239241
ref="resizeControlRef"
240-
class="vue-flow__resize-control nodrag"
241-
:class="[...positionClassNames, variant]"
242-
:style="controlStyle"
242+
class="vue-flow__resize-control"
243+
:class="[...positionClassNames, variant, noDragClassName]"
244+
:style="{
245+
...controlStyle,
246+
scale: variant === ResizeControlVariant.Handle ? `${Math.max(1 / viewport.zoom, 1)}` : undefined,
247+
}"
243248
>
244249
<slot />
245250
</div>

packages/node-resizer/src/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
/* handle styles */
2626
.vue-flow__resize-control.handle {
27-
width: 4px;
28-
height: 4px;
27+
width: 5px;
28+
height: 5px;
2929
border: 1px solid #fff;
3030
border-radius: 1px;
3131
background-color: #3367d9;

packages/node-resizer/src/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export interface NodeResizerProps {
4545
maxHeight?: number
4646
shouldResize?: ShouldResize
4747
keepAspectRatio?: boolean | number
48+
/**
49+
* Scale the controls with the zoom level.
50+
* @default true
51+
*/
52+
autoScale?: boolean
4853
}
4954

5055
export interface NodeResizerEmits {
@@ -62,8 +67,8 @@ export enum ResizeControlVariant {
6267
Handle = 'handle',
6368
}
6469

65-
export interface ResizeControlProps {
66-
nodeId?: string | null
70+
export interface ResizeControlProps extends NodeResizerProps {
71+
nodeId?: string
6772
color?: string
6873
minWidth?: number
6974
minHeight?: number
@@ -73,6 +78,11 @@ export interface ResizeControlProps {
7378
variant?: ResizeControlVariant
7479
shouldResize?: ShouldResize
7580
keepAspectRatio?: boolean | number
81+
/**
82+
* Scale the controls with the zoom level.
83+
* @default true
84+
*/
85+
autoScale?: boolean
7686
}
7787

7888
export interface ResizeControlLineProps extends ResizeControlProps {

0 commit comments

Comments
 (0)