forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotPopupView.tsx
More file actions
317 lines (279 loc) · 8.84 KB
/
Copy pathCopilotPopupView.tsx
File metadata and controls
317 lines (279 loc) · 8.84 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import React, { useEffect, useMemo, useRef, useState } from "react";
import CopilotChatView, {
CopilotChatViewProps,
WelcomeScreenProps,
} from "./CopilotChatView";
import CopilotChatToggleButton from "./CopilotChatToggleButton";
import { CopilotModalHeader } from "./CopilotModalHeader";
import { cn } from "../../lib/utils";
import { renderSlot, SlotValue } from "../../lib/slots";
import {
CopilotChatConfigurationProvider,
CopilotChatDefaultLabels,
useCopilotChatConfiguration,
} from "../../providers/CopilotChatConfigurationProvider";
const DEFAULT_POPUP_WIDTH = 420;
const DEFAULT_POPUP_HEIGHT = 560;
export type CopilotPopupViewProps = CopilotChatViewProps & {
header?: SlotValue<typeof CopilotModalHeader>;
toggleButton?: SlotValue<typeof CopilotChatToggleButton>;
width?: number | string;
height?: number | string;
clickOutsideToClose?: boolean;
defaultOpen?: boolean;
};
const dimensionToCss = (
value: number | string | undefined,
fallback: number,
): string => {
if (typeof value === "number" && Number.isFinite(value)) {
return `${value}px`;
}
if (typeof value === "string" && value.trim().length > 0) {
return value;
}
return `${fallback}px`;
};
export function CopilotPopupView({
header,
toggleButton,
width,
height,
clickOutsideToClose,
defaultOpen = true,
className,
...restProps
}: CopilotPopupViewProps) {
return (
<CopilotChatConfigurationProvider isModalDefaultOpen={defaultOpen}>
<CopilotPopupViewInternal
header={header}
toggleButton={toggleButton}
width={width}
height={height}
clickOutsideToClose={clickOutsideToClose}
className={className}
{...restProps}
/>
</CopilotChatConfigurationProvider>
);
}
function CopilotPopupViewInternal({
header,
toggleButton,
width,
height,
clickOutsideToClose,
className,
...restProps
}: Omit<CopilotPopupViewProps, "defaultOpen">) {
const configuration = useCopilotChatConfiguration();
const isPopupOpen = configuration?.isModalOpen ?? false;
const setModalOpen = configuration?.setModalOpen;
const labels = configuration?.labels ?? CopilotChatDefaultLabels;
const containerRef = useRef<HTMLDivElement>(null);
const [isRendered, setIsRendered] = useState(isPopupOpen);
const [isAnimatingOut, setIsAnimatingOut] = useState(false);
useEffect(() => {
if (isPopupOpen) {
setIsRendered(true);
setIsAnimatingOut(false);
return;
}
if (!isRendered) {
return;
}
setIsAnimatingOut(true);
const timeout = setTimeout(() => {
setIsRendered(false);
setIsAnimatingOut(false);
}, 200);
return () => clearTimeout(timeout);
}, [isPopupOpen, isRendered]);
useEffect(() => {
if (!isPopupOpen) {
return;
}
if (typeof window === "undefined") {
return;
}
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
setModalOpen?.(false);
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [isPopupOpen, setModalOpen]);
useEffect(() => {
if (!isPopupOpen) {
return;
}
const focusTimer = setTimeout(() => {
const container = containerRef.current;
// Don't steal focus if something inside the popup (like the input) is already focused
if (container && !container.contains(document.activeElement)) {
container.focus({ preventScroll: true });
}
}, 200);
return () => clearTimeout(focusTimer);
}, [isPopupOpen]);
useEffect(() => {
if (!isPopupOpen || !clickOutsideToClose) {
return;
}
if (typeof document === "undefined") {
return;
}
const handlePointerDown = (event: PointerEvent) => {
const target = event.target as Node | null;
if (!target) {
return;
}
const container = containerRef.current;
if (container?.contains(target)) {
return;
}
const toggleButton = document.querySelector(
"[data-slot='chat-toggle-button']",
);
if (toggleButton && toggleButton.contains(target)) {
return;
}
setModalOpen?.(false);
};
document.addEventListener("pointerdown", handlePointerDown);
return () => document.removeEventListener("pointerdown", handlePointerDown);
}, [isPopupOpen, clickOutsideToClose, setModalOpen]);
const headerElement = useMemo(
() => renderSlot(header, CopilotModalHeader, {}),
[header],
);
const toggleButtonElement = useMemo(
() => renderSlot(toggleButton, CopilotChatToggleButton, {}),
[toggleButton],
);
const resolvedWidth = dimensionToCss(width, DEFAULT_POPUP_WIDTH);
const resolvedHeight = dimensionToCss(height, DEFAULT_POPUP_HEIGHT);
const popupStyle = useMemo(
() =>
({
"--copilot-popup-width": resolvedWidth,
"--copilot-popup-height": resolvedHeight,
"--copilot-popup-max-width": "calc(100vw - 3rem)",
"--copilot-popup-max-height": "calc(100dvh - 7.5rem)",
paddingTop: "env(safe-area-inset-top)",
paddingBottom: "env(safe-area-inset-bottom)",
paddingLeft: "env(safe-area-inset-left)",
paddingRight: "env(safe-area-inset-right)",
}) as React.CSSProperties,
[resolvedHeight, resolvedWidth],
);
const popupAnimationClass =
isPopupOpen && !isAnimatingOut
? "cpk:pointer-events-auto cpk:translate-y-0 cpk:opacity-100 cpk:md:scale-100"
: "cpk:pointer-events-none cpk:translate-y-4 cpk:opacity-0 cpk:md:translate-y-5 cpk:md:scale-[0.95]";
const popupContent = isRendered ? (
<div
data-copilotkit
className={cn(
"cpk:fixed cpk:inset-0 cpk:z-[1200] cpk:flex cpk:max-w-full cpk:flex-col cpk:items-stretch",
"cpk:md:inset-auto cpk:md:bottom-24 cpk:md:right-6 cpk:md:items-end cpk:md:gap-4",
)}
>
<div
ref={containerRef}
tabIndex={-1}
role="dialog"
aria-label={labels.modalHeaderTitle}
data-testid="copilot-popup"
data-copilot-popup
className={cn(
"copilotKitPopup copilotKitWindow",
"cpk:relative cpk:flex cpk:h-full cpk:w-full cpk:flex-col cpk:overflow-hidden cpk:bg-background cpk:text-foreground",
"cpk:origin-bottom cpk:focus:outline-none cpk:transform-gpu cpk:transition-transform cpk:transition-opacity cpk:duration-200 cpk:ease-out",
"cpk:md:transition-transform cpk:md:transition-opacity",
"cpk:rounded-none cpk:border cpk:border-border/0 cpk:shadow-none cpk:ring-0",
"cpk:md:h-[var(--copilot-popup-height)] cpk:md:w-[var(--copilot-popup-width)]",
"cpk:md:max-h-[var(--copilot-popup-max-height)] cpk:md:max-w-[var(--copilot-popup-max-width)]",
"cpk:md:origin-bottom-right cpk:md:rounded-2xl cpk:md:border-border cpk:md:shadow-xl cpk:md:ring-1 cpk:md:ring-border/40",
popupAnimationClass,
)}
style={popupStyle}
>
{headerElement}
<div className="cpk:flex-1 cpk:overflow-hidden" data-popup-chat>
<CopilotChatView
{...restProps}
className={cn("cpk:h-full cpk:min-h-0", className)}
/>
</div>
</div>
</div>
) : null;
return (
<>
{toggleButtonElement}
{popupContent}
</>
);
}
CopilotPopupView.displayName = "CopilotPopupView";
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CopilotPopupView {
/**
* Popup-specific welcome screen layout:
* - Welcome message centered vertically
* - Suggestions just above input
* - Input fixed at the bottom
*/
export const WelcomeScreen: React.FC<WelcomeScreenProps> = ({
welcomeMessage,
input,
suggestionView,
className,
children,
...props
}) => {
// Render the welcomeMessage slot internally
const BoundWelcomeMessage = renderSlot(
welcomeMessage,
CopilotChatView.WelcomeMessage,
{},
);
if (children) {
return (
<div data-copilotkit style={{ display: "contents" }}>
{children({
welcomeMessage: BoundWelcomeMessage,
input,
suggestionView,
className,
...props,
})}
</div>
);
}
return (
<div
className={cn("cpk:h-full cpk:flex cpk:flex-col", className)}
{...props}
>
{/* Welcome message - centered vertically */}
<div className="cpk:flex-1 cpk:flex cpk:flex-col cpk:items-center cpk:justify-center cpk:px-4">
{BoundWelcomeMessage}
</div>
{/* Suggestions and input at bottom */}
<div>
{/* Suggestions above input */}
<div className="cpk:mb-4 cpk:flex cpk:justify-center cpk:px-4">
{suggestionView}
</div>
{input}
</div>
</div>
);
};
}
export default CopilotPopupView;