# `LiveFlow`
[🔗](https://github.com/rocket4ce/live_flow/blob/main/lib/live_flow/live_flow.ex#L1)

LiveFlow - A Phoenix LiveView library for building interactive node-based diagrams.

LiveFlow provides components and utilities for creating flow diagrams similar to
React Flow or Svelte Flow, but using Phoenix LiveView and LiveComponents.

## Quick Start

    # In your LiveView
    defmodule MyAppWeb.FlowLive do
      use MyAppWeb, :live_view

      alias LiveFlow.{State, Node, Edge}

      def mount(_params, _session, socket) do
        flow = State.new(
          nodes: [
            Node.new("1", %{x: 100, y: 100}, %{label: "Start"}),
            Node.new("2", %{x: 300, y: 200}, %{label: "End"})
          ],
          edges: [
            Edge.new("e1", "1", "2")
          ]
        )

        {:ok, assign(socket, flow: flow)}
      end

      def render(assigns) do
        ~H"""
        <.live_component
          module={LiveFlow.Components.Flow}
          id="my-flow"
          flow={@flow}
          opts={%{controls: true, background: :dots}}
        />
        """
      end
    end

## Components

  * `LiveFlow.Components.Flow` - Main flow container component
  * `LiveFlow.Components.NodeWrapper` - Node wrapper component
  * `LiveFlow.Components.Edge` - Edge rendering component
  * `LiveFlow.Components.Handle` - Handle component for connections

## Data Structures

  * `LiveFlow.Node` - Node struct
  * `LiveFlow.Edge` - Edge struct
  * `LiveFlow.Handle` - Handle struct
  * `LiveFlow.Viewport` - Viewport (pan/zoom) state
  * `LiveFlow.State` - Main state container

## Path Types

  * `LiveFlow.Paths.Bezier` - Smooth curved paths
  * `LiveFlow.Paths.Straight` - Direct lines
  * `LiveFlow.Paths.Step` - Orthogonal paths with 90° turns
  * `LiveFlow.Paths.Smoothstep` - Orthogonal paths with rounded corners

# `animate_positions`

```elixir
@spec animate_positions(
  Phoenix.LiveView.Socket.t(),
  String.t(),
  LiveFlow.State.t(),
  map() | list(),
  keyword()
) :: Phoenix.LiveView.Socket.t()
```

Animates nodes of the flow rendered with `id` to new positions.

Instead of updating the state right away, the client animates the nodes
(and their edges) and pushes the final positions back as an
`lf:node_change` event, which the usual node change handling applies.

`positions` is a map (or list of `{id, position}`) of node IDs to
`%{x: x, y: y}`. For child nodes the position is relative to the parent,
like `LiveFlow.Node.position`.

## Options

  * `:duration` - Animation duration in milliseconds (default: `300`)
  * `:fit_view` - Also fit the view to the final positions, `true` or
    `%{padding: 0.1}` (default: `false`)

## Examples

    positions = LiveFlow.Layout.tree(flow, direction: :lr)
    {:noreply, LiveFlow.animate_positions(socket, "my-flow", flow, positions)}

# `create_flow`

```elixir
@spec create_flow(keyword()) :: LiveFlow.State.t()
```

Creates a new flow state with the given nodes and edges.

## Examples

    iex> LiveFlow.create_flow(
    ...>   nodes: [
    ...>     %{id: "1", position: %{x: 0, y: 0}, data: %{label: "A"}},
    ...>     %{id: "2", position: %{x: 100, y: 100}, data: %{label: "B"}}
    ...>   ],
    ...>   edges: [
    ...>     %{id: "e1", source: "1", target: "2"}
    ...>   ]
    ...> )

# `new_edge`

# `new_handle`

# `new_node`

# `new_state`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
