@yext/chat-ui-react 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -2
- package/THIRD-PARTY-NOTICES +6 -6
- package/lib/bundle-no-resets.css +1 -0
- package/lib/bundle.css +1 -1
- package/lib/commonjs/components/ChatHeader.d.ts +1 -1
- package/lib/commonjs/components/ChatHeader.d.ts.map +1 -1
- package/lib/commonjs/components/ChatHeader.js +6 -5
- package/lib/commonjs/components/ChatHeader.js.map +1 -1
- package/lib/commonjs/components/ChatInput.d.ts +2 -3
- package/lib/commonjs/components/ChatInput.d.ts.map +1 -1
- package/lib/commonjs/components/ChatInput.js +8 -6
- package/lib/commonjs/components/ChatInput.js.map +1 -1
- package/lib/commonjs/components/ChatPanel.d.ts.map +1 -1
- package/lib/commonjs/components/ChatPanel.js +8 -6
- package/lib/commonjs/components/ChatPanel.js.map +1 -1
- package/lib/commonjs/components/ChatPopUp.d.ts.map +1 -1
- package/lib/commonjs/components/ChatPopUp.js +7 -5
- package/lib/commonjs/components/ChatPopUp.js.map +1 -1
- package/lib/commonjs/components/LoadingDots.d.ts.map +1 -1
- package/lib/commonjs/components/LoadingDots.js.map +1 -1
- package/lib/commonjs/components/Markdown.js +1 -1
- package/lib/commonjs/components/Markdown.js.map +1 -1
- package/lib/commonjs/components/MessageBubble.d.ts.map +1 -1
- package/lib/commonjs/components/MessageBubble.js +5 -2
- package/lib/commonjs/components/MessageBubble.js.map +1 -1
- package/lib/commonjs/hooks/useDefaultHandleApiError.d.ts +8 -0
- package/lib/commonjs/hooks/useDefaultHandleApiError.d.ts.map +1 -0
- package/lib/commonjs/hooks/useDefaultHandleApiError.js +28 -0
- package/lib/commonjs/hooks/useDefaultHandleApiError.js.map +1 -0
- package/lib/commonjs/icons/Arrow.d.ts.map +1 -1
- package/lib/commonjs/icons/Arrow.js +1 -1
- package/lib/commonjs/icons/Arrow.js.map +1 -1
- package/lib/commonjs/icons/Cross.d.ts.map +1 -1
- package/lib/commonjs/icons/Cross.js.map +1 -1
- package/lib/commonjs/icons/DualSync.d.ts.map +1 -1
- package/lib/commonjs/icons/DualSync.js +1 -1
- package/lib/commonjs/icons/DualSync.js.map +1 -1
- package/lib/commonjs/icons/Message.d.ts.map +1 -1
- package/lib/commonjs/icons/Message.js +1 -1
- package/lib/commonjs/icons/Message.js.map +1 -1
- package/lib/commonjs/utils/withStylelessCssClasses.d.ts +17 -0
- package/lib/commonjs/utils/withStylelessCssClasses.d.ts.map +1 -0
- package/lib/commonjs/utils/withStylelessCssClasses.js +28 -0
- package/lib/commonjs/utils/withStylelessCssClasses.js.map +1 -0
- package/lib/esm/components/ChatHeader.d.ts +1 -1
- package/lib/esm/components/ChatHeader.d.ts.map +1 -1
- package/lib/esm/components/ChatHeader.js +6 -5
- package/lib/esm/components/ChatHeader.js.map +1 -1
- package/lib/esm/components/ChatInput.d.ts +2 -3
- package/lib/esm/components/ChatInput.d.ts.map +1 -1
- package/lib/esm/components/ChatInput.js +7 -5
- package/lib/esm/components/ChatInput.js.map +1 -1
- package/lib/esm/components/ChatPanel.d.ts.map +1 -1
- package/lib/esm/components/ChatPanel.js +7 -5
- package/lib/esm/components/ChatPanel.js.map +1 -1
- package/lib/esm/components/ChatPopUp.d.ts.map +1 -1
- package/lib/esm/components/ChatPopUp.js +9 -7
- package/lib/esm/components/ChatPopUp.js.map +1 -1
- package/lib/esm/components/LoadingDots.d.ts.map +1 -1
- package/lib/esm/components/LoadingDots.js.map +1 -1
- package/lib/esm/components/Markdown.js +1 -1
- package/lib/esm/components/Markdown.js.map +1 -1
- package/lib/esm/components/MessageBubble.d.ts.map +1 -1
- package/lib/esm/components/MessageBubble.js +5 -2
- package/lib/esm/components/MessageBubble.js.map +1 -1
- package/lib/esm/hooks/useDefaultHandleApiError.d.ts +8 -0
- package/lib/esm/hooks/useDefaultHandleApiError.d.ts.map +1 -0
- package/lib/esm/hooks/useDefaultHandleApiError.js +24 -0
- package/lib/esm/hooks/useDefaultHandleApiError.js.map +1 -0
- package/lib/esm/icons/Arrow.d.ts.map +1 -1
- package/lib/esm/icons/Arrow.js +1 -1
- package/lib/esm/icons/Arrow.js.map +1 -1
- package/lib/esm/icons/Cross.d.ts.map +1 -1
- package/lib/esm/icons/Cross.js.map +1 -1
- package/lib/esm/icons/DualSync.d.ts.map +1 -1
- package/lib/esm/icons/DualSync.js +1 -1
- package/lib/esm/icons/DualSync.js.map +1 -1
- package/lib/esm/icons/Message.d.ts.map +1 -1
- package/lib/esm/icons/Message.js +1 -1
- package/lib/esm/icons/Message.js.map +1 -1
- package/lib/esm/index.d.ts +3 -4
- package/lib/esm/utils/withStylelessCssClasses.d.ts +17 -0
- package/lib/esm/utils/withStylelessCssClasses.d.ts.map +1 -0
- package/lib/esm/utils/withStylelessCssClasses.js +24 -0
- package/lib/esm/utils/withStylelessCssClasses.js.map +1 -0
- package/package.json +5 -4
- package/src/components/ChatHeader.tsx +14 -13
- package/src/components/ChatInput.tsx +9 -8
- package/src/components/ChatPanel.tsx +8 -6
- package/src/components/ChatPopUp.tsx +35 -25
- package/src/components/LoadingDots.tsx +1 -4
- package/src/components/Markdown.tsx +3 -3
- package/src/components/MessageBubble.tsx +5 -2
- package/src/hooks/useDefaultHandleApiError.ts +32 -0
- package/src/icons/Arrow.tsx +12 -4
- package/src/icons/Cross.tsx +10 -4
- package/src/icons/DualSync.tsx +11 -4
- package/src/icons/Message.tsx +9 -3
- package/src/utils/withStylelessCssClasses.ts +28 -0
- package/lib/commonjs/utils/defaultHandleError.d.ts +0 -9
- package/lib/commonjs/utils/defaultHandleError.d.ts.map +0 -1
- package/lib/commonjs/utils/defaultHandleError.js +0 -15
- package/lib/commonjs/utils/defaultHandleError.js.map +0 -1
- package/lib/esm/utils/defaultHandleError.d.ts +0 -9
- package/lib/esm/utils/defaultHandleError.d.ts.map +0 -1
- package/lib/esm/utils/defaultHandleError.js +0 -11
- package/lib/esm/utils/defaultHandleError.js.map +0 -1
- package/src/utils/defaultHandleError.ts +0 -10
package/lib/esm/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export declare function ChatHeader({ title, showRestartButton, restartButtonIcon
|
|
|
19
19
|
* @public
|
|
20
20
|
*/
|
|
21
21
|
export declare interface ChatHeaderCssClasses {
|
|
22
|
-
|
|
22
|
+
container?: string;
|
|
23
23
|
title?: string;
|
|
24
24
|
restartButton?: string;
|
|
25
25
|
closeButton?: string;
|
|
@@ -93,9 +93,8 @@ export declare interface ChatInputProps {
|
|
|
93
93
|
/** Enable auto focus for the input box. Defaults to false. */
|
|
94
94
|
inputAutoFocus?: boolean;
|
|
95
95
|
/**
|
|
96
|
-
* A function which is called when an error occurs from
|
|
97
|
-
*
|
|
98
|
-
* By default, the error is logged to the console.
|
|
96
|
+
* A function which is called when an error occurs from Chat API while processing the user's message.
|
|
97
|
+
* By default, the error is logged to the console and an error message is added to state.
|
|
99
98
|
*/
|
|
100
99
|
handleError?: (e: unknown) => void;
|
|
101
100
|
/** Custom icon for the send button. */
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append styleless css classnames that user can target to add custom styling to components.
|
|
3
|
+
* The classes follows a format of "yext-chat-component-name__css-interface-field-name".
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* the styleless css class name for {@link MessageBubble}'s "message__bot" field defined
|
|
7
|
+
* in its {@link MessageBubbleCssClasses} interface is "yext-chat-message-bubble__message__bot"
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*
|
|
11
|
+
* @param componentName - the component name
|
|
12
|
+
* @param builtInClasses - built in css classnames of the component
|
|
13
|
+
*
|
|
14
|
+
* @returns builtInClasses with styleless css classnames
|
|
15
|
+
*/
|
|
16
|
+
export declare function withStylelessCssClasses<ClassInterface extends Partial<Record<keyof ClassInterface, string | object>>>(componentName: string, builtInClasses: Readonly<ClassInterface>): ClassInterface;
|
|
17
|
+
//# sourceMappingURL=withStylelessCssClasses.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withStylelessCssClasses.d.ts","sourceRoot":"","sources":["../../../src/utils/withStylelessCssClasses.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,uBAAuB,CACrC,cAAc,SAAS,OAAO,CAAC,MAAM,CAAC,MAAM,cAAc,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,EAE7E,aAAa,EAAE,MAAM,EACrB,cAAc,EAAE,QAAQ,CAAC,cAAc,CAAC,GACvC,cAAc,CAOhB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append styleless css classnames that user can target to add custom styling to components.
|
|
3
|
+
* The classes follows a format of "yext-chat-component-name__css-interface-field-name".
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* the styleless css class name for {@link MessageBubble}'s "message__bot" field defined
|
|
7
|
+
* in its {@link MessageBubbleCssClasses} interface is "yext-chat-message-bubble__message__bot"
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*
|
|
11
|
+
* @param componentName - the component name
|
|
12
|
+
* @param builtInClasses - built in css classnames of the component
|
|
13
|
+
*
|
|
14
|
+
* @returns builtInClasses with styleless css classnames
|
|
15
|
+
*/
|
|
16
|
+
export function withStylelessCssClasses(componentName, builtInClasses) {
|
|
17
|
+
const formatString = (str) => str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
18
|
+
const classes = { ...builtInClasses };
|
|
19
|
+
Object.keys(builtInClasses).forEach((key) => {
|
|
20
|
+
classes[key] = `${classes[key]} yext-chat${formatString(componentName)}__${formatString(key)}`;
|
|
21
|
+
});
|
|
22
|
+
return classes;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=withStylelessCssClasses.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withStylelessCssClasses.js","sourceRoot":"","sources":["../../../src/utils/withStylelessCssClasses.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,uBAAuB,CAGrC,aAAqB,EACrB,cAAwC;IAExC,MAAM,YAAY,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;IAClG,MAAM,OAAO,GAAG,EAAE,GAAG,cAAc,EAAE,CAAA;IACrC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QAC1C,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,YAAY,CAAC,aAAa,CAAC,KAAK,YAAY,CAAC,GAAG,CAAC,EAAE,CAAA;IAChG,CAAC,CAAC,CAAA;IACF,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yext/chat-ui-react",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A library of React Components for powering Yext Chat integrations.",
|
|
5
5
|
"author": "clippy@yext.com",
|
|
6
6
|
"main": "./lib/commonjs/index.js",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"import": "./lib/esm/index.js",
|
|
13
13
|
"require": "./lib/commonjs/index.js"
|
|
14
14
|
},
|
|
15
|
-
"./bundle.css": "./lib/bundle.css"
|
|
15
|
+
"./bundle.css": "./lib/bundle.css",
|
|
16
|
+
"./bundle-no-resets.css": "./lib/bundle-no-resets.css"
|
|
16
17
|
},
|
|
17
18
|
"license": "BSD-3-Clause",
|
|
18
19
|
"files": [
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"generate-docs": "api-extractor run --local --verbose && api-documenter markdown --input-folder temp --output-folder docs && rm -rf temp",
|
|
38
39
|
"generate-notices": "generate-license-file --input package.json --output ./THIRD-PARTY-NOTICES --overwrite",
|
|
39
40
|
"build:js": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json",
|
|
40
|
-
"build:css": "tailwindcss -o ./lib/bundle.css --minify -c tailwind.config.js",
|
|
41
|
+
"build:css": "tailwindcss -o ./lib/bundle.css --minify -c tailwind.config.js && tailwindcss -o ./lib/bundle-no-resets.css --minify -c tailwind-no-resets.config.js",
|
|
41
42
|
"build": "rm -rf lib/** && npm run build:js && npm run build:css && npm run generate-docs && npm run generate-notices",
|
|
42
43
|
"build-storybook": "storybook build"
|
|
43
44
|
},
|
|
@@ -79,7 +80,7 @@
|
|
|
79
80
|
"typescript": "^5.0.4"
|
|
80
81
|
},
|
|
81
82
|
"peerDependencies": {
|
|
82
|
-
"@yext/chat-headless-react": "^0.3.
|
|
83
|
+
"@yext/chat-headless-react": "^0.3.2",
|
|
83
84
|
"react": "^16.14 || ^17 || ^18",
|
|
84
85
|
"react-dom": "^16.14 || ^17 || || ^18"
|
|
85
86
|
},
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { useChatActions } from "@yext/chat-headless-react";
|
|
2
|
-
import { DualSyncIcon } from "../icons/DualSync"
|
|
2
|
+
import { DualSyncIcon } from "../icons/DualSync";
|
|
3
3
|
import { useComposedCssClasses } from "../hooks/useComposedCssClasses";
|
|
4
4
|
import { useCallback, useRef, useState } from "react";
|
|
5
5
|
import { twMerge } from "tailwind-merge";
|
|
6
6
|
import { CrossIcon } from "../icons/Cross";
|
|
7
|
+
import { withStylelessCssClasses } from "../utils/withStylelessCssClasses";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* The CSS class interface for the {@link ChatHeader} component.
|
|
@@ -11,19 +12,19 @@ import { CrossIcon } from "../icons/Cross";
|
|
|
11
12
|
* @public
|
|
12
13
|
*/
|
|
13
14
|
export interface ChatHeaderCssClasses {
|
|
14
|
-
|
|
15
|
+
container?: string;
|
|
15
16
|
title?: string;
|
|
16
17
|
restartButton?: string;
|
|
17
18
|
closeButton?: string;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
const builtInCssClasses: Readonly<ChatHeaderCssClasses> = {
|
|
21
|
-
|
|
21
|
+
const builtInCssClasses: Readonly<ChatHeaderCssClasses> = withStylelessCssClasses('Header', {
|
|
22
|
+
container:
|
|
22
23
|
"w-full px-4 py-3 flex justify-between bg-gradient-to-tr from-blue-600 to-blue-800 rounded-t-3xl",
|
|
23
24
|
title: "text-white text-xl font-medium",
|
|
24
25
|
restartButton: "w-8 text-white stroke-[0.2] ml-auto",
|
|
25
|
-
closeButton: "w-8 text-white hover:scale-110"
|
|
26
|
-
};
|
|
26
|
+
closeButton: "w-8 text-white hover:scale-110",
|
|
27
|
+
});
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* The props for the {@link ChatHeader} component.
|
|
@@ -93,7 +94,7 @@ export function ChatHeader({
|
|
|
93
94
|
}, [chat]);
|
|
94
95
|
|
|
95
96
|
return (
|
|
96
|
-
<div className={cssClasses.
|
|
97
|
+
<div className={cssClasses.container}>
|
|
97
98
|
<h1 className={cssClasses.title}>{title}</h1>
|
|
98
99
|
{showRestartButton && (
|
|
99
100
|
<button
|
|
@@ -106,12 +107,12 @@ export function ChatHeader({
|
|
|
106
107
|
)}
|
|
107
108
|
{showCloseButton && (
|
|
108
109
|
<button
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
110
|
+
aria-label="Close Chat"
|
|
111
|
+
onClick={onClose}
|
|
112
|
+
className={cssClasses.closeButton}
|
|
113
|
+
>
|
|
114
|
+
{closeButtonIcon}
|
|
115
|
+
</button>
|
|
115
116
|
)}
|
|
116
117
|
</div>
|
|
117
118
|
);
|
|
@@ -4,7 +4,8 @@ import { ArrowIcon } from "../icons/Arrow";
|
|
|
4
4
|
import { useComposedCssClasses } from "../hooks";
|
|
5
5
|
import Textarea from "react-expanding-textarea";
|
|
6
6
|
import { twMerge } from "tailwind-merge";
|
|
7
|
-
import {
|
|
7
|
+
import { useDefaultHandleApiError } from "../hooks/useDefaultHandleApiError";
|
|
8
|
+
import { withStylelessCssClasses } from "../utils/withStylelessCssClasses";
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* The CSS class interface for the {@link ChatInput} component.
|
|
@@ -17,13 +18,13 @@ export interface ChatInputCssClasses {
|
|
|
17
18
|
sendButton?: string;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
const builtInCssClasses: ChatInputCssClasses = {
|
|
21
|
+
const builtInCssClasses: ChatInputCssClasses = withStylelessCssClasses('Input', {
|
|
21
22
|
container: "w-full h-fit flex flex-row relative @container",
|
|
22
23
|
textArea:
|
|
23
24
|
"w-full p-4 pr-10 border border-slate-300 disabled:bg-slate-50 rounded-3xl resize-none text-[13px] @[480px]:text-base",
|
|
24
25
|
sendButton:
|
|
25
|
-
"rounded-full p-1.5 w-8 stroke-2 text-white bg-blue-600 disabled:bg-slate-100 hover:bg-blue-800 active:scale-90 transition-all absolute right-7 bottom-2.5 @[480px]:bottom-3.5",
|
|
26
|
-
};
|
|
26
|
+
"rounded-full p-1.5 w-8 h-8 stroke-2 text-white bg-blue-600 disabled:bg-slate-100 hover:bg-blue-800 active:scale-90 transition-all absolute right-7 bottom-2.5 @[480px]:bottom-3.5",
|
|
27
|
+
});
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* The props for the {@link ChatInput} component.
|
|
@@ -44,9 +45,8 @@ export interface ChatInputProps {
|
|
|
44
45
|
/** Enable auto focus for the input box. Defaults to false. */
|
|
45
46
|
inputAutoFocus?: boolean;
|
|
46
47
|
/**
|
|
47
|
-
* A function which is called when an error occurs from
|
|
48
|
-
*
|
|
49
|
-
* By default, the error is logged to the console.
|
|
48
|
+
* A function which is called when an error occurs from Chat API while processing the user's message.
|
|
49
|
+
* By default, the error is logged to the console and an error message is added to state.
|
|
50
50
|
*/
|
|
51
51
|
handleError?: (e: unknown) => void;
|
|
52
52
|
/** Custom icon for the send button. */
|
|
@@ -74,6 +74,7 @@ export function ChatInput({
|
|
|
74
74
|
const canSendMessage = useChatState(
|
|
75
75
|
(state) => state.conversation.canSendMessage
|
|
76
76
|
);
|
|
77
|
+
const defaultHandleApiError = useDefaultHandleApiError();
|
|
77
78
|
|
|
78
79
|
const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
|
|
79
80
|
const sendButtonClassNames = twMerge(
|
|
@@ -87,7 +88,7 @@ export function ChatInput({
|
|
|
87
88
|
: chat.getNextMessage(input);
|
|
88
89
|
setInput("");
|
|
89
90
|
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
|
|
90
|
-
}, [chat, input, handleError, stream]);
|
|
91
|
+
}, [chat, input, handleError, defaultHandleApiError, stream]);
|
|
91
92
|
|
|
92
93
|
const handleKeyDown = useCallback(
|
|
93
94
|
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
|
@@ -8,7 +8,8 @@ import {
|
|
|
8
8
|
import { ChatInput, ChatInputCssClasses, ChatInputProps } from "./ChatInput";
|
|
9
9
|
import { LoadingDots } from "./LoadingDots";
|
|
10
10
|
import { useComposedCssClasses } from "../hooks";
|
|
11
|
-
import {
|
|
11
|
+
import { useDefaultHandleApiError } from "../hooks/useDefaultHandleApiError";
|
|
12
|
+
import { withStylelessCssClasses } from "../utils/withStylelessCssClasses";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* The CSS class interface for the {@link ChatPanel} component.
|
|
@@ -23,13 +24,13 @@ export interface ChatPanelCssClasses {
|
|
|
23
24
|
messageBubbleCssClasses?: MessageBubbleCssClasses;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
const builtInCssClasses: ChatPanelCssClasses = {
|
|
27
|
+
const builtInCssClasses: ChatPanelCssClasses = withStylelessCssClasses('Panel', {
|
|
27
28
|
container:
|
|
28
29
|
"h-full w-full flex flex-col relative rounded-3xl shadow-2xl bg-white",
|
|
29
30
|
messagesContainer:
|
|
30
31
|
"flex flex-col gap-y-1 mt-auto px-4 pb-[85px] overflow-auto",
|
|
31
32
|
inputContainer: "w-full absolute bottom-0 p-4 rounded-b-3xl backdrop-blur-lg",
|
|
32
|
-
};
|
|
33
|
+
});
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* The props for the {@link ChatPanel} component.
|
|
@@ -65,6 +66,7 @@ export function ChatPanel(props: ChatPanelProps) {
|
|
|
65
66
|
(state) => state.conversation.canSendMessage
|
|
66
67
|
);
|
|
67
68
|
const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
|
|
69
|
+
const defaultHandleApiError = useDefaultHandleApiError();
|
|
68
70
|
|
|
69
71
|
// Fetch the first message on load, if there are no existing messages or a request being processed
|
|
70
72
|
useEffect(() => {
|
|
@@ -74,7 +76,7 @@ export function ChatPanel(props: ChatPanelProps) {
|
|
|
74
76
|
const { stream = true, handleError } = props;
|
|
75
77
|
const res = stream ? chat.streamNextMessage() : chat.getNextMessage();
|
|
76
78
|
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
|
|
77
|
-
}, [chat, props, messages, canSendMessage]);
|
|
79
|
+
}, [chat, props, messages, defaultHandleApiError, canSendMessage]);
|
|
78
80
|
|
|
79
81
|
const messagesRef = useRef<HTMLDivElement>(null);
|
|
80
82
|
|
|
@@ -82,8 +84,8 @@ export function ChatPanel(props: ChatPanelProps) {
|
|
|
82
84
|
useEffect(() => {
|
|
83
85
|
messagesRef.current?.scroll({
|
|
84
86
|
top: messagesRef.current?.scrollHeight,
|
|
85
|
-
behavior:
|
|
86
|
-
|
|
87
|
+
behavior: "smooth",
|
|
88
|
+
});
|
|
87
89
|
}, [messages]);
|
|
88
90
|
|
|
89
91
|
return (
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { useCallback, useState } from "react";
|
|
2
|
-
import { MessageIcon } from
|
|
2
|
+
import { MessageIcon } from "../icons/Message";
|
|
3
3
|
import { ChatPanel, ChatPanelCssClasses, ChatPanelProps } from "./ChatPanel";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
ChatHeader,
|
|
6
|
+
ChatHeaderCssClasses,
|
|
7
|
+
ChatHeaderProps,
|
|
8
|
+
} from "./ChatHeader";
|
|
5
9
|
import { twMerge } from "tailwind-merge";
|
|
6
10
|
import { useComposedCssClasses } from "../hooks";
|
|
11
|
+
import { withStylelessCssClasses } from "../utils/withStylelessCssClasses";
|
|
7
12
|
|
|
8
13
|
/**
|
|
9
14
|
* The CSS class interface for the {@link ChatPopUp} component.
|
|
@@ -18,30 +23,32 @@ export interface ChatPopUpCssClasses {
|
|
|
18
23
|
button?: string;
|
|
19
24
|
button__display?: string;
|
|
20
25
|
button__hidden?: string;
|
|
21
|
-
headerCssClasses?: ChatHeaderCssClasses
|
|
22
|
-
panelCssClasses?: ChatPanelCssClasses
|
|
26
|
+
headerCssClasses?: ChatHeaderCssClasses;
|
|
27
|
+
panelCssClasses?: ChatPanelCssClasses;
|
|
23
28
|
}
|
|
24
29
|
|
|
25
|
-
const fixedPosition = "fixed bottom-6 right-4 lg:bottom-14 lg:right-10 "
|
|
26
|
-
const builtInCssClasses: ChatPopUpCssClasses = {
|
|
30
|
+
const fixedPosition = "fixed bottom-6 right-4 lg:bottom-14 lg:right-10 z-50 ";
|
|
31
|
+
const builtInCssClasses: ChatPopUpCssClasses = withStylelessCssClasses('PopUp', {
|
|
27
32
|
container: "transition-all",
|
|
28
33
|
panel: fixedPosition + "w-80 lg:w-96 h-[75vh]",
|
|
29
34
|
panel__display: "duration-300 translate-y-0",
|
|
30
35
|
panel__hidden: "duration-300 translate-y-[20%] opacity-0 invisible",
|
|
31
|
-
button:
|
|
36
|
+
button:
|
|
37
|
+
fixedPosition +
|
|
38
|
+
"p-2 w-12 h-12 lg:w-16 lg:h-16 flex justify-center items-center text-white shadow-xl rounded-full bg-gradient-to-br from-blue-600 to-blue-700 hover:-translate-y-2 duration-150",
|
|
32
39
|
button__display: "duration-300 transform translate-y-0",
|
|
33
|
-
button__hidden:
|
|
34
|
-
|
|
40
|
+
button__hidden:
|
|
41
|
+
"duration-300 transform translate-y-[20%] opacity-0 invisible",
|
|
42
|
+
});
|
|
35
43
|
|
|
36
44
|
/**
|
|
37
45
|
* The props for the {@link ChatPopUp} component.
|
|
38
46
|
*
|
|
39
47
|
* @public
|
|
40
48
|
*/
|
|
41
|
-
export interface ChatPopUpProps
|
|
42
|
-
Omit<ChatHeaderProps, "showCloseButton" | "customCssClasses">,
|
|
43
|
-
|
|
44
|
-
{
|
|
49
|
+
export interface ChatPopUpProps
|
|
50
|
+
extends Omit<ChatHeaderProps, "showCloseButton" | "customCssClasses">,
|
|
51
|
+
Omit<ChatPanelProps, "header" | "customCssClasses"> {
|
|
45
52
|
/** Custom icon for the popup button to open the panel. */
|
|
46
53
|
openPanelButtonIcon?: JSX.Element;
|
|
47
54
|
/**
|
|
@@ -63,9 +70,9 @@ export function ChatPopUp(props: ChatPopUpProps) {
|
|
|
63
70
|
openPanelButtonIcon = <MessageIcon />,
|
|
64
71
|
customCssClasses,
|
|
65
72
|
showRestartButton = true,
|
|
66
|
-
onClose:customOnClose,
|
|
67
|
-
title
|
|
68
|
-
} = props
|
|
73
|
+
onClose: customOnClose,
|
|
74
|
+
title,
|
|
75
|
+
} = props;
|
|
69
76
|
const [showChat, setShowChat] = useState(false);
|
|
70
77
|
const onClick = useCallback(() => {
|
|
71
78
|
setShowChat(!showChat);
|
|
@@ -74,8 +81,8 @@ export function ChatPopUp(props: ChatPopUpProps) {
|
|
|
74
81
|
const onClose = useCallback(() => {
|
|
75
82
|
setShowChat(false);
|
|
76
83
|
customOnClose?.();
|
|
77
|
-
}, [customOnClose])
|
|
78
|
-
|
|
84
|
+
}, [customOnClose]);
|
|
85
|
+
|
|
79
86
|
const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
|
|
80
87
|
const panelCssClasses = twMerge(
|
|
81
88
|
cssClasses.panel,
|
|
@@ -92,13 +99,16 @@ export function ChatPopUp(props: ChatPopUpProps) {
|
|
|
92
99
|
<ChatPanel
|
|
93
100
|
{...props}
|
|
94
101
|
customCssClasses={cssClasses.panelCssClasses}
|
|
95
|
-
header={
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
header={
|
|
103
|
+
<ChatHeader
|
|
104
|
+
title={title}
|
|
105
|
+
showRestartButton={showRestartButton}
|
|
106
|
+
showCloseButton={true}
|
|
107
|
+
onClose={onClose}
|
|
108
|
+
customCssClasses={cssClasses.headerCssClasses}
|
|
109
|
+
/>
|
|
110
|
+
}
|
|
111
|
+
/>
|
|
102
112
|
</div>
|
|
103
113
|
<button
|
|
104
114
|
aria-label="Chat Popup Button"
|
|
@@ -9,10 +9,7 @@ export function LoadingDots({ className }: { className?: string }) {
|
|
|
9
9
|
return (
|
|
10
10
|
<div
|
|
11
11
|
aria-label="Loading Indicator"
|
|
12
|
-
className={twMerge(
|
|
13
|
-
"flex gap-1 p-2 animate-fade-in",
|
|
14
|
-
className
|
|
15
|
-
)}
|
|
12
|
+
className={twMerge("flex gap-1 p-2 animate-fade-in", className)}
|
|
16
13
|
>
|
|
17
14
|
<div className="w-2 h-2 bg-slate-500 rounded-full animate-[bounce_1s_infinite]" />
|
|
18
15
|
<div className="w-2 h-2 bg-slate-500 rounded-full animate-[bounce_1s_infinite_0.3s]" />
|
|
@@ -6,7 +6,7 @@ import rehypeSanitize from "rehype-sanitize";
|
|
|
6
6
|
// The Remark and Rehype plugins to use in conjunction with ReactMarkdown.
|
|
7
7
|
const unifiedPlugins: { remark?: PluggableList; rehype: PluggableList } = {
|
|
8
8
|
remark: [
|
|
9
|
-
remarkGfm //renders Github-Flavored Markdown
|
|
9
|
+
remarkGfm, //renders Github-Flavored Markdown
|
|
10
10
|
],
|
|
11
11
|
rehype: [
|
|
12
12
|
rehypeRaw, //to support HTML embedded in markdown
|
|
@@ -22,7 +22,7 @@ interface MarkdownProps {
|
|
|
22
22
|
/**
|
|
23
23
|
* Renders Github-Flavored Markdown from the Knowledge Graph. This Markdown can include
|
|
24
24
|
* arbitrary HTML. Any HTML will be sanitized according to Rehype's default Schema.
|
|
25
|
-
*
|
|
25
|
+
*
|
|
26
26
|
* @public
|
|
27
27
|
*/
|
|
28
28
|
export function Markdown({ content }: MarkdownProps) {
|
|
@@ -33,4 +33,4 @@ export function Markdown({ content }: MarkdownProps) {
|
|
|
33
33
|
rehypePlugins={unifiedPlugins.rehype}
|
|
34
34
|
/>
|
|
35
35
|
);
|
|
36
|
-
}
|
|
36
|
+
}
|
|
@@ -2,6 +2,7 @@ import { Message, MessageSource } from "@yext/chat-headless-react";
|
|
|
2
2
|
import { useComposedCssClasses } from "../hooks";
|
|
3
3
|
import { twMerge } from "tailwind-merge";
|
|
4
4
|
import { Markdown } from "./Markdown";
|
|
5
|
+
import { withStylelessCssClasses } from "../utils/withStylelessCssClasses";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* The CSS class interface for the {@link MessageBubble} component.
|
|
@@ -21,10 +22,11 @@ export interface MessageBubbleCssClasses {
|
|
|
21
22
|
timestamp__user?: string;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
const builtInCssClasses: MessageBubbleCssClasses = {
|
|
25
|
+
const builtInCssClasses: MessageBubbleCssClasses = withStylelessCssClasses('MessageBubble', {
|
|
25
26
|
topContainer: "w-full animate-fade-in @container",
|
|
26
27
|
subContainer:
|
|
27
28
|
"flex flex-col @lg:flex-row @lg:items-center @lg:gap-x-2 @lg:m-1",
|
|
29
|
+
subContainer__bot: "",
|
|
28
30
|
subContainer__user: "@lg:flex-row-reverse",
|
|
29
31
|
message:
|
|
30
32
|
"peer rounded-2xl text-[13px] @[480px]:text-base p-4 w-fit max-w-[80%] prose overflow-x-auto",
|
|
@@ -33,8 +35,9 @@ const builtInCssClasses: MessageBubbleCssClasses = {
|
|
|
33
35
|
"ml-auto @lg:ml-0 text-white bg-gradient-to-tr from-blue-600 to-blue-700",
|
|
34
36
|
timestamp:
|
|
35
37
|
"w-fit my-0.5 text-slate-400 text-[13px] opacity-0 peer-hover:opacity-100 duration-200 whitespace-pre-wrap",
|
|
38
|
+
timestamp__bot: "",
|
|
36
39
|
timestamp__user: "ml-auto",
|
|
37
|
-
};
|
|
40
|
+
});
|
|
38
41
|
|
|
39
42
|
/**
|
|
40
43
|
* The props for the {@link MessageBubble} component.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MessageSource,
|
|
3
|
+
useChatActions,
|
|
4
|
+
useChatState,
|
|
5
|
+
} from "@yext/chat-headless-react";
|
|
6
|
+
import { useCallback } from "react";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns a default handler function for API errors. It will log the error and
|
|
10
|
+
* add a default error message to state.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export function useDefaultHandleApiError() {
|
|
15
|
+
const chat = useChatActions();
|
|
16
|
+
const messages = useChatState((s) => s.conversation.messages);
|
|
17
|
+
|
|
18
|
+
return useCallback(
|
|
19
|
+
(e: unknown) => {
|
|
20
|
+
console.error(e);
|
|
21
|
+
chat.setMessages([
|
|
22
|
+
...messages,
|
|
23
|
+
{
|
|
24
|
+
text: "Sorry, I'm unable to respond at the moment. Please try again later!",
|
|
25
|
+
source: MessageSource.BOT,
|
|
26
|
+
timestamp: new Date().toISOString(),
|
|
27
|
+
},
|
|
28
|
+
]);
|
|
29
|
+
},
|
|
30
|
+
[chat, messages]
|
|
31
|
+
);
|
|
32
|
+
}
|
package/src/icons/Arrow.tsx
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
export function ArrowIcon(): JSX.Element {
|
|
2
2
|
return (
|
|
3
|
-
<svg
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
<svg
|
|
4
|
+
width="100%"
|
|
5
|
+
height="100%"
|
|
6
|
+
viewBox="0 0 20 20"
|
|
7
|
+
fill="none"
|
|
8
|
+
stroke="currentColor"
|
|
9
|
+
strokeLinecap="square"
|
|
10
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
11
|
+
>
|
|
12
|
+
<path d="M10 5V15" />
|
|
13
|
+
<path d="M15 8.84615L10 4L5 8.84615" />
|
|
6
14
|
</svg>
|
|
7
15
|
);
|
|
8
|
-
}
|
|
16
|
+
}
|
package/src/icons/Cross.tsx
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
export function CrossIcon(): JSX.Element {
|
|
2
2
|
return (
|
|
3
|
-
<svg
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
<svg
|
|
4
|
+
width="100%"
|
|
5
|
+
height="100%"
|
|
6
|
+
viewBox="0 0 20 20"
|
|
7
|
+
stroke="currentColor"
|
|
8
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
9
|
+
>
|
|
10
|
+
<path d="M14 14L6 6" strokeLinecap="square" strokeLinejoin="round" />
|
|
11
|
+
<path d="M14 6L6 14" strokeLinecap="square" strokeLinejoin="round" />
|
|
6
12
|
</svg>
|
|
7
13
|
);
|
|
8
|
-
}
|
|
14
|
+
}
|
package/src/icons/DualSync.tsx
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
export function DualSyncIcon(): JSX.Element {
|
|
2
2
|
return (
|
|
3
|
-
<svg
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
<svg
|
|
4
|
+
width="100%"
|
|
5
|
+
height="100%"
|
|
6
|
+
viewBox="0 0 20 20"
|
|
7
|
+
fill="currentColor"
|
|
8
|
+
stroke="currentColor"
|
|
9
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
10
|
+
>
|
|
11
|
+
<path d="M14.3148 6.47397C14.9074 7.36266 15.2593 8.46858 15.2593 9.63375C15.2593 12.675 12.9259 15.1634 10.0741 15.1634C9.11111 15.1634 8.2037 14.8671 7.42593 14.3734L9.05556 13.8995L8.87037 13.1293L6 13.939L6.75926 17L7.48148 16.8025L7.03704 15.0449C7.92593 15.6176 8.96296 15.9336 10.0741 15.9336C13.3519 15.9336 16 13.1095 16 9.614C16 8.2711 15.6111 7.02693 14.9259 6L14.3148 6.47397Z" />
|
|
12
|
+
<path d="M9.92593 4.68459C10.8889 4.68459 11.7963 4.95341 12.5741 5.40143L10.9444 5.81362L11.1296 6.51254L14 5.77778L13.2407 3L12.5185 3.17921L12.963 4.77419C12.0741 4.25448 11.037 3.96774 9.92593 3.96774C6.64815 3.96774 4 6.53047 4 9.70251C4 10.9211 4.40741 12.0681 5.07407 13L5.68518 12.5878C5.09259 11.7634 4.74074 10.7778 4.74074 9.70251C4.74074 6.94265 7.07407 4.68459 9.92593 4.68459Z" />
|
|
6
13
|
</svg>
|
|
7
14
|
);
|
|
8
|
-
}
|
|
15
|
+
}
|
package/src/icons/Message.tsx
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
export function MessageIcon(): JSX.Element {
|
|
2
2
|
return (
|
|
3
|
-
<svg
|
|
4
|
-
|
|
3
|
+
<svg
|
|
4
|
+
width="100%"
|
|
5
|
+
height="100%"
|
|
6
|
+
viewBox="0 0 20 20"
|
|
7
|
+
fill="currentColor"
|
|
8
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
9
|
+
>
|
|
10
|
+
<path d="M3.37424 17.1166L6.31902 14.1718H16.6258V4.84663H3.37424V17.1166ZM3.86503 5.33742H16.135V13.681H6.31902H6.11043L5.96319 13.8282L3.85276 15.9386V5.33742H3.86503Z" />
|
|
5
11
|
<path d="M14.4172 9.2638H6.07362V9.7546H14.4172V9.2638Z" />
|
|
6
12
|
<path d="M13.9264 10.7362H6.07362V11.227H13.9264V10.7362Z" />
|
|
7
13
|
<path d="M13.4356 7.79141H6.07362V8.28221H13.4356V7.79141Z" />
|
|
8
14
|
</svg>
|
|
9
15
|
);
|
|
10
|
-
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append styleless css classnames that user can target to add custom styling to components.
|
|
3
|
+
* The classes follows a format of "yext-chat-component-name__css-interface-field-name".
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* the styleless css class name for {@link MessageBubble}'s "message__bot" field defined
|
|
7
|
+
* in its {@link MessageBubbleCssClasses} interface is "yext-chat-message-bubble__message__bot"
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*
|
|
11
|
+
* @param componentName - the component name
|
|
12
|
+
* @param builtInClasses - built in css classnames of the component
|
|
13
|
+
*
|
|
14
|
+
* @returns builtInClasses with styleless css classnames
|
|
15
|
+
*/
|
|
16
|
+
export function withStylelessCssClasses<
|
|
17
|
+
ClassInterface extends Partial<Record<keyof ClassInterface, string | object>>
|
|
18
|
+
>(
|
|
19
|
+
componentName: string,
|
|
20
|
+
builtInClasses: Readonly<ClassInterface>
|
|
21
|
+
): ClassInterface {
|
|
22
|
+
const formatString = (str: string) => str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
23
|
+
const classes = { ...builtInClasses }
|
|
24
|
+
Object.keys(builtInClasses).forEach((key) => {
|
|
25
|
+
classes[key] = `${classes[key]} yext-chat${formatString(componentName)}__${formatString(key)}`
|
|
26
|
+
})
|
|
27
|
+
return classes;
|
|
28
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"defaultHandleError.d.ts","sourceRoot":"","sources":["../../../src/utils/defaultHandleError.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,OAAO,QAE/C"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defaultHandleApiError = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Default handler function for API errors.
|
|
6
|
-
*
|
|
7
|
-
* @internal
|
|
8
|
-
*
|
|
9
|
-
* @param e - error returned from api request
|
|
10
|
-
*/
|
|
11
|
-
function defaultHandleApiError(e) {
|
|
12
|
-
console.error(e);
|
|
13
|
-
}
|
|
14
|
-
exports.defaultHandleApiError = defaultHandleApiError;
|
|
15
|
-
//# sourceMappingURL=defaultHandleError.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"defaultHandleError.js","sourceRoot":"","sources":["../../../src/utils/defaultHandleError.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,SAAgB,qBAAqB,CAAC,CAAU;IAC9C,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACnB,CAAC;AAFD,sDAEC"}
|