Learn
Guides
Sub Flows

Sub Flows

A sub flow is essentially a flow contained within a node. A sub flow can either function as an independent entity or can be interconnected with nodes outside of its parent node. This feature can be used for grouping nodes. In this section of the documentation, we will implement a flow with sub flows and explain available options for child nodes.

⚠️

Order of Nodes: It's important that your parent nodes appear before their children in the nodes array to get processed correctly.

Defining child nodes

To define a node as a child of another node, you need to use the parentId option (you can find a list of all options in the node options section). Once we do that, the child node is positioned relative to its parent. A position of { x: 0, y: 0 } is at the top left corner of the parent.

const nodes = writable([
  // this is a regular node
  {
    id: 'A',
    data: {},
    position: { x: 0, y: 0 },
  },
  // this gets a child node by using the parentId option
  {
    id: 'B',
    data: { label: 'child 1' },
    position: { x: 10, y: 10 },
    // 👇
    parentId: 'A',
  },
]);

In the following example, we define a fixed width and height of the parent node by using the style option. Additionally, we configure the child extent to 'parent' to restrict the movement of child nodes beyond the boundaries of the parent node.

How child nodes work

When you drag the parent node, you will notice that the child nodes move accordingly. Adding a node to another node with the parentId option positions it relative to its parent. In terms of markup, the child node is not a subordinate. You can drag or position the child outside of its parent (when the extent: 'parent' option is not set). Nevertheless, when you drag the parent, the child moves too.

In the provided example, we use the group type for the parent node. The 'group' type is a convenient node type without handles, but you can use any other node type for this.

Let's continue by adding more nodes and edges. As you can see, we can connect nodes within a group and create connections that go from a sub flow to an outer node:

Any node can be a parent node

To demonstrate, let's remove the label from node B and add some child nodes. This example highlights the flexibility of using one of the default node types as a parent. Furthermore, we set the child nodes to have draggable: false, rendering them non-movable.