forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotChatToggleButton.tsx
More file actions
171 lines (149 loc) · 4.8 KB
/
Copy pathCopilotChatToggleButton.tsx
File metadata and controls
171 lines (149 loc) · 4.8 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
import React, { useState, MouseEvent } from "react";
import { MessageCircle, X } from "lucide-react";
import { renderSlot, SlotValue } from "../../lib/slots";
import { cn } from "../../lib/utils";
import {
CopilotChatDefaultLabels,
useCopilotChatConfiguration,
} from "../../providers/CopilotChatConfigurationProvider";
const DefaultOpenIcon: React.FC<React.SVGProps<SVGSVGElement>> = ({
className,
...props
}) => (
<MessageCircle
className={cn("cpk:h-6 cpk:w-6", className)}
strokeWidth={1.75}
fill="currentColor"
{...props}
/>
);
const DefaultCloseIcon: React.FC<React.SVGProps<SVGSVGElement>> = ({
className,
...props
}) => (
<X
className={cn("cpk:h-6 cpk:w-6", className)}
strokeWidth={1.75}
{...props}
/>
);
DefaultOpenIcon.displayName = "CopilotChatToggleButton.OpenIcon";
DefaultCloseIcon.displayName = "CopilotChatToggleButton.CloseIcon";
export interface CopilotChatToggleButtonProps extends Omit<
React.ButtonHTMLAttributes<HTMLButtonElement>,
"children"
> {
/** Optional slot override for the chat (closed) icon. */
openIcon?: SlotValue<typeof DefaultOpenIcon>;
/** Optional slot override for the close icon. */
closeIcon?: SlotValue<typeof DefaultCloseIcon>;
}
const ICON_TRANSITION_STYLE: React.CSSProperties = Object.freeze({
transition:
"opacity 120ms ease-out, transform 260ms cubic-bezier(0.22, 1, 0.36, 1)",
});
const ICON_WRAPPER_BASE =
"cpk:pointer-events-none cpk:absolute cpk:inset-0 cpk:flex cpk:items-center cpk:justify-center cpk:will-change-transform";
const BUTTON_BASE_CLASSES = cn(
"copilotKitButton",
"cpk:fixed cpk:bottom-6 cpk:right-6 cpk:z-[1100] cpk:flex cpk:h-14 cpk:w-14 cpk:items-center cpk:justify-center",
"cpk:rounded-full cpk:border cpk:border-primary cpk:bg-primary cpk:text-primary-foreground",
"cpk:shadow-sm cpk:transition-all cpk:duration-200 cpk:ease-out",
"cpk:hover:scale-[1.04] cpk:hover:shadow-md",
"cpk:cursor-pointer",
"cpk:active:scale-[0.96]",
"cpk:focus-visible:outline-none cpk:focus-visible:ring-2 cpk:focus-visible:ring-primary/50 cpk:focus-visible:ring-offset-2 cpk:focus-visible:ring-offset-background",
"cpk:disabled:pointer-events-none cpk:disabled:opacity-60",
);
export const CopilotChatToggleButton = React.forwardRef<
HTMLButtonElement,
CopilotChatToggleButtonProps
>(function CopilotChatToggleButton(
{ openIcon, closeIcon, className, ...buttonProps },
ref,
) {
const { onClick, type, disabled, ...restProps } = buttonProps;
const configuration = useCopilotChatConfiguration();
const labels = configuration?.labels ?? CopilotChatDefaultLabels;
const [fallbackOpen, setFallbackOpen] = useState(false);
const isOpen = configuration?.isModalOpen ?? fallbackOpen;
const setModalOpen = configuration?.setModalOpen ?? setFallbackOpen;
const handleClick = (event: MouseEvent<HTMLButtonElement>) => {
if (disabled) {
return;
}
if (onClick) {
onClick(event);
}
if (event.defaultPrevented) {
return;
}
const nextOpen = !isOpen;
setModalOpen(nextOpen);
};
const renderedOpenIcon = renderSlot(openIcon, DefaultOpenIcon, {
className: "cpk:h-6 cpk:w-6",
"aria-hidden": true,
focusable: false,
});
const renderedCloseIcon = renderSlot(closeIcon, DefaultCloseIcon, {
className: "cpk:h-6 cpk:w-6",
"aria-hidden": true,
focusable: false,
});
const openIconElement = (
<span
aria-hidden="true"
data-slot="chat-toggle-button-open-icon"
className={ICON_WRAPPER_BASE}
style={{
...ICON_TRANSITION_STYLE,
opacity: isOpen ? 0 : 1,
transform: `scale(${isOpen ? 0.75 : 1}) rotate(${isOpen ? 90 : 0}deg)`,
}}
>
{renderedOpenIcon}
</span>
);
const closeIconElement = (
<span
aria-hidden="true"
data-slot="chat-toggle-button-close-icon"
className={ICON_WRAPPER_BASE}
style={{
...ICON_TRANSITION_STYLE,
opacity: isOpen ? 1 : 0,
transform: `scale(${isOpen ? 1 : 0.75}) rotate(${isOpen ? 0 : -90}deg)`,
}}
>
{renderedCloseIcon}
</span>
);
return (
<button
ref={ref}
type={type ?? "button"}
data-copilotkit
data-testid="copilot-chat-toggle"
data-slot="chat-toggle-button"
data-state={isOpen ? "open" : "closed"}
className={cn(BUTTON_BASE_CLASSES, className)}
aria-label={
isOpen ? labels.chatToggleCloseLabel : labels.chatToggleOpenLabel
}
aria-pressed={isOpen}
disabled={disabled}
onClick={handleClick}
{...restProps}
>
{openIconElement}
{closeIconElement}
</button>
);
});
CopilotChatToggleButton.displayName = "CopilotChatToggleButton";
export default CopilotChatToggleButton;
export {
DefaultOpenIcon as CopilotChatToggleButtonOpenIcon,
DefaultCloseIcon as CopilotChatToggleButtonCloseIcon,
};