Palette
Open a command palette from a button or Ctrl/Cmd+K. The dialog is named by its title, runs the chosen command, closes and returns focus to the opener.
"use client"
import {
FolderPlusIcon,
LayoutDashboardIcon,
SearchIcon,
SettingsIcon,
UserPlusIcon,
} from "lucide-react"
import * as React from "react"
import { Button } from "@/components/ui/button"
import {
Command,
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut,
} from "@/components/ui/command"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
/** Opens a controlled dialog and returns focus to whatever opened it. */
function useRestoringOpen() {
const [open, setOpen] = React.useState(false)
const isOpen = React.useRef(false)
const returnFocus = React.useRef<HTMLElement | null>(null)
const change = React.useCallback((next: boolean) => {
if (next === isOpen.current) return
isOpen.current = next
if (next) returnFocus.current = document.activeElement as HTMLElement | null
setOpen(next)
if (!next) requestAnimationFrame(() => returnFocus.current?.focus())
}, [])
return [open, change] as const
}
export function CommandPalette() {
const [paletteOpen, setPaletteOpen] = useRestoringOpen()
const [lastAction, setLastAction] = React.useState<string | null>(null)
React.useEffect(() => {
function onKeyDown(event: KeyboardEvent) {
if (event.key.toLowerCase() === "k" && (event.metaKey || event.ctrlKey) && !event.repeat) {
event.preventDefault()
setPaletteOpen(true)
}
}
document.addEventListener("keydown", onKeyDown)
return () => document.removeEventListener("keydown", onKeyDown)
}, [setPaletteOpen])
function run(action: string) {
setLastAction(action)
setPaletteOpen(false)
}
return (
<div className="flex w-full flex-col items-start gap-6">
<div className="flex flex-col items-start gap-2">
<Button
intent="neutral"
emphasis="stroke"
aria-keyshortcuts="Control+K Meta+K"
onClick={() => setPaletteOpen(true)}
>
<SearchIcon />
Search commands
<KbdGroup aria-hidden="true">
<Kbd>Ctrl</Kbd>
<Kbd>K</Kbd>
</KbdGroup>
</Button>
<p className="text-paragraph-sm text-sub-600" aria-live="polite">
{lastAction ? `Last command: ${lastAction}` : "No command run yet."}
</p>
<CommandDialog
open={paletteOpen}
onOpenChange={setPaletteOpen}
title="Command palette"
description="Search for a page or an action, then press Enter to run it."
>
<CommandInput placeholder="Search pages and actions..." />
<CommandList>
<CommandEmpty>Nothing matches that search. Try a page name or a verb.</CommandEmpty>
<CommandGroup heading="Go to">
<CommandItem onSelect={() => run("Open dashboard")}>
<LayoutDashboardIcon />
Dashboard
<CommandShortcut>G D</CommandShortcut>
</CommandItem>
<CommandItem onSelect={() => run("Open account settings")}>
<SettingsIcon />
Account settings
<CommandShortcut>G S</CommandShortcut>
</CommandItem>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Actions">
<CommandItem onSelect={() => run("Create project")}>
<FolderPlusIcon />
Create project
<CommandShortcut>Ctrl Alt N</CommandShortcut>
</CommandItem>
<CommandItem onSelect={() => run("Invite teammate")}>
<UserPlusIcon />
Invite teammate
</CommandItem>
</CommandGroup>
</CommandList>
</CommandDialog>
</div>
</div>
)
}