-
Notifications
You must be signed in to change notification settings - Fork 112
/
ChatComposerExamples.tsx
251 lines (225 loc) · 7.12 KB
/
ChatComposerExamples.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { Box } from "@twilio-paste/box";
import { Button } from "@twilio-paste/button";
import { ChatBubble, ChatMessage, ChatMessageMeta, ChatMessageMetaItem } from "@twilio-paste/chat-log";
import type { Chat } from "@twilio-paste/chat-log";
import { SendIcon } from "@twilio-paste/icons/esm/SendIcon";
import {
CLEAR_EDITOR_COMMAND,
COMMAND_PRIORITY_HIGH,
KEY_ENTER_COMMAND,
useLexicalComposerContext,
} from "@twilio-paste/lexical-library";
import * as React from "react";
export const RichTextExample = `const RichTextExample = () => {
return (
<ChatComposer
ariaLabel="A rich text chat composer"
config={{
namespace: 'customer-chat',
onError (e) { throw e },
editorState () {
const root = $getRoot();
if (root.getFirstChild() !== null) return;
root.append(
$createParagraphNode().append(
$createTextNode('Hello '),
$createTextNode('world! ').toggleFormat('bold'),
$createTextNode('This is a '),
$createTextNode('chat composer ').toggleFormat('italic'),
$createTextNode('with rich text functionality.')
)
);
},
}}
/>
)
}
render(<RichTextExample/>)`.trim();
export const MaxHeightExample = `const MaxHeightExample = () => {
return (
<ChatComposer
maxHeight="size10"
ariaLabel="A max height chat composer"
config={{
namespace: 'customer-chat',
onError (e) { throw e },
editorState () {
const root = $getRoot();
if (root.getFirstChild() !== null) return;
for (let i = 0; i < 10; i++) {
root.append(
$createParagraphNode().append(
$createTextNode('this is a really really long initial value')
)
);
}
},
}}
/>
)
}
render(<MaxHeightExample />)`.trim();
function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
}
export const createNewMessage = (message: string): Omit<Chat, "id"> => {
const time = new Date().toLocaleString("en-US", {
hour: "numeric",
minute: "numeric",
hour12: true,
});
const messageDirection = getRandomInt(2) === 1 ? "inbound" : "outbound";
return {
variant: messageDirection,
content: (
<ChatMessage variant={messageDirection}>
<ChatBubble>{message}</ChatBubble>
<ChatMessageMeta aria-label={`said by you at ${time}`}>
<ChatMessageMetaItem>{time}</ChatMessageMetaItem>
</ChatMessageMeta>
</ChatMessage>
),
};
};
export const SendButtonPlugin = ({ onClick }: { onClick: () => void }): JSX.Element => {
const [editor] = useLexicalComposerContext();
const handleSend = (): void => {
onClick();
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
};
return (
<Box position="absolute" top="space30" right="space30">
<Button variant="primary_icon" size="reset" onClick={handleSend}>
<SendIcon decorative={false} title="Send message" />
</Button>
</Box>
);
};
export const EnterKeySubmitPlugin = ({ onKeyDown }: { onKeyDown: () => void }): null => {
const [editor] = useLexicalComposerContext();
const handleEnterKey = React.useCallback(
(event: KeyboardEvent) => {
const { shiftKey, ctrlKey } = event;
if (shiftKey || ctrlKey) return false;
event.preventDefault();
event.stopPropagation();
onKeyDown();
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
return true;
},
[editor, onKeyDown],
);
React.useEffect(() => {
return editor.registerCommand(KEY_ENTER_COMMAND, handleEnterKey, COMMAND_PRIORITY_HIGH);
}, [editor, handleEnterKey]);
return null;
};
export const ChatDialogExample = `const ChatDialog = () => {
const {chats, push} = useChatLogger(
{
content: (
<ChatBookend>
<ChatBookendItem>Today</ChatBookendItem>
<ChatBookendItem>
<strong>Chat Started</strong>・3:34 PM
</ChatBookendItem>
</ChatBookend>
),
},
{
variant: 'inbound',
content: (
<ChatMessage variant="inbound">
<ChatBubble>Quisque ullamcorper ipsum vitae lorem euismod sodales.</ChatBubble>
<ChatBubble>
<ChatAttachment attachmentIcon={<DownloadIcon color="colorTextIcon" decorative />}>
<ChatAttachmentLink href="www.google.com">Document-FINAL.doc</ChatAttachmentLink>
<ChatAttachmentDescription>123 MB</ChatAttachmentDescription>
</ChatAttachment>
</ChatBubble>
<ChatMessageMeta aria-label="said by Gibby Radki at 5:04pm">
<ChatMessageMetaItem>Gibby Radki ・ 5:04 PM</ChatMessageMetaItem>
</ChatMessageMeta>
</ChatMessage>
),
},
{
content: (
<ChatEvent>
<strong>Lauren Gardner</strong> has joined the chat ・ 4:26 PM
</ChatEvent>
),
},
{
variant: 'inbound',
content: (
<ChatMessage variant="inbound">
<ChatBubble>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</ChatBubble>
<ChatMessageMeta aria-label="said by Lauren Gardner at 4:30pm">
<ChatMessageMetaItem>
<Avatar name="Lauren Gardner" size="sizeIcon20" />
Lauren Gardner ・ 4:30 PM
</ChatMessageMetaItem>
</ChatMessageMeta>
</ChatMessage>
),
}
);
const [message, setMessage] = React.useState('');
const [mounted, setMounted] = React.useState(false);
const loggerRef = React.useRef(null);
const scrollerRef = React.useRef(null);
React.useEffect(() => {
setMounted(true);
}, []);
React.useEffect(() => {
if (!mounted || !loggerRef.current) return;
scrollerRef.current?.scrollTo({top: loggerRef.current.scrollHeight, behavior: 'smooth'});
}, [chats, mounted]);
const handleComposerChange = (editorState) => {
editorState.read(() => {
const text = $getRoot().getTextContent();
setMessage(text);
});
};
const submitMessage = () => {
if (message === '') return;
push(createNewMessage(message));
};
return (
<Box>
<Box ref={scrollerRef} overflowX="hidden" overflowY="auto" maxHeight="size50" tabIndex={0}>
<ChatLogger ref={loggerRef} chats={chats} />
</Box>
<Box
borderStyle="solid"
borderWidth="borderWidth0"
borderTopWidth="borderWidth10"
borderColor="colorBorderWeak"
display="flex"
flexDirection="row"
columnGap="space30"
paddingX="space70"
paddingTop="space50"
>
<ChatComposer
maxHeight="size10"
config={{
namespace: 'foo',
onError: (error) => {
throw error;
},
}}
ariaLabel="Message"
placeholder="Type here..."
onChange={handleComposerChange}
>
<ClearEditorPlugin />
<SendButtonPlugin onClick={submitMessage} />
<EnterKeySubmitPlugin onKeyDown={submitMessage} />
</ChatComposer>
</Box>
</Box>
);
};
render(<ChatDialog />)`.trim();