Lewati ke konten

Command

Unduh .md

Command mencari menu dan data sambil kamu mengetik, dan menjadi dasar pencarian di kartu header dasbor.

  • Pakai untuk pencarian global atas menu dan data, dibuka dengan ⌘K atau Ctrl+K.
  • Untuk menyaring satu tabel, pakai Input.
React
Vue
Menu
Rilis
Royalti
Dokumen
Pengaturan⌘,

Halaman tanpa kerangka dasbor bisa memakai kartu pencarian melayang sendiri: kartu (rounded-lg border border-border bg-card shadow-md) yang tampak seperti kolom pencarian dan membuka command palette di dialog. Di dalam kerangka Dasbor, pencarian ada di kartu header.

React
Vue
import * as React from "react"
import { SearchIcon } from "lucide-react"
import {
Command,
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command"
/** The floating search card of the Alt A dashboard header: a card-styled trigger that opens the command palette. */
export default function SearchCardDemo() {
const [open, setOpen] = React.useState(false)
React.useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
// One palette per keypress: the first listener claims it, so other ⌘K handlers stay closed.
if (e.defaultPrevented) return
e.preventDefault()
e.stopPropagation()
setOpen((o) => !o)
}
}
document.addEventListener("keydown", onKey)
return () => document.removeEventListener("keydown", onKey)
}, [])
return (
<>
<button
type="button"
onClick={() => setOpen(true)}
className="flex h-12 w-full max-w-lg items-center gap-3 rounded-lg border border-border bg-card px-4 text-left text-sm text-muted-foreground shadow-md outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50"
>
<SearchIcon className="size-4" />
<span className="flex-1">Cari rilis, artis, atau dokumen</span>
<kbd className="rounded-sm border border-border bg-muted px-1.5 font-mono text-xs">⌘K</kbd>
</button>
<CommandDialog open={open} onOpenChange={setOpen}>
{/* In Base UI, CommandDialog is only the window: the palette inside is a Command. */}
<Command>
<CommandInput placeholder="Cari rilis, artis, atau dokumen" />
<CommandList>
<CommandEmpty>Tidak ada yang cocok.</CommandEmpty>
<CommandGroup heading="Rilis">
<CommandItem>Senja di Kota</CommandItem>
<CommandItem>Kaset Pertama</CommandItem>
</CommandGroup>
<CommandGroup heading="Menu">
<CommandItem>Royalti</CommandItem>
<CommandItem>Dokumen</CommandItem>
</CommandGroup>
</CommandList>
</Command>
</CommandDialog>
</>
)
}
1234
  1. 1Kolom pencarian
  2. 2Judul grup
  3. 3Hasil yang disorot
  4. 4Pintasan keyboard

Kolom pencarian dengan garis border di bawahnya, lalu kelompok item. Item memakai radius-md, dan item yang disorot memakai accent. Di dialog, jendelanya memakai radius-xl; kalau tampil langsung di halaman, daftarnya memakai radius-lg.

Tampil langsung di halaman (Command) atau di dalam dialog (CommandDialog).

React Vue Flutter Tipe Default Kegunaan
CommandDialog open onOpenChange CommandDialog v-model:open showShadSheet boolean false Membuka palette. Di React, taruh Command di dalamnya; di Vue sudah termasuk.
CommandInput placeholder CommandInput placeholder ShadInput placeholder string tidak ada Apa yang dicari.
CommandItem onSelect CommandItem @select onPressed function tidak ada Jalan saat item dipilih.
CommandEmpty CommandEmpty teks kosong elemen tidak ada Tampil kalau tidak ada yang cocok.
import { BarChart3Icon, DiscIcon, FileTextIcon, SettingsIcon } from "lucide-react"
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandShortcut,
} from "@/components/ui/command"
export default function CommandDemo() {
return (
<Command className="w-full max-w-sm border border-border shadow-md">
<CommandInput placeholder="Cari menu atau rilis" />
<CommandList>
<CommandEmpty>Tidak ada yang cocok.</CommandEmpty>
<CommandGroup heading="Menu">
<CommandItem>
<DiscIcon />
Rilis
</CommandItem>
<CommandItem>
<BarChart3Icon />
Royalti
</CommandItem>
<CommandItem>
<FileTextIcon />
Dokumen
</CommandItem>
<CommandItem>
<SettingsIcon />
Pengaturan
<CommandShortcut>⌘,</CommandShortcut>
</CommandItem>
</CommandGroup>
</CommandList>
</Command>
)
}
Terminal window
npx shadcn@latest add https://design.sosialoka.tech/r/react/command.json
  • Tombol panah berpindah antaritem, Enter memilih, Escape menutup dialog.
  • Tampilkan pintasan (⌘K) di kartu pencarian supaya orang menemukannya.