Standalone chat widget based on the NLX Chat SDK
npm install @nlxai/chat-widgetThe chat widget is a styled, configurable UI widget you can drop in on your website or web application.
The simplest way to talk to a bot is to include an out-of-the-box chat widget on your existing website.
~~~html
In this snippet, the script is deferred in order to reduce impact on page speed. After the script is initialized, we use the nlxai.chatWidget global to instantiate the widget. The following parameters are used:
- config: the bot configuration, including the bot URL obtained and headers obtained from the deployment of your bot.
- titleBar: configuration for the header bar of the widget, containing the following fields:
- title: the text content of the title, e.g. "Support chat".
- logo: the static URL of a logo image that appears left of the title.
You can install the web widget via npm and use a bundler:
npm install --save @nlxai/chat-widget react react-dom
After installation, you can render a chat widget in your document with just a few lines of code:
``jsx
import { create } from "@nlxai/chat-widget";
// This will render the widget as the last element in the
create({
config: {
botUrl: "",
headers: {
"nlx-api-key": "",
},
},
initiallyExpanded: true,
theme: {
primaryColor: "teal",
darkMessageColor: "#000",
lightMessageColor: "#fff",
fontFamily: "Helvetica",
},
});
`
Initiating the chat takes the following parameters (see type definition for details):
The configuration of the chat itself, containing botUrl and request headers. See the core SDK example.
The web widget exposes a number of style theme parameters that can be customized:
- \primaryColor\: the general primary color, used for the header bubble.darkMessageColor\
- \: the background color of the dark message bubbles. As a starting point, we recommend making this identical with the primaryColor, but this is by no means a requirement.lightMessageColor\
- \: the background color of the light message bubbles.white\
- \: the hex code of the general chat background, supporting off-white.fontFamily\
- \: the font setting for the widget.spacing\
- \: the general spacing unit.borderRadius\
- \: border radius for the chat widget.chatWindowMaxHeight\
- \: the maximum height of the chat box, relevant for large screens.
See Theme type definition for additional details.
The URL of an image you can set to override the default chat icon in the chat pin in the lower right corner. PNG and SVG work best.
Renders an optional title bar at the top. If the object is provided, it has the following fields:
- title (mandatory): title text.icon
- (optional): a URL for an icon image.downloadable
- (optional): if set to true, the title bar will include a button that allows chat history to be downloaded.
When set to a non-empty string, a small bubble will appear above the chat pin when the chat is not expanded, helping the user understand what the chat is for. This bubble appears 3s after the chat is loaded, and disappears after 20s.
When this option is set to "localStorage" or "sessionStorage", the state of the chat conversation is persisted in local or session storage respectively. This allows the state and history of the conversation to persist between full page refreshes.
> When using the session storage feature, it is your responsibility to make sure that your website complies with your data protection and privacy policy requirements.
When a bot response is expected, the UI shows a message bubble with a loading animation. By setting a loaderMessage property, a message will appear next to it, by default after a few seconds. This can help long responses seem less frustrating to the user.
Some example strategies:
- inform the user of the delay: Your request is taking longer than expected, please wait.Processing your booking
- inform the user what is happening exactly: .
A duration in milliseconds after which the loaderMessage should appear. If you want the loader message to appear instantly, simply set this value to 0.
The create function (window.nlxChat.widget.create if you are using the packaged version) returns an object that you can use to control the widget programmatically. It has the following methods:
- expand: expand the widget programmatically. You can do this as a response to e.g. a button on your page being clicked.collapse
- : collapse the widget programmatically.teardown
- : remove the chat widget from the page. This cleans up all internal event listeners.getConversationHandler
- : a function that returns the current conversation handler object. Note that this handler might not be available synchronously after widget initialization, and therefore an undefined check is highly recommended before use.
The widget can be configured to handle a number of custom behaviors. Select one from below and see how the code snippet changes:
Simple chat
`html`
defer
src="https://unpkg.com/@nlxai/chat-widget/lib/index.umd.js"
>
Send welcome intent when the chat is opened
~~~html
Send custom intent after a period of inactivity
~~~html
Initialize conversation with context
~~~html
Retain conversation through refreshes (SessionStorage)
~~~html
Retain conversation through refreshes and closed browser sessions (LocalStorage)
~~~html
The chat widget supports fully custom embeddable components to augment the out-of-the-box chat bubbles and choice buttons. Embeddable components represent the best-of-both-worlds combination of a fully built widget and one custom-engineered from the ground up.
As the widget is built in React, it exposes the React instance that allows the user to define embeddable components that not only support fully custom styling but can interact with the conversation in a granular way. This component is included in the customModalities field of the widget configuration, and is rendered whenever the modality by the same key is triggered:
~~~html
MIT.