|
| 1 | +# loadSidebarV2 |
| 2 | + |
| 3 | +Embeds the Angie assistant in a host page (SaaS app, WordPress frontend, or any site) via an iframe and a small host-side shell. Use `AngieMcpSdk.loadSidebarV2()` from the public package API. |
| 4 | + |
| 5 | +## Quick start |
| 6 | + |
| 7 | +```typescript |
| 8 | +import { AngieMcpSdk, LAYOUT_FLOATING_CHAT } from '@elementor/angie-sdk'; |
| 9 | + |
| 10 | +const sdk = new AngieMcpSdk(); |
| 11 | + |
| 12 | +await sdk.loadSidebarV2({ |
| 13 | + host: { appId: 'my-app' }, |
| 14 | + container: { layout: LAYOUT_FLOATING_CHAT }, |
| 15 | +}); |
| 16 | +``` |
| 17 | + |
| 18 | +**Sidebar layout** (dock-style panel, resizable, open state persisted): |
| 19 | + |
| 20 | +```typescript |
| 21 | +await sdk.loadSidebarV2({ |
| 22 | + host: { appId: 'my-app' }, |
| 23 | + container: { |
| 24 | + layout: 'sidebar', |
| 25 | + styleTheme: 'wordpress', |
| 26 | + chatToggleButton: { |
| 27 | + enabled: true, |
| 28 | + selector: '#my-toggle', |
| 29 | + }, |
| 30 | + }, |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +Local demos: |
| 35 | + |
| 36 | +- [`demo/load-sidebar-v2-sidebar/`](../../demo/load-sidebar-v2-sidebar/) — minimal sidebar |
| 37 | +- [`demo/load-sidebar-v2-floating-chat/`](../../demo/load-sidebar-v2-floating-chat/) — minimal floating chat |
| 38 | +- [`demo/load-sidebar-v2-full-config/`](../../demo/load-sidebar-v2-full-config/) — full example (`aiContext`, custom CSS) |
| 39 | + |
| 40 | +## Layouts |
| 41 | + |
| 42 | +| Layout | Constant | Typical use | |
| 43 | +|--------|----------|-------------| |
| 44 | +| Sidebar | `LAYOUT_SIDEBAR` (`'sidebar'`) | Fixed side panel, resize, persist open/closed | |
| 45 | +| Floating chat | `LAYOUT_FLOATING_CHAT` (`'floatingChat'`) | Bottom-corner widget with optional injected toggle button | |
| 46 | + |
| 47 | +Each layout applies [presets](./presets/) (defaults for `persistOpenState`, `resizable`, `chatToggleButton`, etc.). Override any field via `container` options. |
| 48 | + |
| 49 | +## Configuration |
| 50 | + |
| 51 | +`LoadSidebarV2Options` (see [`config.ts`](./config.ts)): |
| 52 | + |
| 53 | +| Section | Purpose | |
| 54 | +|---------|---------| |
| 55 | +| `host` | **Required.** `appId`, optional `aiContext`, `website`, `analytics` sent to the embedded Angie app (see [aiContext](#hostaicontext)) | |
| 56 | +| `boot` | `allowInIframe` — skip boot when the host page is itself in an iframe (default `false`) | |
| 57 | +| `container` | DOM container id, `layout`, `styleTheme` (`'wordpress'` injects WP admin-bar CSS), resize/persist flags, chat toggle button | |
| 58 | +| `iframe` | Angie origin, path (`angie/embedded`), `uiTheme`, `isRTL` | |
| 59 | +| `callbacks` | `onClose`, `getExternalHeaders` for auth/API headers | |
| 60 | +| `widgetConfig` | Close button behavior (`collapse` vs `close`); layout-specific defaults in [`widget-config.ts`](./widget-config.ts) | |
| 61 | + |
| 62 | +Embedded config uses `configVersion: 2` (`LOAD_SIDEBAR_V2_CONFIG_VERSION`). |
| 63 | + |
| 64 | +### host.aiContext |
| 65 | + |
| 66 | +Object passed in `embedded.aiContext` on `HOST_READY` (and `sdk-embedded-config`). The embedded Angie app injects it into the agent so replies can use your host app state. |
| 67 | + |
| 68 | +Keep it focused on what helps the agent answer screen-level questions: |
| 69 | + |
| 70 | +| Key | Purpose | |
| 71 | +|-----|---------| |
| 72 | +| `whatUserSees` | What is on the current screen (labels, selection, visible fields) | |
| 73 | +| `whatUserCanDo` | Actions the user can take on this screen | |
| 74 | + |
| 75 | +Example: [`demo/load-sidebar-v2-full-config/host.js`](../../demo/load-sidebar-v2-full-config/host.js) reads `#demo-host-app` into `whatUserSees` and lists allowed actions in `whatUserCanDo`. |
| 76 | + |
| 77 | +Enable `widgetConfig.aiContextGuidance: { enabled: true }` so users see that host context is available. |
| 78 | + |
| 79 | +### Custom CSS (toggle + sidebar panel) |
| 80 | + |
| 81 | +| Target | Selector | Notes | |
| 82 | +|--------|----------|--------| |
| 83 | +| Toggle button | Your selector (e.g. `#my-angie-toggle`) | Host DOM; wire via `container.chatToggleButton.selector` | |
| 84 | +| Sidebar panel | `#${container.id}` (default `#angie-sidebar-container`) | SDK injects layout rules in `src/sidebar.css` | |
| 85 | +| Panel width / z-index | `:root { --angie-sidebar-width; --angie-sidebar-z-index; }` | Read by SDK when opening/resizing | |
| 86 | +| Gap from sidebar | `body.angie-sidebar-active { padding-inline-start: calc(var(--angie-sidebar-width) + 1.5rem) }` | SDK sets padding to width only; add your own gap (see demo CSS) | |
| 87 | +| Iframe | `#angie-sidebar-container iframe#angie-iframe` | `id="angie-iframe"` is set by the SDK | |
| 88 | + |
| 89 | +If you use a custom `container.id`, copy or adapt the rules from `sidebar.css` for your id. |
| 90 | + |
| 91 | +Example: [`demo/load-sidebar-v2-full-config/demo-host.css`](../../demo/load-sidebar-v2-full-config/demo-host.css). |
| 92 | + |
| 93 | +## Boot flow |
| 94 | + |
| 95 | +``` |
| 96 | +loadSidebarV2(options) |
| 97 | + → resolveConfig + shouldBoot |
| 98 | + → initHostApiBridge (postMessage API) |
| 99 | + → ensureSidebarContainer |
| 100 | + → layout strategy (initShell → open iframe → afterOpen) |
| 101 | + → sendEmbeddedConfig / sendWidgetConfig |
| 102 | +``` |
| 103 | + |
| 104 | +Entry point: [`boot-sidebar.ts`](./boot-sidebar.ts). Layout strategies: [`layouts/index.ts`](./layouts/index.ts). |
| 105 | + |
| 106 | +## Host API bridge |
| 107 | + |
| 108 | +[`host-api-bridge.ts`](./host-api-bridge.ts) listens for messages from the Angie iframe (origin-checked) and responds on a `MessagePort`: |
| 109 | + |
| 110 | +- `GET_EXTERNAL_HEADERS` — `callbacks.getExternalHeaders()` |
| 111 | +- `angie/context/get-website-context` — host + document metadata |
| 112 | +- `angie/context/get-analytics-context` — screen path + `host.analytics` |
| 113 | +- Host localStorage get/set (for embedded persistence) |
| 114 | + |
| 115 | +## Module map |
| 116 | + |
| 117 | +| File / folder | Role | |
| 118 | +|---------------|------| |
| 119 | +| `boot-sidebar.ts` | Orchestration | |
| 120 | +| `resolve-config.ts` | Merge options, env, presets | |
| 121 | +| `open-embedded-iframe.ts` | Open iframe via shared `iframe.ts` | |
| 122 | +| `embedded-handshake.ts` | Post-open config messages | |
| 123 | +| `shell.ts`, `sidebar-toggle.ts` | Sidebar layout DOM/state | |
| 124 | +| `chat-toggle/` | Floating chat shell and toggle UI | |
| 125 | +| `presets/` | Per-layout defaults | |
| 126 | +| `inject-style-theme.ts` | Optional WordPress theme CSS | |
| 127 | + |
| 128 | +## Tests |
| 129 | + |
| 130 | +Jest specs live next to modules (`*.test.ts`). Run the package test script from the repo root. |
| 131 | + |
| 132 | +## Exports |
| 133 | + |
| 134 | +From `@elementor/angie-sdk`: `loadSidebarV2` on `AngieMcpSdk`, plus `LAYOUT_SIDEBAR`, `LAYOUT_FLOATING_CHAT`, `LoadSidebarV2Options`, `ExternalHeadersCallback`. |
0 commit comments