# `<llumi-graph>`

Interactive graph powered by [Cytoscape.js](https://js.cytoscape.org): nodes and
edges, pluggable layouts, zoom and pan (native trackpad gestures on macOS).
Renders in **shadow DOM**. React: `Graph` in `graph.react.tsx`.

## Usage

```html
<script type="module" src="https://esm.sh/@llumi/ui@1/graph.mjs"></script>

<llumi-graph style="height: 400px"></llumi-graph>
<script>
  const graph = document.querySelector("llumi-graph");
  graph.elements = [
    { data: { id: "a", label: "A" } },
    { data: { id: "b", label: "B" } },
    { data: { id: "ab", source: "a", target: "b" } },
  ];
  graph.layout = { name: "circle" };
</script>
```

All inputs are properties (no attributes). Pass stable references: assigning a
new array or object counts as a change.

## Properties

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `elements` | `ElementDefinition[]` | `[]` | Nodes and edges. Changing it re-runs the layout. |
| `stylesheet` | `StylesheetStyle[]` | `DEFAULT_STYLESHEET` | Cytoscape stylesheet (labels from `data(label)`, bezier edges with arrows). |
| `layout` | `GraphLayoutOptions` | `{ name: "cose" }` | Layout; re-runs on change. Built-in: `cose`, `grid`, `circle`, `concentric`, `breadthfirst`, `random`, `preset`; `dagre`, `cose-bilkent` and `elk` need their plugin. |
| `options` | `Omit<CytoscapeOptions, "container" \| "elements" \| "style" \| "layout">` | — | Extra Cytoscape options. Changing it recreates the instance. |
| `plugins` | `((cytoscape) => void)[]` | `[]` | Extensions (e.g. `cytoscape-dagre`'s default export), registered once. Changing it recreates the instance. |
| `cy` | `Core \| null` (read-only) | — | The Cytoscape instance, for direct access. |

The host is `display: block` with a `min-height` of 400px; size it with CSS.
The canvas container is exposed as `::part(container)`.

## Events

| Event | Detail |
|-------|--------|
| `llumi-graph-ready` | `{ cy }`, each time a Cytoscape instance is created |

## Dependencies

`lit`, `cytoscape` (+ `@types/cytoscape`). References for the stylesheet and
selectors: [`docs/cytoscape-style.md`](./docs/cytoscape-style.md),
[`docs/cytoscape-selector.md`](./docs/cytoscape-selector.md).

## React

```tsx
import dagre from "cytoscape-dagre";
import { Graph } from "./graph.react";

const plugins = [dagre]; // stable: module level or memoized

<Graph elements={elements} layout={{ name: "dagre", rankDir: "LR" }} plugins={plugins} onReady={(cy) => cy.fit()} />
```

Props are the properties above, plus `onReady(cy)`, `ref` (the `LlumiGraph`
element; `ref.current.cy` is the instance), `className` and `style` (CSS of the
host; the Cytoscape stylesheet is `stylesheet`).
