forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotPopup.tsx
More file actions
359 lines (330 loc) · 8.72 KB
/
Copy pathCopilotPopup.tsx
File metadata and controls
359 lines (330 loc) · 8.72 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// NOTE: This component needs to be exported from index.ts
// e.g. export { CopilotPopup } from "./CopilotPopup";
// export type { CopilotPopupProps, CopilotPopupHandle } from "./CopilotPopup";
import React, {
forwardRef,
useCallback,
useEffect,
useImperativeHandle,
useRef,
useState,
} from "react";
import type { ReactNode } from "react";
import {
Modal,
Pressable,
StyleSheet,
Text,
TouchableOpacity,
View,
useWindowDimensions,
} from "react-native";
import type { ViewStyle } from "react-native";
import { CopilotChat } from "./CopilotChat";
import type { NativeAttachmentsConfig } from "./hooks/use-attachments";
import type { CopilotKitCoreErrorCode } from "@copilotkit/core";
export interface CopilotPopupProps {
/**
* The agent ID to use for this chat session.
* Passed through to CopilotChat.
*/
agentId?: string;
/**
* @deprecated Use `agentId` instead.
*/
agentName?: string;
/**
* Thread ID for this chat session.
*/
threadId?: string;
/**
* Throttle interval (ms) for re-renders.
*/
throttleMs?: number;
/**
* Whether the popup starts in the open state.
* @default false
*/
defaultOpen?: boolean;
/**
* Height of the popup card. Accepts a number (points) or a percentage
* string (e.g. "60%") relative to the screen height.
* @default "60%"
*/
height?: number | string;
/**
* Error handler scoped to this popup's chat agent.
*/
onError?: (error: Error) => void;
/**
* Title displayed in the popup header bar.
* @default "CopilotKit"
*/
headerTitle?: string;
/**
* Enable multimodal file attachments. Forwarded to the internal CopilotChat.
* Children access attachment state via `useCopilotChatContext()`.
*/
attachments?: NativeAttachmentsConfig;
/**
* Optional children rendered below the CopilotChat content
* inside the popup card.
*/
children?: ReactNode;
/**
* Callback fired when the popup opens.
*/
onOpen?: () => void;
/**
* Callback fired when the popup closes.
*/
onClose?: () => void;
/**
* Whether tapping the semi-transparent backdrop dismisses the popup.
* Equivalent to web SDK's `clickOutsideToClose`.
* @default true
*/
dismissOnBackdropPress?: boolean;
/**
* Whether to show the floating action button (FAB) that toggles the popup.
* @default true
*/
showToggleButton?: boolean;
/**
* Custom styles applied to the popup card container.
*/
style?: ViewStyle;
}
/**
* Imperative handle exposed via ref for controlling the popup programmatically.
*/
export interface CopilotPopupHandle {
open: () => void;
close: () => void;
toggle: () => void;
}
/**
* CopilotPopup for React Native.
*
* A floating action button (FAB) that opens a modal chat overlay.
* The popup appears as a card floating above content with rounded corners,
* a shadow, and a semi-transparent backdrop.
*
* ```tsx
* import { CopilotPopup } from "@copilotkit/react-native";
*
* const popupRef = useRef<CopilotPopupHandle>(null);
*
* <CopilotPopup
* ref={popupRef}
* agentId="my-agent"
* headerTitle="Chat"
* defaultOpen={false}
* />
* ```
*/
export const CopilotPopup = forwardRef<CopilotPopupHandle, CopilotPopupProps>(
function CopilotPopup(
{
agentId,
agentName,
threadId,
throttleMs,
defaultOpen = false,
height = "60%",
onError,
headerTitle = "CopilotKit",
attachments: attachmentsConfig,
children,
onOpen,
onClose,
dismissOnBackdropPress = true,
showToggleButton = true,
style,
}: CopilotPopupProps,
ref: React.Ref<CopilotPopupHandle>,
) {
const [visible, setVisible] = useState(defaultOpen);
const { height: screenHeight } = useWindowDimensions();
// Stable refs for callbacks to avoid effect churn
const onOpenRef = useRef(onOpen);
const onCloseRef = useRef(onClose);
useEffect(() => {
onOpenRef.current = onOpen;
}, [onOpen]);
useEffect(() => {
onCloseRef.current = onClose;
}, [onClose]);
const handleOpen = useCallback(() => {
setVisible(true);
onOpenRef.current?.();
}, []);
const handleClose = useCallback(() => {
setVisible(false);
onCloseRef.current?.();
}, []);
const handleToggle = useCallback(() => {
setVisible((prev) => {
const next = !prev;
if (next) {
onOpenRef.current?.();
} else {
onCloseRef.current?.();
}
return next;
});
}, []);
// Expose imperative methods
useImperativeHandle(
ref,
() => ({
open: handleOpen,
close: handleClose,
toggle: handleToggle,
}),
[handleOpen, handleClose, handleToggle],
);
// Resolve popup height
const resolvedHeight =
typeof height === "string" && height.endsWith("%")
? (parseFloat(height) / 100) * screenHeight
: typeof height === "number"
? height
: 0.6 * screenHeight;
// Wrap onError to match CopilotChat's expected signature
const chatOnError = onError
? (event: {
error: Error;
code: CopilotKitCoreErrorCode;
context: Record<string, any>;
}) => onError(event.error)
: undefined;
return (
<>
{/* Floating Action Button */}
{showToggleButton && !visible && (
<TouchableOpacity
testID="copilot-popup-fab"
style={styles.fab}
onPress={handleToggle}
activeOpacity={0.8}
accessibilityLabel="Open chat"
accessibilityRole="button"
>
<Text style={styles.fabIcon}>💬</Text>
</TouchableOpacity>
)}
{/* Modal Overlay */}
<Modal
testID="copilot-popup-modal"
visible={visible}
transparent
animationType="slide"
onRequestClose={handleClose}
>
{/* Backdrop */}
<Pressable
testID="copilot-popup-backdrop"
style={styles.backdrop}
onPress={dismissOnBackdropPress ? handleClose : undefined}
>
{/* Card — stop propagation so tapping the card doesn't dismiss */}
<Pressable
testID="copilot-popup-card"
style={[styles.card, { height: resolvedHeight }, style]}
onPress={() => {
// Prevent backdrop press from firing
}}
>
{/* Header */}
<View style={styles.header}>
<Text style={styles.headerTitle}>{headerTitle}</Text>
<TouchableOpacity
testID="copilot-popup-close"
onPress={handleClose}
hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}
accessibilityLabel="Close chat"
accessibilityRole="button"
>
<Text style={styles.closeButton}>✕</Text>
</TouchableOpacity>
</View>
{/* Chat Content */}
<View style={styles.chatContainer}>
<CopilotChat
agentId={agentId}
agentName={agentName}
threadId={threadId}
throttleMs={throttleMs}
onError={chatOnError}
attachments={attachmentsConfig}
>
{children}
</CopilotChat>
</View>
</Pressable>
</Pressable>
</Modal>
</>
);
},
);
const styles = StyleSheet.create({
fab: {
position: "absolute",
bottom: 24,
right: 24,
width: 56,
height: 56,
borderRadius: 28,
backgroundColor: "#6366f1",
alignItems: "center",
justifyContent: "center",
elevation: 6,
shadowColor: "#000",
shadowOffset: { width: 0, height: 3 },
shadowOpacity: 0.27,
shadowRadius: 4.65,
},
fabIcon: {
fontSize: 24,
},
backdrop: {
flex: 1,
backgroundColor: "rgba(0, 0, 0, 0.4)",
justifyContent: "flex-end",
},
card: {
backgroundColor: "#ffffff",
borderTopLeftRadius: 16,
borderTopRightRadius: 16,
overflow: "hidden",
elevation: 10,
shadowColor: "#000",
shadowOffset: { width: 0, height: -3 },
shadowOpacity: 0.25,
shadowRadius: 8,
},
header: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
paddingHorizontal: 16,
paddingVertical: 12,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: "#e5e7eb",
},
headerTitle: {
fontSize: 17,
fontWeight: "600",
color: "#111827",
},
closeButton: {
fontSize: 18,
color: "#6b7280",
fontWeight: "500",
},
chatContainer: {
flex: 1,
},
});