OpenSourceUIOpenSourceUIOpensource UI
GitHub—
  1. Home
  2. Components
  3. Docks
  4. Spotlight Bar
Back to DocksComponents / Docks

Spotlight Bar

Free Docks React component — SpotlightBar. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Spotlight-style command search in the frosted dock shell — keyboard nav and suggestions.

Preview

K

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/docks/spotlight-bar.tsx in your project.

  4. 4

    Import and render:

    Example

    import { SpotlightBar } from "@/components/docks/spotlight-bar";

    <SpotlightBar onSelectSuggestion={(id) => console.log(id)} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/docks/spotlight-bar.tsx

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
183
184
185
186
187
188
"use client";
 
import {
forwardRef,
useId,
useState,
type ComponentPropsWithoutRef,
type FormEvent,
type KeyboardEvent,
} from "react";
 
import { Command, Search, Sparkles } from "lucide-react";
 
import { cn } from "@/lib/cn";
 
export type SpotlightSuggestion = Readonly<{
id: string;
label: string;
hint?: string;
}>;
 
export type SpotlightBarProps = Readonly<
{
placeholder?: string;
suggestions?: readonly SpotlightSuggestion[];
showShortcut?: boolean;
onSubmit?: (value: string) => void;
onSelectSuggestion?: (id: string) => void;
} & Omit<ComponentPropsWithoutRef<"form">, "onSubmit">
>;
 
const DEFAULT_SUGGESTIONS: readonly SpotlightSuggestion[] = [
{ id: "new-page", label: "Create page", hint: "P" },
{ id: "invite", label: "Invite teammate", hint: "I" },
{ id: "analytics", label: "Open analytics", hint: "A" },
];
 
// Spotlight bar — frosted command search bar aligned with MacDock shell.
export const SpotlightBar = forwardRef<HTMLFormElement, SpotlightBarProps>(
(
{
className,
placeholder = "Search actions, pages, people…",
suggestions = DEFAULT_SUGGESTIONS,
showShortcut = true,
onSubmit,
onSelectSuggestion,
...props
},
ref,
) => {
const inputId = useId();
const [value, setValue] = useState("");
const [open, setOpen] = useState(false);
const [activeIndex, setActiveIndex] = useState(0);
 
const filtered = suggestions.filter((item) =>
item.label.toLowerCase().includes(value.trim().toLowerCase()),
);
 
const run = (event?: FormEvent) => {
event?.preventDefault();
const target = filtered[activeIndex];
if (target) {
onSelectSuggestion?.(target.id);
setValue("");
setOpen(false);
return;
}
if (value.trim()) {
onSubmit?.(value.trim());
setValue("");
setOpen(false);
}
};
 
const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
if (!open && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
setOpen(true);
return;
}
if (event.key === "ArrowDown") {
event.preventDefault();
setActiveIndex((index) =>
filtered.length === 0 ? 0 : (index + 1) % filtered.length,
);
}
if (event.key === "ArrowUp") {
event.preventDefault();
setActiveIndex((index) =>
filtered.length === 0
? 0
: (index - 1 + filtered.length) % filtered.length,
);
}
if (event.key === "Escape") setOpen(false);
};
 
return (
<form
ref={ref}
data-slot="spotlight-bar"
onSubmit={run}
className={cn("relative w-sm font-sans", className)}
{...props}
>
<div className="flex items-center gap-2.5 rounded-2xl border border-neutral-50 bg-white/50 p-3 shadow-xl shadow-black/10 backdrop-blur-md">
<label htmlFor={inputId} className="sr-only">
Spotlight search
</label>
<Search
size={16}
aria-hidden
className="ml-1 shrink-0 text-neutral-400"
/>
<input
id={inputId}
value={value}
onChange={(event) => {
setValue(event.target.value);
setOpen(true);
setActiveIndex(0);
}}
onFocus={() => setOpen(true)}
onKeyDown={onKeyDown}
placeholder={placeholder}
className="min-w-0 flex-1 bg-transparent text-sm text-neutral-900 ring-0 outline-none placeholder:text-neutral-400 focus:border-transparent focus:ring-0"
/>
{showShortcut ? (
<kbd className="hidden items-center gap-0.5 rounded-md border border-neutral-100 bg-neutral-50 px-1.5 py-0.5 text-[10px] font-medium text-neutral-500 md:inline-flex">
<Command size={10} aria-hidden />K
</kbd>
) : null}
</div>
 
<ul
role="listbox"
className={cn(
"ease-smooth absolute top-[calc(100%+0.5rem)] left-0 z-10 w-full origin-top overflow-hidden rounded-2xl border border-neutral-50 bg-white/90 p-2 shadow-xl shadow-black/10 backdrop-blur-md transition-[opacity,transform] duration-200",
open && filtered.length > 0
? "pointer-events-auto scale-100 opacity-100"
: "pointer-events-none scale-95 opacity-0",
)}
>
{filtered.map((item, index) => (
<li key={item.id}>
<button
type="button"
role="option"
aria-selected={index === activeIndex}
onMouseEnter={() => setActiveIndex(index)}
onClick={() => {
onSelectSuggestion?.(item.id);
setValue("");
setOpen(false);
}}
className={cn(
"ease-smooth flex w-full items-center justify-between gap-3 rounded-lg px-2.5 py-2.5 text-left transition-colors duration-150",
index === activeIndex
? "bg-neutral-100 text-neutral-900"
: "text-neutral-700 hover:bg-neutral-50",
)}
>
<span className="flex min-w-0 items-center gap-2">
<Sparkles
size={14}
aria-hidden
className="shrink-0 text-neutral-400"
/>
<span className="truncate text-sm font-medium">
{item.label}
</span>
</span>
{item.hint ? (
<kbd className="rounded-md border border-neutral-100 bg-white px-1.5 py-0.5 text-[10px] font-medium text-neutral-500">
{item.hint}
</kbd>
) : null}
</button>
</li>
))}
</ul>
</form>
);
},
);
 
SpotlightBar.displayName = "SpotlightBar";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/docks/spotlight-bar.tsx
Platinum slotAvailable

Feature your product here.

Sticky card next to every component — highest-intent placement.

Claim this card