components/command-menu.tsx (shadcn's free Command)
"use client";
import * as React from "react";
import { useRouter } from "next/navigation";
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut,
} from "@/components/ui/command";
const pages = [
{ href: "/dashboard", label: "Overview" },
{ href: "/dashboard/invoices", label: "Invoices" },
{ href: "/dashboard/team", label: "Team" },
];
// shadcn's free Command in a dialog, opened with Cmd+K (Ctrl+K on Windows and Linux).
export function CommandMenu() {
const [open, setOpen] = React.useState(false);
const router = useRouter();
React.useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key.toLowerCase() === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
setOpen((o) => !o);
}
};
document.addEventListener("keydown", onKey);
return () => document.removeEventListener("keydown", onKey);
}, []);
const go = (href: string) => {
setOpen(false);
router.push(href);
};
return (
<CommandDialog open={open} onOpenChange={setOpen}>
<CommandInput placeholder="Type a page or an action..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup heading="Go to">
{pages.map((p) => (
<CommandItem key={p.href} value={p.label} onSelect={() => go(p.href)}>
{p.label}
</CommandItem>
))}
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Actions">
<CommandItem value="New invoice" onSelect={() => go("/dashboard/invoices/new")}>
New invoice
<CommandShortcut>N</CommandShortcut>
</CommandItem>
</CommandGroup>
</CommandList>
</CommandDialog>
);
}
app/globals.css (Command-K Dashboard on Tailwind v4)
/* Command-K Dashboard was built with preflight off. Tailwind v4 turns it back on. */
/* Replace @import "tailwindcss"; with: */
@layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/utilities.css" layer(utilities);