> ## Documentation Index
> Fetch the complete documentation index at: https://npm-packages.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Workflow

> Arrange steps and connect a small publishing workflow.

Package: [`@xyflow/react`](https://www.npmjs.com/package/@xyflow/react).

## Try it

Drag the steps, connect two handles, and use the zoom controls. Select an edge and press Backspace to remove it; Reset restores the starting graph.

## Code

```tsx flow-example.tsx theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { useCallback, useId, useState } from "react";
import {
  addEdge,
  Background,
  Controls,
  ReactFlow,
  useEdgesState,
  useNodesState,
} from "@xyflow/react";
import type { Connection, Edge, Node } from "@xyflow/react";
import { connectionError, makeNode, workflowEdges, workflowNodes } from "./flow-data.ts";
import { DemoIcon } from "./demo-icons.tsx";
import "@xyflow/react/dist/style.css";
import "./flow-example.css";

const validationNodes: Node[] = [
  makeNode("ingest", "Ingest", 30, 80),
  makeNode("transform", "Transform", 260, 180),
  makeNode("publish", "Publish", 490, 80),
];
const validationEdges: Edge[] = [
  { id: "ingest-transform", source: "ingest", target: "transform" },
  { id: "transform-publish", source: "transform", target: "publish" },
];

export function FlowExample({ validation = false }: { validation?: boolean }) {
  const [nodes, setNodes, onNodesChange] = useNodesState(
    validation ? validationNodes : workflowNodes(),
  );
  const [edges, setEdges, onEdgesChange] = useEdgesState(
    validation ? validationEdges : workflowEdges(),
  );
  const [source, setSource] = useState("publish");
  const [target, setTarget] = useState("ingest");
  const [message, setMessage] = useState("Connections must point forward without a cycle.");
  const selectedSource = nodes.some((node) => node.id === source) ? source : (nodes[0]?.id ?? "");
  const selectedTarget = nodes.some((node) => node.id === target)
    ? target
    : (nodes[nodes.length - 1]?.id ?? "");
  const sourceId = useId();
  const targetId = useId();
  const validConnection = useCallback(
    (connection: Edge | Connection) =>
      nodes.some((node) => node.id === connection.source) &&
      nodes.some((node) => node.id === connection.target) &&
      !connectionError(connection, edges),
    [edges, nodes],
  );
  const onConnect = useCallback(
    (connection: Connection) => {
      if (
        !nodes.some((node) => node.id === connection.source) ||
        !nodes.some((node) => node.id === connection.target)
      ) {
        setMessage("Choose two existing steps.");
        return;
      }
      if (validation) {
        const error = connectionError(connection, edges);
        if (error) {
          setMessage(error);
          return;
        }
      }
      setEdges((current) => addEdge(connection, current));
      setMessage(`Connected ${connection.source} to ${connection.target}.`);
    },
    [edges, nodes, setEdges, validation],
  );

  function reset() {
    setNodes(validation ? validationNodes : workflowNodes());
    setEdges(validation ? validationEdges : workflowEdges());
    setMessage("Connections must point forward without a cycle.");
  }

  return (
    <section
      className="flow-example not-prose"
      aria-label={validation ? "Connection validation example" : "Workflow example"}
    >
      <div className="flow-toolbar">
        <span>
          {nodes.length} steps · {edges.length} connections
        </span>
        <button type="button" className="demo-button demo-button-small" onClick={reset}>
          <DemoIcon name="reset" />
          Reset diagram
        </button>
      </div>
      <div className="flow-canvas">
        <ReactFlow
          id={validation ? "validation-flow" : "workflow-flow"}
          nodes={nodes}
          edges={edges}
          onNodesChange={onNodesChange}
          onEdgesChange={onEdgesChange}
          onConnect={onConnect}
          isValidConnection={validation ? validConnection : undefined}
          width={740}
          height={430}
          fitView
          fitViewOptions={{ padding: 0.2 }}
          minZoom={0.35}
          maxZoom={1.8}
          colorMode="system"
          defaultEdgeOptions={{ type: "smoothstep" }}
        >
          <Background gap={22} size={1} color="var(--demo-border)" />
          <Controls showInteractive={false} />
        </ReactFlow>
      </div>
      {validation && (
        <form
          className="flow-validation"
          onSubmit={(event) => {
            event.preventDefault();
            onConnect({
              source: selectedSource,
              target: selectedTarget,
              sourceHandle: null,
              targetHandle: null,
            });
          }}
        >
          <div className="flow-connection-field">
            <label htmlFor={sourceId}>From</label>
            <span className="demo-select-wrap">
              <select
                className="demo-select"
                id={sourceId}
                value={selectedSource}
                onChange={(event) => setSource(event.currentTarget.value)}
              >
                {nodes.map((node) => (
                  <option key={node.id} value={node.id}>
                    {String(node.data.label)}
                  </option>
                ))}
              </select>
              <DemoIcon name="chevron-down" />
            </span>
          </div>
          <div className="flow-connection-field">
            <label htmlFor={targetId}>To</label>
            <span className="demo-select-wrap">
              <select
                className="demo-select"
                id={targetId}
                value={selectedTarget}
                onChange={(event) => setTarget(event.currentTarget.value)}
              >
                {nodes.map((node) => (
                  <option key={node.id} value={node.id}>
                    {String(node.data.label)}
                  </option>
                ))}
              </select>
              <DemoIcon name="chevron-down" />
            </span>
          </div>
          <button type="submit" className="demo-button" disabled={nodes.length < 2}>
            Add connection
          </button>
          <p role="status">{message}</p>
        </form>
      )}
      {!validation && (
        <p className="flow-caption">
          Drag a step, connect the handles, or select a connection and press Backspace to remove it.
        </p>
      )}
    </section>
  );
}
```

## Keyboard interaction

Tab to a node and use arrow keys to move it. Press Escape to release selection. The package supplies node descriptions and keyboard controls.

## Rendering

Nodes include explicit dimensions and handle positions so the diagram can render in the initial HTML. Pan, zoom, and editing become available when the component hydrates. [React Flow reference](https://reactflow.dev/learn/core-concepts/adding-interactivity).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.