forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch-trigger.tsx
More file actions
76 lines (69 loc) · 2.33 KB
/
Copy pathsearch-trigger.tsx
File metadata and controls
76 lines (69 loc) · 2.33 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
"use client";
import { useState, useEffect } from "react";
import { SearchModal } from "./search-modal";
export function SearchTrigger({
iconOnly = false,
}: { iconOnly?: boolean } = {}) {
const [isMac, setIsMac] = useState(true);
const [open, setOpen] = useState(false);
useEffect(() => {
const mac =
typeof navigator !== "undefined" && /mac/i.test(navigator.userAgent);
setIsMac(mac);
}, []);
useEffect(() => {
function onKeyDown(e: KeyboardEvent) {
if ((e.metaKey || e.ctrlKey) && e.key === "k") {
e.preventDefault();
setOpen((prev) => !prev);
}
if (e.key === "Escape") setOpen(false);
}
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, []);
if (iconOnly) {
return (
<>
<button
onClick={() => setOpen(true)}
className="flex items-center justify-center w-8 h-8 rounded-md text-[var(--text-muted)] hover:text-[var(--text-secondary)] hover:bg-[var(--bg-elevated)] transition-colors cursor-pointer"
aria-label="Search"
>
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
/>
</svg>
</button>
{open && <SearchModalWrapper onClose={() => setOpen(false)} />}
</>
);
}
return (
<>
<button
onClick={() => setOpen(true)}
className="flex items-center gap-2 rounded-lg border border-[var(--border)] bg-[var(--bg-elevated)] px-3 py-1.5 text-xs text-[var(--text-muted)] cursor-pointer hover:border-[var(--text-faint)] transition-colors min-w-[200px]"
>
<span>⌕</span>
<span>Search docs, demos...</span>
<span className="ml-auto font-mono text-[10px] border border-[var(--border)] px-1 py-0.5 rounded bg-[var(--bg-surface)]">
{isMac ? "⌘K" : "Ctrl+K"}
</span>
</button>
{open && <SearchModalWrapper onClose={() => setOpen(false)} />}
</>
);
}
function SearchModalWrapper({ onClose }: { onClose: () => void }) {
return <SearchModal onClose={onClose} />;
}