-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchat-bubble.tsx
204 lines (189 loc) · 4.48 KB
/
chat-bubble.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
import * as React from 'react'
import MessageLoading from './message-loading'
import { Avatar, Button } from '@stacklok/ui-kit'
import { tv } from 'tailwind-variants'
import { twMerge } from 'tailwind-merge'
// ChatBubble
const chatBubbleVariant = tv({
base: 'group relative flex max-w-[60%] items-end gap-2 text-sm',
variants: {
variant: {
received: 'self-start',
sent: 'flex-row-reverse self-end',
},
layout: {
default: '',
ai: 'w-full max-w-full items-center',
},
},
defaultVariants: {
variant: 'received',
layout: 'default',
},
})
interface ChatBubbleProps extends React.HTMLAttributes<HTMLDivElement> {
variant: 'received' | 'sent'
layout?: 'default' | 'ai'
}
const ChatBubble = React.forwardRef<HTMLDivElement, ChatBubbleProps>(
({ className, variant, layout, children, ...props }, ref) => (
<div
className={twMerge(
chatBubbleVariant({ variant, layout, className }),
'group relative'
)}
ref={ref}
{...props}
>
{React.Children.map(children, (child) =>
React.isValidElement(child) && typeof child.type !== 'string'
? React.cloneElement(child, {
variant,
layout,
} as React.ComponentProps<typeof child.type>)
: child
)}
</div>
)
)
ChatBubble.displayName = 'ChatBubble'
// ChatBubbleAvatar
interface ChatBubbleAvatarProps extends React.ComponentProps<typeof Avatar> {
src?: string
fallback?: string
className?: string
}
const ChatBubbleAvatar: React.FC<ChatBubbleAvatarProps> = ({
src,
fallback,
className,
...rest
}) => (
<Avatar
src={src}
name={fallback}
className={twMerge(className, 'rounded-full')}
{...rest}
/>
)
// ChatBubbleMessage
const chatBubbleMessageVariants = tv({
base: 'bg-gray-100 p-4 text-primary',
variants: {
variant: {
received: 'rounded-r-lg rounded-tl-lg',
sent: 'rounded-l-lg rounded-tr-lg',
},
layout: {
default: '',
ai: 'w-full rounded-none border-t bg-transparent',
},
},
defaultVariants: {
variant: 'received',
layout: 'default',
},
})
interface ChatBubbleMessageProps extends React.HTMLAttributes<HTMLDivElement> {
variant: 'received' | 'sent'
layout?: 'default' | 'ai'
isLoading?: boolean
}
const ChatBubbleMessage = React.forwardRef<
HTMLDivElement,
ChatBubbleMessageProps
>(
(
{ className, variant, layout, isLoading = false, children, ...props },
ref
) => (
<div
className={twMerge(
chatBubbleMessageVariants({ variant, layout, className }),
'max-w-full break-words'
)}
ref={ref}
{...props}
>
{isLoading ? (
<div className="flex items-center space-x-2">
<MessageLoading />
</div>
) : (
children
)}
</div>
)
)
ChatBubbleMessage.displayName = 'ChatBubbleMessage'
// ChatBubbleTimestamp
interface ChatBubbleTimestampProps
extends React.HTMLAttributes<HTMLDivElement> {
timestamp: string
}
const ChatBubbleTimestamp: React.FC<ChatBubbleTimestampProps> = ({
timestamp,
className,
...props
}) => (
<div className={twMerge('mt-2 text-right text-sm', className)} {...props}>
{timestamp}
</div>
)
// ChatBubbleAction
type ChatBubbleActionProps = React.ComponentProps<typeof Button> & {
icon: React.ReactNode
}
const ChatBubbleAction: React.FC<ChatBubbleActionProps> = ({
icon,
onPress,
className,
variant = 'tertiary',
isIcon = true,
...props
}) => (
<Button
variant={variant}
isIcon={isIcon}
className={className}
onPress={onPress}
{...props}
>
{icon}
</Button>
)
interface ChatBubbleActionWrapperProps
extends React.HTMLAttributes<HTMLDivElement> {
variant?: 'sent' | 'received'
className?: string
}
const ChatBubbleActionWrapper = React.forwardRef<
HTMLDivElement,
ChatBubbleActionWrapperProps
>(({ variant, className, children, ...props }, ref) => (
<div
ref={ref}
className={twMerge(
`absolute top-1/2 flex -translate-y-1/2 opacity-0 transition-opacity duration-200
group-hover:opacity-100`,
variant === 'sent'
? '-left-1 -translate-x-full flex-row-reverse'
: '-right-1 translate-x-full',
className
)}
{...props}
>
{children}
</div>
))
ChatBubbleActionWrapper.displayName = 'ChatBubbleActionWrapper'
export {
ChatBubble,
ChatBubbleAvatar,
ChatBubbleMessage,
ChatBubbleTimestamp,
chatBubbleVariant,
chatBubbleMessageVariants,
ChatBubbleAction,
ChatBubbleActionWrapper,
}