-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added the WebChatCustomElement component to aid in using web ch…
…at with a custom element (#23)
- Loading branch information
1 parent
a969b54
commit b79a471
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* (C) Copyright IBM Corp. 2022. | ||
* | ||
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/MIT | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
import React, { useCallback, useMemo, useState } from 'react'; | ||
import { WebChatContainer, WebChatContainerProps } from './WebChatContainer'; | ||
import { WebChatInstance } from './types/WebChatInstance'; | ||
|
||
interface WebChatCustomElementProps extends WebChatContainerProps { | ||
/** | ||
* An optional classname that will be added to the custom element. | ||
*/ | ||
className?: string; | ||
|
||
/** | ||
* An optional id that will be added to the custom element. | ||
*/ | ||
id?: string; | ||
|
||
/** | ||
* 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 just adds the classname "HideWebChat" when the main window is closed and removes it when the main | ||
* window is opened. If you use the default, you will also need to add a | ||
* "#WACContainer.WACContainer .HideWebChat { display: none }" rule to your CSS. | ||
* | ||
* 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. | ||
*/ | ||
onViewChange?: (event: any, instance: WebChatInstance) => void; | ||
} | ||
|
||
/** | ||
* This component can be used if you want to render web chat inside a custom element. It will perform two functions: | ||
* | ||
* 1. It will create the custom element as part of the React application. | ||
* 2. It will attach web chat to the custom element and use the WebChatContainer component to manage the life cycle | ||
* of the web chat instance. | ||
*/ | ||
function WebChatCustomElement(props: WebChatCustomElementProps) { | ||
const { className, id, onViewChange, config, onBeforeRender, ...containerProps } = props; | ||
const [customElement, setCustomElement] = useState<HTMLDivElement>(); | ||
|
||
// Make sure to memoize the config object. If we pass a new object to WebChatContainer (even if all the properties | ||
// inside of it are the same), the container will just continually destroy and recreate the web chat instance | ||
// because it thinks the config keeps changing. | ||
const useConfig = useMemo(() => { | ||
return { | ||
...config, | ||
element: customElement, | ||
}; | ||
}, [config, customElement]); | ||
|
||
const onBeforeRenderOverride = useCallback( | ||
async (instance: WebChatInstance) => { | ||
/** | ||
* A default handler for the "view:change" event. This will be used to show or hide the web chat main window | ||
* using a simple classname. | ||
*/ | ||
function defaultViewChangeHandler(event: any, instance: WebChatInstance) { | ||
if (event.newViewState.mainWindow) { | ||
instance.elements.getMainWindow().removeClassName('HideWebChat'); | ||
} else { | ||
instance.elements.getMainWindow().addClassName('HideWebChat'); | ||
} | ||
} | ||
|
||
instance.on({ type: 'view:change', handler: onViewChange || defaultViewChangeHandler }); | ||
|
||
return onBeforeRender?.(instance); | ||
}, | ||
[onBeforeRender, onViewChange], | ||
); | ||
|
||
return ( | ||
<> | ||
<div className={className} id={id} ref={setCustomElement} /> | ||
{customElement && ( | ||
<WebChatContainer config={useConfig} onBeforeRender={onBeforeRenderOverride} {...containerProps} /> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export { WebChatCustomElement }; |