forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference-version-selector.tsx
More file actions
128 lines (118 loc) · 4.24 KB
/
Copy pathreference-version-selector.tsx
File metadata and controls
128 lines (118 loc) · 4.24 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
"use client";
import Link from "next/link";
import { ChevronDown } from "lucide-react";
import { useEffect, useRef, useState } from "react";
import type { ReferenceVersion } from "@/lib/reference-items";
export type { ReferenceVersion };
export type ReferenceVersionOption = {
version: ReferenceVersion;
href: string;
};
// The selector now switches between SDKs, not just React versions. Labels
// are user-facing; keep them in sync with REFERENCE_VERSIONS.
const VERSION_LABELS: Record<ReferenceVersion, string> = {
v2: "React (V2)",
v1: "React (V1)",
"react-native": "React Native",
vue: "Vue",
angular: "Angular",
core: "Core (TypeScript)",
bot: "Bots",
};
export function ReferenceVersionSelector({
activeVersion,
options,
}: {
activeVersion: ReferenceVersion;
options: ReferenceVersionOption[];
}) {
const [open, setOpen] = useState(false);
const panelRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (!open) return;
const handleClick = (event: MouseEvent) => {
const target = event.target instanceof Node ? event.target : null;
if (!target) return;
if (
panelRef.current?.contains(target) ||
buttonRef.current?.contains(target)
) {
return;
}
setOpen(false);
};
const handleKey = (event: KeyboardEvent) => {
if (event.key === "Escape") setOpen(false);
};
document.addEventListener("mousedown", handleClick);
document.addEventListener("keydown", handleKey);
return () => {
document.removeEventListener("mousedown", handleClick);
document.removeEventListener("keydown", handleKey);
};
}, [open]);
return (
<div>
<div className="relative">
<button
ref={buttonRef}
type="button"
onClick={() => setOpen((value) => !value)}
aria-haspopup="listbox"
aria-expanded={open}
className="shell-docs-radius-control flex h-12 w-full cursor-pointer items-center gap-2 border border-[var(--nav-control-border)] bg-[var(--accent-dim)] p-1.5 text-[13px] font-medium text-[var(--text)] shadow-[var(--shadow-control)] transition-colors hover:border-[var(--nav-control-border-hover)] hover:bg-[var(--accent-light)]"
>
<span
className="shell-docs-picker-icon-chip h-8 w-8 shrink-0 text-base"
aria-hidden="true"
>
🪁
</span>
<span className="min-w-0 flex-1 text-left">
<span className="block truncate leading-tight">
{VERSION_LABELS[activeVersion]}
</span>
<span className="mt-0.5 block text-[9px] uppercase leading-tight tracking-wider text-[var(--text-faint)]">
SDK
</span>
</span>
<ChevronDown className="mr-0.5 h-3.5 w-3.5 shrink-0 text-[var(--text-muted)]" />
</button>
{open && (
<div
ref={panelRef}
role="listbox"
className="shell-docs-radius-surface absolute left-0 right-0 top-full z-50 mt-1 border border-[var(--border)] bg-[var(--bg-surface)] p-2 shadow-[var(--shadow-panel)]"
>
{options.map(({ version, href }) => {
const active = version === activeVersion;
return (
<Link
key={version}
href={href}
role="option"
aria-selected={active}
onClick={() => setOpen(false)}
className={[
"shell-docs-radius-control flex w-full items-center gap-2 px-2 py-1.5 text-[13px] transition-colors",
active
? "bg-[var(--accent-dim)] text-[var(--accent)]"
: "text-[var(--text-secondary)] hover:bg-[var(--bg-elevated)] hover:text-[var(--text)]",
].join(" ")}
>
<span aria-hidden="true" className="shrink-0 text-sm">
🪁
</span>
<span className="min-w-0 flex-1 truncate">
{VERSION_LABELS[version]}
</span>
</Link>
);
})}
</div>
)}
</div>
</div>
);
}