From 30646f4c2b8306e552aeb0895701746d2879f00b Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Sun, 5 Nov 2023 17:58:11 +0100 Subject: [PATCH] docs(guide): update edge page --- docs/src/guide/edge.md | 565 ++++++++++++++++++++++++++++------------- 1 file changed, 387 insertions(+), 178 deletions(-) diff --git a/docs/src/guide/edge.md b/docs/src/guide/edge.md index 64c5326d..22cb5a90 100644 --- a/docs/src/guide/edge.md +++ b/docs/src/guide/edge.md @@ -1,5 +1,7 @@ -# Edges +# Introduction to Edges -Edges are what connects your nodes into a map. +Edges are the links connecting your nodes, forming a map. +Each edge runs from one handle to another, and can be customized to your liking. -They cannot exist on their own and need nodes to which they are connected. +Remember, every edge is unique and thus **requires a unique id**, a source and target node id. -Each edge requires a unique id, a source node and a target node id. +For the full list of options available for an edge, check out the [Edge Type](/typedocs/types/Edge). -You can view the full options-list for an edge [here](/typedocs/types/Edge). +## Adding Edges to the Graph -## Usage +Edges are generally created by adding them to the `mode-value` (using `v-model`) or to the `edges` prop of the Vue Flow component. +This can be done dynamically at any point in your component's lifecycle. -Generally you create edges by adding them to the model-value or the edges prop of the Vue Flow component. +:::code-group -```vue - + ``` -For more advanced graphs that require more state access you will want to use the useVueFlow composable. -[useVueFlow](/typedocs/functions/useVueFlow) will provide the [`addEdges`](/typedocs/interfaces/Actions#addedges/) action, -which you can use to add edges directly to the state. +```vue [] -```vue + + + +``` + +::: + +If you are working with more complex graphs that necessitate extensive state access, the `useVueFlow` composable should +be employed. +The [`addEdges`](/typedocs/interfaces/Actions#addEdges) action is available +through [useVueFlow](/typedocs/functions/useVueFlow), allowing you to add edges straight to the state. + +What's more, this action isn't limited to the component rendering the graph; it can be utilized elsewhere, like in a +Sidebar or Toolbar. + +::: code-group + +```vue [] + ``` -## [Default Edge-Types](/typedocs/interfaces/DefaultEdgeTypes) +```vue [] + + + +``` + +::: + +## [Predefined Edge-Types](/typedocs/interfaces/DefaultEdgeTypes) + +Vue Flow provides several built-in edge types that you can leverage immediately. +The included node types are `default` (bezier), `step`, `smoothstep` and `straight`. ### Default Edge (Bezier) The default edge is a bezier curve that connects two nodes. -
+
- +
@@ -186,9 +268,9 @@ The default edge is a bezier curve that connects two nodes. A step edge has a straight path with a step towards the target. -
+
- +
@@ -196,9 +278,9 @@ A step edge has a straight path with a step towards the target. The same as the step edge though with a border radius on the step (rounded step). -
+
- +
@@ -206,42 +288,155 @@ The same as the step edge though with a border radius on the step (rounded step) A simple straight path. -
+
- +
-## Custom Edges +## User-Defined Edges -In addition to the default edge types from the previous chapter, you can define any amount of custom edge-types. -Edge-types are inferred from your edge's definition. +On top of the default edge types mentioned earlier, you can create as many custom edge-types as you need. +Edge-types are determined from your edges' definitions. -```js{4} -const edges = [ +::: code-group + +```js [edges ] +import { ref } from 'vue' + +export const edges = ref([ { id: 'e1-2', - type: 'custom', source: '1', target: '2', + // this will create the edge-type `custom` + type: 'custom', }, -] + { + id: 'e1-2', + source: '1', + target: '2', + // this will create the edge-type `special` + type: 'special', + } +]) ``` -Vue Flow will now try to resolve this edge-type to a component. -First and foremost we will look for a definition in the `edgeTypes` object of the state. -After that we will try to resolve the component to a globally registered one that matches the exact name. -Finally, we will check if a template slot has been provided to fill the edge-type. +```vue [CustomEdge.vue ] + + + + + +``` + +```ts [edges ] +import { ref } from 'vue' +import type { Edge } from '@vue-flow/core' + +// You can pass 3 optional generic arguments to the Edge type, allowing you to define: +// 1. The data object type +// 2. The events object type +// 3. The possible edge types + +interface CustomData { + hello: string +} + +type CustomEdgeTypes = 'custom' | 'special' + +type CustomEdge = Edge + +export const edges = ref([ + { + id: 'e1-2', + source: '1', + target: '2', + // this will create the edge-type `custom` + type: 'custom', + }, + { + id: 'e1-2', + source: '1', + target: '2', + // this will create the edge-type `special` + type: 'special', + }, + + // this will throw a type error, as the type is not defined in the CustomEdgeTypes + // regardless it would be rendered as a default edge type + { + id: 'e1-2', + source: '1', + target: '2', + type: 'not-defined', + } +]) +``` + +```vue [CustomEdge.vue ] + + + + + +``` + +::: + +Vue Flow will then attempt to resolve this edge-type to a component. +Priority is given to a definition in the edgeTypes object of the state. +Next, it tries to match the component to a globally registered one with the same name. +Finally, it searches for a provided template slot to fill in the edge-type. + +If no methods produce a result in resolving the component, the default edge-type is used as a fallback. ### Template slots -The easiest way to define custom edges is, by passing them as template slots. -Your custom edge-types are dynamically resolved to slot-names, meaning an edge with the type `custom` -will expect a slot to have the name `edge-custom`. - -You can choose any name you want for your edge-type, and it will be resolved to the corresponding slot (i.e. `my-edge-type` -> `#edge-my-edge-type`) +One of the easiest ways to define custom edges is, by passing them as template slots. +Dynamic resolution to slot-names is done for your user-defined edge-types, +meaning a edge with the type `custom` is expected to have a slot named `#edge-custom`. ```vue{18,26} - -``` - -### [Custom Edge Props](/typedocs/interfaces/EdgeProps) - -Your custom edges are wrapped so that the basic functions like selecting work. -But you might want to extend on that functionality or implement your own business logic inside of edges, therefore -your edges receive the following props: +Your custom edges are enclosed so that fundamental functions like selecting operate. +But you may wish to expand on these features or implement your business logic inside edges, thus your edges receive the following properties: | Name | Definition | Type | Optional | |---------------------|-------------------------------|------------------------------------------------|--------------------------------------------| @@ -400,14 +545,74 @@ your edges receive the following props: | markerStart | Edge marker id | string | | | curvature | Edge path curvature | number | | -### (Custom) Edge Events +## Edge Events -In addition to the event handlers that you can access through [`useVueFlow`](/guide/composables#useVueFlow/) or the Vue Flow component, -you can also pass in event handlers in your initial edge definition, or you can access the edge events through the `events` prop passed -to your edge components. +Vue Flow provides two main ways of listening to edge events, +either by using `useVueFlow` to bind listeners to the event handlers +or by using binding listeners to the `` component. -```vue{19-26} +::: code-group + +```vue [useVueFlow] + + +``` + +```vue [component] + -``` -As you can see above, you can also pass in custom event handlers. These will not be called by Vue Flow but can be used -to forward callback functions to your custom components. -The `click` handler is part of the [`EdgeEventsHandler`](/typedocs/types/EdgeEventsHandler) type, meaning it will be -triggered when the edge is clicked. - -```vue - ``` + +::: + +
+ + +

Interact to see events in browser console

+
+ +
+
+ +## Customizing Appearance + +::: tip +To override the styles of the default theme, visit the [Theming section](/guide/theming). +:::