forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotModalHeader.tsx
More file actions
129 lines (113 loc) · 3.54 KB
/
Copy pathCopilotModalHeader.tsx
File metadata and controls
129 lines (113 loc) · 3.54 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
import React, { useCallback } from "react";
import { cn } from "../../lib/utils";
import {
useCopilotChatConfiguration,
CopilotChatDefaultLabels,
} from "../../providers/CopilotChatConfigurationProvider";
import { renderSlot, WithSlots } from "../../lib/slots";
import { X } from "lucide-react";
type HeaderSlots = {
titleContent: typeof CopilotModalHeader.Title;
closeButton: typeof CopilotModalHeader.CloseButton;
};
type HeaderRestProps = {
title?: string;
} & Omit<React.HTMLAttributes<HTMLDivElement>, "children">;
export type CopilotModalHeaderProps = WithSlots<HeaderSlots, HeaderRestProps>;
export function CopilotModalHeader({
title,
titleContent,
closeButton,
children,
className,
...rest
}: CopilotModalHeaderProps) {
const configuration = useCopilotChatConfiguration();
const fallbackTitle =
configuration?.labels.modalHeaderTitle ??
CopilotChatDefaultLabels.modalHeaderTitle;
const resolvedTitle = title ?? fallbackTitle;
const handleClose = useCallback(() => {
configuration?.setModalOpen?.(false);
}, [configuration]);
const BoundTitle = renderSlot(titleContent, CopilotModalHeader.Title, {
children: resolvedTitle,
});
const BoundCloseButton = renderSlot(
closeButton,
CopilotModalHeader.CloseButton,
{
onClick: handleClose,
},
);
if (children) {
return children({
titleContent: BoundTitle,
closeButton: BoundCloseButton,
title: resolvedTitle,
...rest,
});
}
return (
<header
data-testid="copilot-modal-header"
data-slot="copilot-modal-header"
className={cn(
"copilotKitHeader",
"cpk:flex cpk:items-center cpk:justify-between cpk:border-b cpk:border-border cpk:px-4 cpk:py-4",
"cpk:bg-background/95 cpk:backdrop-blur cpk:supports-[backdrop-filter]:bg-background/80",
className,
)}
{...rest}
>
<div className="cpk:flex cpk:w-full cpk:items-center cpk:gap-2">
<div className="cpk:flex-1" aria-hidden="true" />
<div className="cpk:flex cpk:flex-1 cpk:justify-center cpk:text-center">
{BoundTitle}
</div>
<div className="cpk:flex cpk:flex-1 cpk:justify-end">
{BoundCloseButton}
</div>
</div>
</header>
);
}
CopilotModalHeader.displayName = "CopilotModalHeader";
export namespace CopilotModalHeader {
export const Title: React.FC<React.HTMLAttributes<HTMLDivElement>> = ({
children,
className,
...props
}) => (
<div
data-testid="copilot-header-title"
className={cn(
"cpk:w-full cpk:text-base cpk:font-medium cpk:leading-none cpk:tracking-tight cpk:text-foreground",
className,
)}
{...props}
>
{children}
</div>
);
export const CloseButton: React.FC<
React.ButtonHTMLAttributes<HTMLButtonElement>
> = ({ className, ...props }) => (
<button
type="button"
data-testid="copilot-close-button"
className={cn(
"cpk:inline-flex cpk:size-8 cpk:items-center cpk:justify-center cpk:rounded-full cpk:text-muted-foreground cpk:transition cpk:cursor-pointer",
"cpk:hover:bg-muted cpk:hover:text-foreground cpk:focus-visible:outline-none cpk:focus-visible:ring-2 cpk:focus-visible:ring-ring",
className,
)}
aria-label="Close"
{...props}
>
<X className="cpk:h-4 cpk:w-4" aria-hidden="true" />
</button>
);
}
CopilotModalHeader.Title.displayName = "CopilotModalHeader.Title";
CopilotModalHeader.CloseButton.displayName = "CopilotModalHeader.CloseButton";
export default CopilotModalHeader;