Skip to content

Commit b493893

Browse files
committed
docs(guide): extend custom node and edges section
1 parent 4786e7a commit b493893

File tree

2 files changed

+153
-35
lines changed

2 files changed

+153
-35
lines changed

docs/src/guide/edge.md

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,13 @@ Edge-types are determined from your edges' definitions.
433433

434434
::: code-group
435435

436-
```js [edges <LogosJavascript />]
436+
```vue [App.vue <LogosJavascript />]
437+
<script setup>
437438
import { ref } from 'vue'
439+
import { VueFlow } from '@vue-flow/core'
440+
441+
import CustomEdge from './CustomEdge.vue'
442+
import SpecialEdge from './SpecialEdge.vue'
438443
439444
export const edges = ref([
440445
{
@@ -445,22 +450,57 @@ export const edges = ref([
445450
type: 'custom',
446451
},
447452
{
448-
id: 'e1-2',
453+
id: 'e1-3',
449454
source: '1',
450-
target: '2',
455+
target: '3',
451456
// this will create the edge-type `special`
452457
type: 'special',
453458
}
454459
])
460+
461+
const nodes = ref([
462+
{
463+
id: '1',
464+
label: 'Node 1',
465+
position: { x: 50, y: 50 },
466+
},
467+
{
468+
id: '2',
469+
label: 'Node 2',
470+
position: { x: 50, y: 250 },
471+
},
472+
{
473+
id: '3',
474+
label: 'Node 3',
475+
position: { x: 250, y: 50 },
476+
},
477+
{
478+
id: '4',
479+
label: 'Node 4',
480+
position: { x: 250, y: 250 },
481+
},
482+
])
483+
</script>
484+
485+
<template>
486+
<VueFlow :nodes="nodes" :edges="edges">
487+
<template #edge-custom="customEdgeProps">
488+
<CustomEdge v-bind="customEdgeProps" />
489+
</template>
490+
491+
<template #edge-special="specialEdgeProps">
492+
<SpecialEdge v-bind="specialEdgeProps" />
493+
</template>
494+
</VueFlow>
495+
</template>
455496
```
456497

457498
```vue [CustomEdge.vue <LogosJavascript />]
458499
<script setup>
459500
import { BezierEdge } from '@vue-flow/core';
460501
502+
// props were passed from the slot using `v-bind="customEdgeProps"`
461503
const props = defineProps(['sourceX', 'sourceY', 'targetX', 'targetY', 'sourcePosition', 'targetPosition']);
462-
463-
console.log(props.data.hello) // 'world'
464504
</script>
465505
466506
<script lang="ts">
@@ -481,9 +521,14 @@ export default {
481521
</template>
482522
```
483523

484-
```ts [edges <LogosTypescript />]
524+
```vue{25-26,32-33,40-42} [App.vue <LogosTypescript />]
525+
<script setup lang="ts">
485526
import { ref } from 'vue'
486527
import type { Edge } from '@vue-flow/core'
528+
import { VueFlow } from '@vue-flow/core'
529+
530+
import CustomEdge from './CustomEdge.vue'
531+
import SpecialEdge from './SpecialEdge.vue'
487532
488533
// You can pass 3 optional generic arguments to the Edge type, allowing you to define:
489534
// 1. The data object type
@@ -496,33 +541,69 @@ interface CustomData {
496541
497542
type CustomEdgeTypes = 'custom' | 'special'
498543
499-
type CustomEdge = Edge<CustomEdgeData, any, CustomEdgeTypes>
544+
type CustomEdge = Edge<CustomData, any, CustomEdgeTypes>
500545
501546
export const edges = ref<CustomEdge[]>([
502547
{
503-
id: 'e1-2',
504-
source: '1',
505-
target: '2',
506-
// this will create the edge-type `custom`
507-
type: 'custom',
548+
id: 'e1-2',
549+
source: '1',
550+
target: '2',
551+
// this will create the edge-type `custom`
552+
type: 'custom',
508553
},
509554
{
510-
id: 'e1-2',
511-
source: '1',
512-
target: '2',
513-
// this will create the edge-type `special`
514-
type: 'special',
555+
id: 'e1-3',
556+
source: '1',
557+
target: '3',
558+
// this will create the edge-type `special`
559+
type: 'special',
515560
},
516561
517-
// this will throw a type error, as the type is not defined in the CustomEdgeTypes
518-
// regardless it would be rendered as a default edge type
519562
{
520-
id: 'e1-2',
521-
source: '1',
522-
target: '2',
523-
type: 'not-defined', // should be 'custom' | 'special'
563+
id: 'e1-4',
564+
source: '1',
565+
target: '4',
566+
// this will throw a type error, as the type is not defined in the CustomEdgeTypes
567+
// regardless it would be rendered as a default edge type
568+
type: 'not-defined',
524569
}
525570
])
571+
572+
const nodes = ref([
573+
{
574+
id: '1',
575+
label: 'Node 1',
576+
position: { x: 50, y: 50 },
577+
},
578+
{
579+
id: '2',
580+
label: 'Node 2',
581+
position: { x: 50, y: 250 },
582+
},
583+
{
584+
id: '3',
585+
label: 'Node 3',
586+
position: { x: 250, y: 50 },
587+
},
588+
{
589+
id: '4',
590+
label: 'Node 4',
591+
position: { x: 250, y: 250 },
592+
},
593+
])
594+
</script>
595+
596+
<template>
597+
<VueFlow :nodes="nodes" :edges="edges">
598+
<template #edge-custom="customEdgeProps">
599+
<CustomEdge v-bind="customEdgeProps" />
600+
</template>
601+
602+
<template #edge-special="specialEdgeProps">
603+
<SpecialEdge v-bind="specialEdgeProps" />
604+
</template>
605+
</VueFlow>
606+
</template>
526607
```
527608

528609
```vue [CustomEdge.vue <LogosTypescript />]
@@ -532,6 +613,7 @@ import { BezierEdge } from '@vue-flow/core';
532613
533614
import { CustomData } from './edges'
534615
616+
// props were passed from the slot using `v-bind="customEdgeProps"`
535617
const props = defineProps<EdgeProps<CustomData>>();
536618
537619
console.log(props.data.hello) // 'world'
@@ -676,8 +758,7 @@ But you may wish to expand on these features or implement your business logic in
676758
## Edge Events
677759

678760
Vue Flow provides two main ways of listening to edge events,
679-
either by using `useVueFlow` to bind listeners to the event handlers
680-
or by using binding listeners to the `<VueFlow>` component.
761+
either by using `useVueFlow` to bind listeners to the event handlers or by binding them to the `<VueFlow>` component.
681762

682763
::: code-group
683764

docs/src/guide/node.md

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,13 @@ Node-types are determined from your nodes' definitions.
474474

475475
::: code-group
476476

477-
```js [nodes <LogosJavascript />]
477+
```vue{11-12,18-19} [App.vue <LogosJavascript />]
478+
<script setup>
478479
import { ref } from 'vue'
480+
import { VueFlow } from '@vue-flow/core'
481+
482+
import CustomNode from './CustomNode.vue'
483+
import SpecialNode from './SpecialNode.vue'
479484
480485
export const nodes = ref([
481486
{
@@ -493,12 +498,26 @@ export const nodes = ref([
493498
position: { x: 150, y: 50 },
494499
}
495500
])
501+
</script>
502+
503+
<template>
504+
<VueFlow :nodes="nodes">
505+
<template #node-custom="customNodeProps">
506+
<CustomNode v-bind="customNodeProps" />
507+
</template>
508+
509+
<template #node-special="specialNodeProps">
510+
<SpecialNode v-bind="specialNodeProps" />
511+
</template>
512+
</VueFlow>
513+
</template>
496514
```
497515

498516
```vue [CustomNode.vue <LogosJavascript />]
499517
<script setup>
500518
import { Position } from '@vue-flow/core'
501519
520+
// props were passed from the slot using `v-bind="customNodeProps"`
502521
const props = defineProps(['label'])
503522
</script>
504523
@@ -511,9 +530,14 @@ const props = defineProps(['label'])
511530
</template>
512531
```
513532

514-
```ts [nodes <LogosTypescript />]
533+
```vue{30-31,37-38,45-47} [App.vue <LogosTypescript />]
534+
<script setup lang="ts">
515535
import { ref } from 'vue'
516536
import type { Node } from '@vue-flow/core'
537+
import { VueFlow } from '@vue-flow/core'
538+
539+
import CustomNode from './CustomNode.vue'
540+
import SpecialNode from './SpecialNode.vue'
517541
518542
// You can pass 3 optional generic arguments to the Node interface, allowing you to define:
519543
// 1. The data object type
@@ -541,22 +565,35 @@ export const nodes = ref<CustomNode[]>([
541565
position: { x: 50, y: 50 },
542566
},
543567
{
544-
id: '1',
545-
label: 'Node 1',
568+
id: '2',
569+
label: 'Node 2',
546570
// this will create the node-type `special`
547571
type: 'special',
548572
position: { x: 150, y: 50 },
549573
},
550574
551-
// this will throw a type error, as the type is not defined in the CustomEdgeTypes
552-
// regardless it would be rendered as a default edge type
553575
{
554-
id: '1',
555-
label: 'Node 1',
576+
id: '3',
577+
label: 'Node 3',
578+
// this will throw a type error, as the type is not defined in the CustomEdgeTypes
579+
// regardless it would be rendered as a default edge type
556580
type: 'invalid',
557581
position: { x: 150, y: 50 },
558582
}
559583
])
584+
</script>
585+
586+
<template>
587+
<VueFlow :nodes="nodes">
588+
<template #node-custom="customNodeProps">
589+
<CustomNode v-bind="customNodeProps" />
590+
</template>
591+
592+
<template #node-special="specialNodeProps">
593+
<SpecialNode v-bind="specialNodeProps" />
594+
</template>
595+
</VueFlow>
596+
</template>
560597
```
561598

562599
```vue [CustomNode.vue <LogosTypescript />]
@@ -566,6 +603,7 @@ import { Position } from '@vue-flow/core'
566603
567604
import { CustomData, CustomEvents } from './nodes'
568605
606+
// props were passed from the slot using `v-bind="customNodeProps"`
569607
const props = defineProps<NodeProps<CustomData, CustomEvents>>()
570608
571609
console.log(props.data.hello) // 'world'
@@ -691,8 +729,7 @@ But you may wish to expand on these features or implement your business logic in
691729
## [Node Events](/typedocs/types/NodeEventsHandler)
692730

693731
Vue Flow provides two main ways of listening to node events,
694-
either by using `useVueFlow` to bind listeners to the event handlers
695-
or by using binding listeners to the `<VueFlow>` component.
732+
either by using `useVueFlow` to bind listeners to the event handlers or by binding them to the `<VueFlow>` component.
696733

697734
::: code-group
698735

0 commit comments

Comments
 (0)