# Web Component & Plugins

> Embed <praisonai-diagram> in any HTML page or framework, handle events, and register custom layout plugins.

# Web Component & Plugins (`aidiagram-js`)

`aidiagram-js` ships a zero-dependency Shadow-DOM Web Component (`<praisonai-diagram>`) and a modular ESM plugin registry.

## Step 1: Install & Register

```bash
npm install aidiagram-js
```

```html
<script type="module">
  import 'aidiagram-js'
</script>

<praisonai-diagram theme="auto" accent="amber">
flowchart LR
  A[Open pull request] --> B[Review changes]
  B --> C[Maintainer decides whether to merge]
</praisonai-diagram>
```

Importing `aidiagram-js` registers `<praisonai-diagram>` (primary), `<ai-diagram>`, and `<praisonai-visual>` (legacy alias) using distinct subclasses so all tags can coexist on the same page and repeated imports are safe no-ops.

## Step 2: Configure Attributes & Properties

| Attribute / Property | Values | Purpose |
|---|---|---|
| `theme` attribute | `auto` (default), `light`, `dark` | Switches light/dark tokens (follows `.dark`, `data-theme="dark"`, or OS) |
| `accent` attribute | `amber` (default), `indigo`, `teal`, `rose` | Picks the accent colour preset |
| `el.spec` property | Mermaid string, YAML string, JSON string, or JS object | Dynamically updates the rendered diagram |

## Step 3: Listen to Events

```js
const el = document.querySelector('praisonai-diagram')

el.addEventListener('diagram:select', (e) => {
  console.log('Active step:', e.detail) // { node, edge, stepIndex }
})

el.addEventListener('diagram:fallback', (e) => {
  // Fired in standalone mode when an unsupported/opted-out Mermaid block is passed
  console.log('Preserved source:', e.detail.source)
})
```

## Step 4: Register a Custom Layout Plugin

Extend the engine at runtime without forking core:

```js
import { registerLayout, compileDiagram, parseDiagramSpec } from 'aidiagram-js'

registerLayout('status-card', (spec) => ({
  type: 'status-card',
  width: 520,
  height: 110,
  label: spec.label,
  shapes: [
    { kind: 'rect', x: 16, y: 16, w: 488, h: 78, rx: 10, cls: 'boxHi', nodeId: 's1' },
    { kind: 'text', x: 260, y: 60, text: String(spec.title ?? 'Ready'), cls: 'h2', anchor: 'middle', nodeId: 's1' },
  ],
  nodes: [{ id: 's1', index: 0, title: String(spec.title ?? 'Ready'), x: 16, y: 16, w: 488, h: 78, connectedNodeIds: [], connectedEdgeIndices: [] }],
  edges: [],
  stepMode: 'nodes',
}))

const parsed = parseDiagramSpec('type: status-card\nlabel: Ready card\ntitle: All systems go')
if (parsed.ok) compileDiagram(parsed.spec)
```

## Related pages

- [Quick Start](https://diagram.praison.ai/docs/getting-started/quickstart.md)
- [Python CLI & API](https://diagram.praison.ai/docs/guides/python-cli.md)
- [Timeline Layout](https://diagram.praison.ai/docs/layouts/timeline.md)
