Skip to content

Commit 5838454

Browse files
committed
refactor(core): resolve slots before returning default component (#1013)
1 parent 82e79a6 commit 5838454

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

.changeset/hot-nails-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": patch
3+
---
4+
5+
Deprecate template prop for nodes

.changeset/rude-pets-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": minor
3+
---
4+
5+
Resolve slot templates before falling back to default node/edge components

packages/core/src/container/EdgeRenderer/EdgeRenderer.vue

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const {
2222
emits,
2323
} = useVueFlow()
2424
25+
const instance = getCurrentInstance()
26+
2527
function selectable(edgeSelectable?: boolean) {
2628
return typeof edgeSelectable === 'undefined' ? elementsSelectable.value : edgeSelectable
2729
}
@@ -34,8 +36,13 @@ function focusable(edgeFocusable?: boolean) {
3436
3537
function getType(type?: string, template?: GraphEdge['template']) {
3638
const name = type || 'default'
39+
40+
const slot = slots?.[`edge-${name}`]
41+
if (slot) {
42+
return slot
43+
}
44+
3745
let edgeType = template ?? getEdgeTypes.value[name]
38-
const instance = getCurrentInstance()
3946
4047
if (typeof edgeType === 'string') {
4148
if (instance) {
@@ -45,17 +52,14 @@ function getType(type?: string, template?: GraphEdge['template']) {
4552
}
4653
}
4754
}
55+
4856
if (edgeType && typeof edgeType !== 'string') {
4957
return edgeType
5058
}
5159
52-
const slot = slots?.[`edge-${name}`]
53-
if (!slot) {
54-
emits.error(new VueFlowError(ErrorCode.EDGE_TYPE_MISSING, edgeType))
55-
return false
56-
}
60+
emits.error(new VueFlowError(ErrorCode.EDGE_TYPE_MISSING, edgeType))
5761
58-
return slot
62+
return false
5963
}
6064
</script>
6165

packages/core/src/container/NodeRenderer/NodeRenderer.vue

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts" setup>
2-
import { getCurrentInstance, inject, nextTick, onBeforeUnmount, onMounted, resolveComponent } from 'vue'
2+
import { getCurrentInstance, inject, nextTick, onBeforeUnmount, onMounted, ref, resolveComponent } from 'vue'
33
import { until } from '@vueuse/core'
44
import { NodeWrapper } from '../../components'
55
import type { GraphNode, HandleConnectable, NodeComponent } from '../../types'
@@ -19,20 +19,22 @@ const {
1919
getNodeTypes,
2020
updateNodeDimensions,
2121
emits,
22-
} = $(useVueFlow())
22+
} = useVueFlow()
2323
24-
let resizeObserver = $ref<ResizeObserver>()
24+
const resizeObserver = ref<ResizeObserver>()
2525
26-
until(() => getNodes.length > 0 && getNodesInitialized.length === getNodes.length)
26+
const instance = getCurrentInstance()
27+
28+
until(() => getNodes.value.length > 0 && getNodesInitialized.value.length === getNodes.value.length)
2729
.toBe(true)
2830
.then(() => {
2931
nextTick(() => {
30-
emits.nodesInitialized(getNodesInitialized)
32+
emits.nodesInitialized(getNodesInitialized.value)
3133
})
3234
})
3335
3436
onMounted(() => {
35-
resizeObserver = new ResizeObserver((entries) => {
37+
resizeObserver.value = new ResizeObserver((entries) => {
3638
const updates = entries.map((entry) => {
3739
const id = entry.target.getAttribute('data-id') as string
3840
@@ -47,25 +49,30 @@ onMounted(() => {
4749
})
4850
})
4951
50-
onBeforeUnmount(() => resizeObserver?.disconnect())
52+
onBeforeUnmount(() => resizeObserver.value?.disconnect())
5153
5254
function draggable(nodeDraggable?: boolean) {
53-
return typeof nodeDraggable === 'undefined' ? nodesDraggable : nodeDraggable
55+
return typeof nodeDraggable === 'undefined' ? nodesDraggable.value : nodeDraggable
5456
}
5557
function selectable(nodeSelectable?: boolean) {
56-
return typeof nodeSelectable === 'undefined' ? elementsSelectable : nodeSelectable
58+
return typeof nodeSelectable === 'undefined' ? elementsSelectable.value : nodeSelectable
5759
}
5860
function connectable(nodeConnectable?: HandleConnectable) {
59-
return typeof nodeConnectable === 'undefined' ? nodesConnectable : nodeConnectable
61+
return typeof nodeConnectable === 'undefined' ? nodesConnectable.value : nodeConnectable
6062
}
6163
function focusable(nodeFocusable?: boolean) {
62-
return typeof nodeFocusable === 'undefined' ? nodesFocusable : nodeFocusable
64+
return typeof nodeFocusable === 'undefined' ? nodesFocusable.value : nodeFocusable
6365
}
6466
6567
function getType(type?: string, template?: GraphNode['template']) {
6668
const name = type || 'default'
67-
let nodeType = template ?? getNodeTypes[name]
68-
const instance = getCurrentInstance()
69+
70+
const slot = slots?.[`node-${name}`]
71+
if (slot) {
72+
return slot
73+
}
74+
75+
let nodeType = template ?? getNodeTypes.value[name]
6976
7077
if (typeof nodeType === 'string') {
7178
if (instance) {
@@ -75,17 +82,14 @@ function getType(type?: string, template?: GraphNode['template']) {
7582
}
7683
}
7784
}
78-
if (typeof nodeType !== 'string') {
85+
86+
if (nodeType && typeof nodeType !== 'string') {
7987
return nodeType
8088
}
8189
82-
const slot = slots?.[`node-${name}`]
83-
if (!slot) {
84-
emits.error(new VueFlowError(ErrorCode.NODE_TYPE_MISSING, nodeType))
85-
return false
86-
}
90+
emits.error(new VueFlowError(ErrorCode.NODE_TYPE_MISSING, nodeType))
8791
88-
return slot
92+
return false
8993
}
9094
</script>
9195

packages/core/src/types/node.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ export interface Node<Data = ElementData, CustomEvents extends Record<string, Cu
8585
style?: Styles | StyleFunc<GraphNode<Data, CustomEvents>>
8686
/** Is node hidden */
8787
hidden?: boolean
88-
/** overwrites current node type */
88+
/**
89+
* @deprecated will be removed in the next major release
90+
* overwrites current node type
91+
*/
8992
template?: NodeComponent
9093
/** Additional data that is passed to your custom components */
9194
data?: Data

0 commit comments

Comments
 (0)