Installation on Angular
This guide shows how to embed the XAPP Chat Widget inside a page of an Angular application, for example on a "Talk to us" page.
For the standard floating chat button on every page, you don't need any of this: add the script snippet to src/index.html as described in Manual Installation.
Try it first on the Embedded Chat Examples page — choose Angular to see the source for different layouts.
How It Works
The chat widget is a React component. A small standalone Angular component creates a React root inside its own element after the view initializes and removes it when the component is destroyed, so the chat works with the Angular router. React is installed alongside your app; lazy-loading the chat page keeps it out of your initial bundle.
Prerequisites
- An Angular 17 or later application using standalone components (Angular CLI), and a developer who can build and deploy it
- Your chat widget key from Studio
- See instructions here
Installation Steps
-
Install the packages
npm install @xapp/chat-widget react react-dom react-redux stentor-models
npm install --save-dev @types/react @types/react-dom -
Add the widget stylesheet — in
angular.json, add it to thestylesarray of your build options:"styles": [
"src/styles.css",
"node_modules/@xapp/chat-widget/dist/index.css"
] -
Add the chat component —
src/app/xapp-chat.tsimport { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core';
import { createElement } from 'react';
import { createRoot, Root } from 'react-dom/client';
import { Chat, WidgetEnv } from '@xapp/chat-widget';
const CHAT_KEY = 'YOUR_CHAT_KEY';
@Component({
selector: 'app-xapp-chat',
template: '<div #container class="xapp-chat"></div>',
styles: ':host, .xapp-chat { display: block; height: 100%; }',
})
export class XappChat implements AfterViewInit, OnDestroy {
@ViewChild('container', { static: true }) private container!: ElementRef<HTMLDivElement>;
private root: Root | null = null;
private destroyed = false;
async ngAfterViewInit(): Promise<void> {
let studioConfig: WidgetEnv;
try {
const res = await fetch(`https://widget.xapp.ai/config.json?key=${CHAT_KEY}`);
if (!res.ok) {
throw new Error(`Chat config request failed: ${res.status}`);
}
studioConfig = await res.json();
} catch (error) {
// A network failure rejects fetch itself, so catch both that and a bad status.
console.error(error);
return;
}
if (this.destroyed) {
return;
}
const config: WidgetEnv = {
...studioConfig,
// The action bar is a floating page element; hide it inside an embedded chat.
actionBar: studioConfig.actionBar && { ...studioConfig.actionBar, enabled: false },
};
this.root = createRoot(this.container.nativeElement);
this.root.render(createElement(Chat, { config, mode: 'docked' }));
}
ngOnDestroy(): void {
this.destroyed = true;
this.root?.unmount();
this.root = null;
}
}Note! Please replace "YOUR_CHAT_KEY" with your actual widget key.
-
Place it in a container with a height — for example
src/app/help-page.tsIn
dockedmode the chat fills the element it is placed in, so give that element a height (and a width if you want it narrower than the column):import { Component } from '@angular/core';
import { XappChat } from './xapp-chat';
@Component({
selector: 'app-help-page',
imports: [XappChat],
template: `
<h1>Talk to us</h1>
<div style="height: 600px; max-width: 420px">
<app-xapp-chat />
</div>
`,
})
export class HelpPage {} -
Lazy-load the page — in
src/app/app.routes.tsimport { Routes } from '@angular/router';
export const routes: Routes = [
// ...your other routes
// Lazy-loaded, so the chat widget and React are only downloaded when this page is opened.
{ path: 'help', loadComponent: () => import('./help-page').then((m) => m.HelpPage) },
];Importing the page eagerly also works, but adds roughly 680 kB to the initial bundle — more than the Angular CLI's default 500 kB budget warning allows.
ng build prints warnings that react, react-dom/client and related modules are not ESM. They are expected and do not affect the chat.
Hide the Chat Header (Optional)
When your page already has its own heading, the chat's title bar repeats it. Add header to the config your component builds:
const config: WidgetEnv = {
...studioConfig,
actionBar: studioConfig.actionBar && { ...studioConfig.actionBar, enabled: false },
header: { ...studioConfig.header, hidden: true },
};
- Requires
@xapp/chat-widget1.103.0 or later. - Applies in
dockedandstaticmode only. A floating (normal) chat always keeps its header, because that is where its minimize and close buttons are. - If your menu button is set to appear in the header, it moves to the footer so the menu stays reachable.
Try it with the Hide header option on the Embedded Chat Examples page.
Verify Installation
- Open the page with the chat and confirm it shows its welcome message inside your container.
- Navigate to another page and back without refreshing — the chat should appear again.
- Check the browser console for errors mentioning the chat widget.
Troubleshooting
- Chat is unstyled: The stylesheet is missing from the
stylesarray inangular.json. Restartng serveafter editingangular.json. - Embedded chat is not visible: Its container has no height. Give the container a fixed or flex height.
- Two chat windows on one page: The script snippet is also installed. Don't combine the floating script snippet with an embedded chat on the same page.
- Bundle budget exceeded: Lazy-load the page that contains the chat, as in step 5.