|
| 1 | +--- |
| 2 | +title: Handles |
| 3 | +--- |
| 4 | + |
| 5 | +# Introduction to Handles |
| 6 | + |
| 7 | +Handles are the small circles that are usually placed on the borders of a node. They are used to connect nodes |
| 8 | +together by dragging a connection-line from one handle to another, resulting in a connection ([Edge](/guide/edge)) |
| 9 | +between the nodes. |
| 10 | + |
| 11 | +Handles are a crucial part of the VueFlow library, as they are the main interaction point for the user to create |
| 12 | +connections between nodes. |
| 13 | + |
| 14 | +Without handles, it is basically impossible to create edges between nodes, as the `<Handle>` components are used |
| 15 | +to calculate the points for the edges. |
| 16 | + |
| 17 | +## Handle Component |
| 18 | + |
| 19 | +The `<Handle>` component is a simple component that is used to create a handle for a node. It is a wrapper around a |
| 20 | +`<div>` element that provides the necessary event handlers to create edges between nodes. |
| 21 | + |
| 22 | +The `<Handle>` component is used in the `<Node>` component to create handles for the node. |
| 23 | + |
| 24 | +## Multiple Handles |
| 25 | + |
| 26 | +A node can have multiple handles, the number of handles is not limited and you can use as many handles as you need. |
| 27 | +When using multiple handles of the same type (`source` or `target`), each handle needs to have a unique id. |
| 28 | + |
| 29 | +```vue |
| 30 | +<!-- each of these handles needs a unique id since we're using two `source` type handles --> |
| 31 | +<Handle id="a" type="source" :position="Position.Right" /> |
| 32 | +<Handle id="b" type="source" :position="Position.Right" /> |
| 33 | +``` |
| 34 | + |
| 35 | +The `id` prop is used to identify the handle when creating edges between nodes. If no `id` is provided, the first handle |
| 36 | +of the necessary type will be used. |
| 37 | + |
| 38 | +```ts |
| 39 | +const { onConnect } = useVueFlow() |
| 40 | + |
| 41 | +onConnect(({ source, target, sourceHandle, targetHandle }) => { |
| 42 | + console.log('source', source) |
| 43 | + console.log('target', target) |
| 44 | + // these are the handle ids of the source and target node |
| 45 | + // if no id is specified these will be `null`, meaning the first handle of the necessary type will be used |
| 46 | + console.log('sourceHandle', sourceHandle) |
| 47 | + console.log('targetHandle', targetHandle) |
| 48 | +}) |
| 49 | +``` |
| 50 | + |
| 51 | +## Hide Handles |
| 52 | + |
| 53 | +In some cases you might not want to display a handle at all. You can hide a handle by setting `opacity: 0` as the styles for that handle. |
| 54 | + |
| 55 | +You cannot hide a handle by removing it from the DOM (for example using `v-if` or `v-show`) as that would break the calculations for the edges. |
| 56 | + |
| 57 | +```vue |
| 58 | +<Handle type="source" :position="Position.Right" style="opacity: 0" /> |
| 59 | +``` |
0 commit comments