forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot-chat-tools-menu.ts
More file actions
182 lines (169 loc) · 6.08 KB
/
Copy pathcopilot-chat-tools-menu.ts
File metadata and controls
182 lines (169 loc) · 6.08 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
import {
Component,
input,
computed,
ChangeDetectionStrategy,
ViewEncapsulation,
} from "@angular/core";
import { CommonModule } from "@angular/common";
import { CdkMenuModule } from "@angular/cdk/menu";
import { OverlayModule } from "@angular/cdk/overlay";
import { LucideAngularModule, Settings2, ChevronRight } from "lucide-angular";
import type { ToolsMenuItem } from "./copilot-chat-input.types";
import { cn } from "../../utils";
import { injectChatLabels } from "../../chat-config";
@Component({
selector: "copilot-chat-tools-menu",
standalone: true,
imports: [CommonModule, CdkMenuModule, OverlayModule, LucideAngularModule],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `
@if (hasItems()) {
<button
type="button"
[disabled]="disabled()"
[class]="buttonClass()"
[cdkMenuTriggerFor]="menu"
>
<lucide-angular [img]="Settings2Icon" [size]="18"></lucide-angular>
<span class="text-sm font-normal">{{ label }}</span>
</button>
<ng-template #menu>
<div
class="min-w-[200px] bg-white dark:bg-[#1F1F1F] border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg p-1"
cdkMenu
>
@for (item of toolsMenu(); track $index) {
@if (item === "-") {
<div class="h-px bg-gray-200 dark:bg-gray-700 my-1"></div>
} @else if (isMenuItem(item)) {
@if (item.items && item.items.length > 0) {
<!-- Submenu trigger -->
<button
type="button"
class="w-full px-3 py-2 text-left bg-transparent border-none rounded hover:bg-gray-100 dark:hover:bg-gray-800 cursor-pointer text-sm flex items-center justify-between"
[cdkMenuTriggerFor]="submenu"
cdkMenuItem
>
{{ item.label }}
<lucide-angular
[img]="ChevronRightIcon"
[size]="12"
class="ml-auto"
></lucide-angular>
</button>
<!-- Submenu template -->
<ng-template #submenu>
<div
class="min-w-[200px] bg-white dark:bg-[#1F1F1F] border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg p-1"
cdkMenu
>
@for (subItem of item.items; track $index) {
@if (subItem === "-") {
<div class="h-px bg-gray-200 dark:bg-gray-700 my-1"></div>
} @else if (isMenuItem(subItem)) {
<button
type="button"
class="w-full px-3 py-2 text-left bg-transparent border-none rounded hover:bg-gray-100 dark:hover:bg-gray-800 cursor-pointer text-sm"
(click)="handleItemClick(subItem)"
cdkMenuItem
>
{{ subItem.label }}
</button>
}
}
</div>
</ng-template>
} @else {
<!-- Regular menu item -->
<button
type="button"
class="w-full px-3 py-2 text-left bg-transparent border-none rounded hover:bg-gray-100 dark:hover:bg-gray-800 cursor-pointer text-sm"
(click)="handleItemClick(item)"
cdkMenuItem
>
{{ item.label }}
</button>
}
}
}
</div>
</ng-template>
}
`,
styles: [
`
/* CDK Overlay styles for positioning */
.cdk-overlay-pane {
position: absolute;
pointer-events: auto;
z-index: 1000;
}
/* Ensure menu appears above other content */
.cdk-overlay-container {
position: fixed;
z-index: 1000;
}
/* Menu animation */
[cdkMenu] {
animation: menuFadeIn 0.15s ease-out;
}
@keyframes menuFadeIn {
from {
opacity: 0;
transform: translateY(4px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
`,
],
})
export class CopilotChatToolsMenu {
readonly Settings2Icon = Settings2;
readonly ChevronRightIcon = ChevronRight;
inputToolsMenu = input<(ToolsMenuItem | "-")[] | undefined>();
inputDisabled = input<boolean | undefined>();
inputClass = input<string | undefined>();
private labels = injectChatLabels();
// Derive state from inputs
toolsMenu = computed(() => this.inputToolsMenu() ?? []);
disabled = computed(() => this.inputDisabled() ?? false);
customClass = computed(() => this.inputClass());
hasItems = computed(() => this.toolsMenu().length > 0);
readonly label = this.labels.chatInputToolbarToolsButtonLabel;
buttonClass = computed(() => {
const baseClasses = cn(
// Base button styles
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-full text-sm font-medium",
"transition-all disabled:pointer-events-none disabled:opacity-50",
"shrink-0 outline-none",
"focus-visible:ring-[3px]",
// chatInputToolbarSecondary variant
"cursor-pointer",
"bg-transparent text-[#444444]",
"dark:text-white dark:border-[#404040]",
"transition-colors",
"focus:outline-none",
"hover:bg-[#f8f8f8] hover:text-[#333333]",
"dark:hover:bg-[#404040] dark:hover:text-[#FFFFFF]",
"disabled:cursor-not-allowed disabled:opacity-50",
"disabled:hover:bg-transparent disabled:hover:text-[#444444]",
"dark:disabled:hover:bg-transparent dark:disabled:hover:text-[#CCCCCC]",
// Size
"h-9 px-3 gap-2 font-normal",
);
return cn(baseClasses, this.customClass());
});
isMenuItem(item: any): item is ToolsMenuItem {
return item && typeof item === "object" && "label" in item;
}
handleItemClick(item: ToolsMenuItem): void {
if (item.action) {
item.action();
}
}
}