npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

super-great-testing-chat-temp

v0.1.41

Published

Be sure to review the [chat documentation](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?target=carbon).

Downloads

2,176

Readme

Carbon for watsonx web chat with React

Be sure to review the chat documentation.

Installation

To install using npm:

npm install super-great-testing-chat-temp

Or using yarn:

yarn add super-great-testing-chat-temp

Using ChatContainer

Basic example

The WebChatContainer function component is intended to make it as easy as possible to include web chat in your React application. To use, you simply need to render this component anywhere in your application and provide the web chat configuration options object as a prop.

import React from 'react';
import { ChatContainer } from 'super-great-testing-chat-temp';

const webChatOptions = {
  // Your configuration object.
};

function App() {
  return <ChatContainer config={webChatOptions} />;
}

Props

ChatContainer has the following props.

| Attribute | Required | Type | Description | |-----------|----------|---------|-------------| | config | Yes | object | The chat configuration options object. Note that any onLoad property will be ignored. If this prop is changed and a new object provided, then the current web chat will be destroyed and a new one created with the new object. | | instanceRef | No | MutableRefObject | A convenience prop that is a reference to the web chat instance. This component will set the value of this ref using the current property when the instance has been created. | | onBeforeRender | No | function | This is a callback function that is called after web chat has been loaded and before the render function is called. This function is passed a single argument which is the instance of web chat that was loaded. This function can be used to obtain a reference to the web chat instance if you want to make use of the instance methods that are available. | | onAfterRender | No | function | This is a callback function that is called after web chat has been loaded and after the render function is called. This function is passed a single argument which is the instance of web chat that was loaded. This function can be used to obtain a reference to the web chat instance if you want to make use of the instance methods that are available. | | renderUserDefinedResponse | No | function | This function is a callback function that will be called by this container to render user defined responses. If this prop is provided, then the container will listen for user defined response events from web chat and will generate a React portal for each event. This function will be called once during component render for each user defined response event. This function takes two arguments. The first is the user defined response event that triggered the user defined response. The second is a convenience argument that is the instance of web chat. The function should return a ReactNode that renders the user defined content for the response. |

Using ChatCustomElement

This library provides the component ChatCustomElement which can be used to aid in rendering web chat inside a custom element. This is needed if you want to be able to change the location where web chat is rendered. This component will render an element in your React app and use that element as the custom element for rendering web chat.

The default behavior of this component will add and remove a classname from the web chat main window as well as your custom element to control the visibility of web chat when it is opened or closed. It will also inject a style tag into your application to define the rules for these classnames. When web chat is closed, a classname will be added to the web chat main window to hide the element and a classname will be added to your custom element to set its width and height to 0 so it doesn't take up space. Note that the custom element should remain visible if you want to use the built-in web chat launcher which is also contained in your custom element.

If you don't want these behaviors, then provide your own onViewChange prop to ChatCustomElement and provide your own logic for controlling the visibility of web chat. If you want custom animations when web chat is opened and closed, this would be the mechanism to do that.

The simplest example is this:

import React from 'react';
import { ChatCustomElement } from 'super-great-testing-chat-temp';

import './App.css';

const webChatOptions = { /* Web chat options */ };

function App() {
  return <ChatCustomElement className="MyCustomElement" config={webChatOptions} />;
}
.MyCustomElement {
  position: absolute;
  left: 100px;
  top: 100px;
  width: 500px;
  height: 500px;
}

Props

ChatCustomElement inherits all of the props from ChatContainer. It also has the following additional optional props.

| Attribute | Type | Description | |-----------|---------|-------------| | className | string | An optional classname that will be added to the custom element. | | id | string | An optional id that will be added to the custom element. | | onViewChange | function | An optional listener for "view:change" events. Such a listener is required when using a custom element in order to control the visibility of the web chat main window. If no callback is provided here, a default one will be used that uses some classnames to control web chat and your custom element. You can provide a different callback here if you want custom behavior such as an animation when the main window is opened or closed. Note that this function can only be provided before web chat is loaded. After web chat is loaded, the event handler will not be updated. |

Accessing instance methods

You can use the onBeforeRender or onAfterRender props to get access to the instance of web chat if you need call instance methods later. This example renders a button that toggles web chat open and is only rendered after the instance has become available.

import React, { useCallback, useState } from 'react';
import { ChatContainer } from 'super-great-testing-chat-temp';

const webChatOptions = {
  // Your configuration object.
};

function App() {
  const [instance, setInstance] = useState(null);

  const toggleWebChat = useCallback(() => {
    instance.toggleOpen();
  }, [instance]);

  return (
    <>
      {instance && (
        <button type="button" onClick={toggleWebChat}>
          Toggle web chat
        </button>
      )}
      <ChatContainer config={webChatOptions} onBeforeRender={(instance) => onBeforeRender(instance, setInstance)} />
    </>
  );
}

function onBeforeRender(instance, setInstance) {
  // Make the instance available to the React components.
  setInstance(instance);

  // Do any other work you might want to do before rendering. If you don't need any other work here, you can just use
  // onBeforeRender={setInstance} in the component above.
}

User defined responses

This component is also capable of managing user defined responses. To do so, you need to pass a renderUserDefinedResponse function as a render prop. This function should return a React component that will render the content for the specific message for that response. You should make sure that the ChatContainer component does not get unmounted in the middle of the life of your application because it will lose all user defined responses that were previously received by web chat.

You should treat the renderUserDefinedResponse prop like any typical React render prop; it is different from the userDefinedResponse event or a typical event handler. The event is fired only once when web chat initially receives the response from the server. The renderUserDefinedResponse prop however is called every time the App re-renders and it should return an up-to-date React component for the provided message item just like the render function would for a typical React component.

import React from 'react';
import { ChatContainer } from 'super-great-testing-chat-temp';

const webChatOptions = {
  // Your configuration object.
};

function App() {
  return <ChatContainer renderUserDefinedResponse={renderUserDefinedResponse} config={webChatOptions} />;
}

function renderUserDefinedResponse(event) {
  // The event here will contain details for each user defined response that needs to be rendered.
  // The "user_defined_type" property is just an example; it is not required. You can use any other property or
  // condition you want here. This makes it easier to handle different response types if you have more than
  // one user defined response type.
  if (event.data.message.user_defined && event.data.message.user_defined.user_defined_type === 'my-custom-type') {
    return <div>My custom content</div> // This can be any full React component.
  }
}