Skip to content

Commit 2637733

Browse files
committed
chore(docs): update custom node example
1 parent 1006006 commit 2637733

File tree

4 files changed

+44
-93
lines changed

4 files changed

+44
-93
lines changed

docs/examples/custom-node/App.vue

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
11
<script setup>
2-
import { MiniMap } from '@vue-flow/minimap'
3-
import { Position, VueFlow, useVueFlow } from '@vue-flow/core'
42
import { ref } from 'vue'
3+
import { MiniMap } from '@vue-flow/minimap'
4+
import { Position, VueFlow, useNodesData, useVueFlow } from '@vue-flow/core'
55
import ColorSelectorNode from './ColorSelectorNode.vue'
6+
import OutputNode from './OutputNode.vue'
67
import { presets } from './presets.js'
78
8-
const { findNode } = useVueFlow()
9-
10-
const gradient = ref(false)
9+
useVueFlow()
1110
12-
const bgColor = ref(presets.ayame)
13-
14-
const bgName = ref('AYAME')
11+
const nodeData = useNodesData('1')
1512
1613
const nodes = ref([
1714
{
1815
id: '1',
1916
type: 'color-selector',
20-
data: { color: bgColor.value },
17+
data: { color: presets.ayame },
2118
position: { x: 0, y: 50 },
2219
},
2320
{
2421
id: '2',
2522
type: 'output',
26-
position: { x: 350, y: 25 },
27-
targetPosition: Position.Left,
28-
},
29-
{
30-
id: '3',
31-
type: 'output',
32-
position: { x: 350, y: 200 },
23+
position: { x: 350, y: 114 },
3324
targetPosition: Position.Left,
3425
},
3526
])
@@ -42,25 +33,12 @@ const edges = ref([
4233
target: '2',
4334
animated: true,
4435
style: () => ({
45-
stroke: bgColor.value,
46-
filter: 'invert(100%)',
47-
}),
48-
},
49-
{
50-
id: 'e1b-3',
51-
source: '1',
52-
sourceHandle: 'b',
53-
target: '3',
54-
animated: true,
55-
style: () => ({
56-
stroke: bgColor.value,
36+
stroke: nodeData.value?.color,
5737
filter: 'invert(100%)',
5838
}),
5939
},
6040
])
6141
62-
const connectionLineStyle = { stroke: '#fff' }
63-
6442
// minimap stroke color functions
6543
function nodeStroke(n) {
6644
switch (n.type) {
@@ -77,44 +55,31 @@ function nodeStroke(n) {
7755
7856
function nodeColor(n) {
7957
if (n.type === 'color-selector') {
80-
return bgColor.value
58+
return nodeData.value?.color
8159
}
8260
8361
return '#fff'
8462
}
85-
86-
function onChange(color) {
87-
gradient.value = false
88-
bgColor.value = color.value
89-
bgName.value = color.name
90-
91-
findNode('3').hidden = false
92-
}
93-
94-
function onGradient() {
95-
gradient.value = true
96-
bgColor.value = null
97-
bgName.value = 'gradient'
98-
99-
findNode('3').hidden = true
100-
}
10163
</script>
10264
10365
<template>
10466
<VueFlow
10567
:nodes="nodes"
10668
:edges="edges"
10769
class="custom-node-flow"
108-
:class="[gradient ? 'animated-bg-gradient' : '']"
109-
:style="{ backgroundColor: bgColor }"
110-
:connection-line-style="connectionLineStyle"
70+
:class="[nodeData?.isGradient ? 'animated-bg-gradient' : '']"
71+
:style="{ backgroundColor: nodeData?.color }"
11172
:default-viewport="{ zoom: 1.5 }"
11273
fit-view-on-init
11374
>
11475
<template #node-color-selector="{ data }">
11576
<ColorSelectorNode :data="data" />
11677
</template>
11778
79+
<template #node-output="{ data }">
80+
<OutputNode :data="data" />
81+
</template>
82+
11883
<MiniMap :node-stroke-color="nodeStroke" :node-color="nodeColor" />
11984
</VueFlow>
12085
</template>
Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,42 @@
11
<script setup>
2-
import { Handle, Position, useVueFlow } from '@vue-flow/core'
2+
import { Handle, Position, useNodeId, useVueFlow } from '@vue-flow/core'
33
import { colors } from './presets.js'
44
5-
const props = defineProps({
6-
id: {
7-
type: String,
8-
required: true,
9-
},
5+
defineProps({
106
data: {
117
type: Object,
128
required: true,
139
},
1410
})
1511
12+
const nodeId = useNodeId()
13+
1614
const { updateNodeData } = useVueFlow()
1715
1816
function onSelect(color) {
19-
updateNodeData(props.id, { color: color.value }, { replace: true })
17+
updateNodeData(nodeId, { color }, { replace: true })
2018
}
2119
2220
function onGradient() {
23-
updateNodeData(props.id, { isGradient: true }, { replace: true })
21+
updateNodeData(nodeId, { isGradient: true }, { replace: true })
2422
}
2523
</script>
2624

2725
<template>
2826
<div>Select a color</div>
2927

30-
<div
31-
class="color-selector"
32-
style="display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center; max-width: 90%; margin: auto; gap: 4px"
33-
>
34-
<template v-for="color of colors" :key="color.name">
35-
<button :title="color.name" :style="{ backgroundColor: color.value }" type="button" @click="onSelect(color)" />
36-
</template>
28+
<div class="color-selector nodrag nopan">
29+
<button
30+
v-for="{ name: colorName, value: color } of colors"
31+
:key="colorName"
32+
:title="colorName"
33+
:style="{ backgroundColor: color }"
34+
type="button"
35+
@click="onSelect(color)"
36+
/>
3737

3838
<button class="animated-bg-gradient" title="gradient" type="button" @click="onGradient" />
3939
</div>
4040

41-
<!-- When using multiple handles of the same type, each handle should have a unique id -->
42-
<Handle
43-
id="a"
44-
type="source"
45-
:position="Position.Right"
46-
:style="{ backgroundColor: data.color, filter: 'invert(100%)', top: '10px' }"
47-
/>
48-
49-
<Handle
50-
id="b"
51-
type="source"
52-
:position="Position.Right"
53-
:style="{
54-
backgroundColor: data.color,
55-
filter: 'invert(100%)',
56-
bottom: '10px',
57-
top: 'auto',
58-
}"
59-
/>
41+
<Handle id="a" type="source" :position="Position.Right" />
6042
</template>
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<script setup>
2-
import { useHandleConnections, useNodesData } from '@vue-flow/core'
2+
import { Handle, Position, useHandleConnections, useNodesData } from '@vue-flow/core'
33
4-
const props = defineProps({
5-
id: {
6-
type: String,
4+
defineProps({
5+
data: {
6+
type: Object,
77
required: true,
88
},
99
})
1010
1111
const connections = useHandleConnections({
12-
nodeId: props.id,
1312
type: 'target',
1413
})
1514
16-
const data = useNodesData()
15+
const data = useNodesData(() => connections.value[0]?.source)
1716
</script>
17+
18+
<template>
19+
<Handle type="target" :position="Position.Left" :style="{ backgroundColor: data?.color, filter: 'invert(100%)' }" />
20+
{{ data?.isGradient ? 'GRADIENT' : data?.color }}
21+
</template>

docs/examples/custom-node/style.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
}
2323

2424
.custom-node-flow .vue-flow__node-color-selector .color-selector button {
25-
border: none;
25+
border: 1px solid #777;
2626
cursor: pointer;
2727
padding: 5px;
2828
width: 25px;
2929
height: 25px;
3030
border-radius: 25px;
31-
-webkit-box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.3);
32-
box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.3);
3331
}
3432

3533
.custom-node-flow .vue-flow__node-color-selector .color-selector button:hover {
3634
-webkit-box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.5);
3735
box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.5);
36+
transform: scale(105%);
37+
transition: 250ms all ease;
3838
}
3939

4040
.animated-bg-gradient {

0 commit comments

Comments
 (0)