@@ -6,62 +6,120 @@ title: Composables
6
6
7
7
## [ useVueFlow] ( /typedocs/functions/useVueFlow )
8
8
9
- If you're using the options API of Vue you will soon notice that your access to the state of Vue Flow is limited.
10
-
11
- This is where the composition API comes in.
12
-
13
- The composition API and the power of provide/inject allows us to act more flexible with the way we provide states inside
14
- a component tree.
15
- Thus accessing the internal state of Vue Flow becomes super easy when using composition.
9
+ The ` useVueFlow ` composable provides you with a set of methods to interact with the graph.
16
10
17
11
``` vue
18
12
<script setup>
13
+ import { ref } from 'vue'
19
14
import { useVueFlow, VueFlow } from '@vue-flow/core'
20
15
21
- const { nodes, edges } = useVueFlow({
22
- nodes: [
23
- {
24
- id: '1',
25
- label: 'Node 1',
26
- position: { x: 0, y: 0 },
27
- }
28
- ]
29
- })
16
+ const { onInit, findNode, fitView, snapToGrid } = useVueFlow()
17
+
18
+ const nodes = ref([/* ... */])
19
+
20
+ const edges = ref([/* ... */])
30
21
31
- onMounted(() => {
32
- console.log(nodes.value) // will log a single node
33
- console.log(edges.value) // will log an empty array
22
+ // to enable snapping to grid
23
+ snapToGrid.value = true
24
+
25
+ // any event that is emitted from the `<VueFlow />` component can be listened to using the `onEventName` method
26
+ onInit((instance) => {
27
+ // `instance` is the same type as the return of `useVueFlow` (VueFlowStore)
28
+
29
+ fitView()
30
+
31
+ const node = findNode('1')
32
+
33
+ if (node) {
34
+ node.position = { x: 100, y: 100 }
35
+ }
34
36
})
35
37
</script>
36
38
<template>
37
- <VueFlow />
39
+ <VueFlow :nodes="nodes" :edges="edges" />
38
40
</template>
39
41
```
40
42
41
- ` useVueFlow ` exposes basically the whole internal state.
42
- The values are reactive, meaning changing the state values returned from ` useVueFlow ` will trigger changes in the graph.
43
+ ` useVueFlow ` exposes the whole internal state, including the nodes and edges .
44
+ The values are reactive, meaning changing the values returned from ` useVueFlow ` will trigger changes in the graph.
43
45
44
- ## [ useZoomPanHelper ] ( /typedocs/functions/useZoomPanHelper )
46
+ ## [ useHandleConnections ] ( /typedocs/functions/useHandleConnections )
45
47
46
- ::: warning [ deprecated]
47
- All functions of ` useZoomPanHelper ` are also available in ` useVueFlow ` .
48
- ` useZoomPanHelper ` might be removed in a future version.
49
- :::
48
+ ` useHandleConnections ` provides you with an array of connections that are connected to the node you pass to it.
50
49
51
- The ` useZoomPanHelper ` utility can be used to access core store functions like getting Elements or
52
- using viewpane transforms.
53
- All functions can also be accessed from ` useVueFlow ` .
54
- It requires a valid Vue Flow store in its context.
50
+ ``` ts
51
+ import { useHandleConnections } from ' @vue-flow/core'
55
52
56
- ``` vue
57
- <script setup>
58
- import { useZoomPanHelper } from '@vue-flow/core'
53
+ // get all connections where this node is the target (incoming connections)
54
+ const targetConnections = useHandleConnections ({
55
+ type: ' target' ,
56
+ })
59
57
60
- const { fitView } = useZoomPanHelper()
61
- </script>
62
- <template>
63
- <button @click="fitView({ padding: 0.2, includeHiddenNodes: true })"></button>
64
- </template>
58
+ // get all connections where this node is the source (outgoing connections)
59
+ const sourceConnections = useHandleConnections ({
60
+ type: ' source' ,
61
+ })
62
+
63
+ const connections = useHandleConnections ({
64
+ id: ' handle-1' , // you can explicitly pass a handle id if there are multiple handles of the same type
65
+ nodeId: ' 1' , // you can explicitly pass a node id, otherwise it's used from the `NodeId injection
66
+ type: ' target' ,
67
+ onConnect : (connections : Connection []) => {
68
+ // do something with the connections
69
+ },
70
+ onDisconnect : (connections : Connection []) => {
71
+ // do something with the connections
72
+ },
73
+ })
74
+ ```
75
+
76
+ ## [ useNodesData] ( /typedocs/functions/useNode )
77
+
78
+ ` useNodesData ` provides you with an array of data objects depending on the node ids you pass to it.
79
+ It's especially useful when used together with ` useHandleConnections ` .
80
+
81
+ ``` ts
82
+ import { useNodesData , useHandleConnections } from ' @vue-flow/core'
83
+
84
+ // get all connections where this node is the target (incoming connections)
85
+ const connections = useHandleConnections ({
86
+ type: ' target' ,
87
+ })
88
+
89
+ const data = useNodesData (() => connections .value .map ((connection ) => connection .source ))
90
+
91
+ console .log (data .value ) // [{ /* ... */]
92
+ ```
93
+
94
+ To further narrow down the type of the returned data, you can pass a guard function as the 2nd argument.
95
+
96
+ ``` ts
97
+ import { useNodesData , useHandleConnections , type Node } from ' @vue-flow/core'
98
+
99
+ type MyNode = Node <{ foo: string }>
100
+
101
+ const connections = useHandleConnections ({
102
+ type: ' target' ,
103
+ })
104
+
105
+ const data = useNodesData (() => connections .value .map ((connection ) => connection .source ), (node ): node is MyNode => node .type === ' foo' )
106
+
107
+ console .log (data .value ) // [{ /* foo: string */]
108
+ ```
109
+
110
+ ## [ useNodeId] ( /typedocs/functions/useNodeId )
111
+
112
+ ` useNodeId ` provides you with the current node id.
113
+
114
+ This composable should be called * inside a custom node component* ,
115
+ as the id for the node is provided by the internal ` <NodeWrapper /> ` component.
116
+
117
+ ``` ts
118
+ import { useNodeId } from ' @vue-flow/core'
119
+
120
+ const nodeId = useNodeId ()
121
+
122
+ console .log (nodeId .value ) // '1'
65
123
```
66
124
67
125
## [ useHandle] ( /typedocs/functions/useHandle )
0 commit comments