ExamplesInteraction

Computing Flows

This example combines useHandleConnections,useNodesData and updateNodeData helpers for handling data flow between nodes.

<script lang="ts">
  import { writable } from 'svelte/store';
  import {
    SvelteFlow,
    Controls,
    Background,
    BackgroundVariant,
    MiniMap,
    type Node,
    type NodeTypes,
    type Edge
  } from '@xyflow/svelte';
  import '@xyflow/svelte/dist/style.css';
 
  import TextNode from './TextNode.svelte';
  import UppercaseNode from './UppercaseNode.svelte';
  import ResultNode from './ResultNode.svelte';
 
  const nodeTypes: NodeTypes = {
    text: TextNode,
    uppercase: UppercaseNode,
    result: ResultNode
  };
 
  const nodes = writable<Node[]>([
    {
      id: '1',
      type: 'text',
      data: {
        text: 'hello'
      },
      position: { x: -100, y: -50 }
    },
    {
      id: '1a',
      type: 'uppercase',
      data: {},
      position: { x: 100, y: 0 }
    },
    {
      id: '2',
      type: 'text',
      data: {
        text: 'world'
      },
      position: { x: 0, y: 100 }
    },
 
    {
      id: '3',
      type: 'result',
      data: {},
      position: { x: 300, y: 50 }
    }
  ]);
 
  const edges = writable<Edge[]>([
    {
      id: 'e1-1a',
      source: '1',
      target: '1a'
    },
    {
      id: 'e1a-3',
      source: '1a',
      target: '3'
    },
    {
      id: 'e2-3',
      source: '2',
      target: '3'
    }
  ]);
</script>
 
<main>
  <SvelteFlow {nodes} {edges} {nodeTypes} fitView>
    <Controls />
    <Background variant={BackgroundVariant.Dots} />
    <MiniMap />
  </SvelteFlow>
</main>
 
<style>
  main {
    height: 100vh;
    display: flex;
    flex-direction: column-reverse;
  }
</style>