Skip to content

Command

Command palette for keyboard-first navigation and search.

Chỉ dành cho Web

Component này chỉ khả dụng cho các framework web (Vue, React, Angular). Command palette dành cho bàn phím không phù hợp với nền tảng mobile.

Suggestions
CalendarCtrl+C
Search EmojiCtrl+E
CalculatorCtrl+K

Cài đặt

bash
npx @galaxy-stack/nebula-cli add command
bash
npx @galaxy-stack/nebula-cli add command
bash
npx @galaxy-stack/nebula-cli add command

Usage

tsx
import {
  Command,
  CommandDialog,
  CommandInput,
  CommandList,
  CommandEmpty,
  CommandGroup,
  CommandItem,
  CommandSeparator,
  CommandShortcut,
} from '@/components/command'

export default function CommandDemo() {
  return (
    <Command className="rounded-lg border shadow-md">
      <CommandInput placeholder="Type a command or search..." />
      <CommandList>
        <CommandEmpty>No results found.</CommandEmpty>
        <CommandGroup heading="Suggestions">
          <CommandItem>
            <span>Calendar</span>
          </CommandItem>
          <CommandItem>
            <span>Search Emoji</span>
          </CommandItem>
          <CommandItem>
            <span>Calculator</span>
          </CommandItem>
        </CommandGroup>
        <CommandSeparator />
        <CommandGroup heading="Settings">
          <CommandItem>
            <span>Profile</span>
            <CommandShortcut>⌘P</CommandShortcut>
          </CommandItem>
          <CommandItem>
            <span>Settings</span>
            <CommandShortcut>⌘S</CommandShortcut>
          </CommandItem>
        </CommandGroup>
      </CommandList>
    </Command>
  )
}
vue
<script setup lang="ts">
import { Command } from '@/components/command'
</script>

<template>
  <Command class="rounded-lg border shadow-md" placeholder="Type a command or search...">
    <template #default="{ search }">
      <div class="p-1">
        <div v-if="!search">No results found.</div>
        <div v-else>
          <div class="px-2 py-1.5 text-xs font-medium text-muted-foreground">
            Suggestions
          </div>
          <button class="relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none hover:bg-accent hover:text-accent-foreground">
            Calendar
          </button>
        </div>
      </div>
    </template>
  </Command>
</template>

API Reference

Props

PropKiểuMặc địnhMô tảFrameworks
valuestring-Giá trị tìm kiếm được kiểm soátReact, Vue, Angular
onValueChange(value: string) => void-Được gọi khi giá trị tìm kiếm thay đổiReact, Vue, Angular
labelstring-Nhãn truy cập cho command menuReact, Vue, Angular
loopbooleanfalseĐiều hướng bàn phím có lặp qua các item khôngReact, Vue, Angular
shouldFilterbooleantrueLọc tự động có được bật khôngReact, Vue, Angular
className / classstring""Tên CSS classReact (className), Vue/Angular (class)
placeholderstring-Văn bản giữ chỗ cho ô tìm kiếmVue, Angular

Sub-components

CommandDialog

PropKiểuMặc địnhMô tảFrameworks
openboolean-Trạng thái mở được kiểm soát cho dialogReact, Vue, Angular
defaultOpenbooleanfalseTrạng thái mở ban đầu cho chế độ uncontrolledReact, Vue, Angular
onOpenChange(open: boolean) => void-Được gọi khi trạng thái mở dialog thay đổiReact, Vue, Angular

CommandInput

PropKiểuMặc địnhMô tảFrameworks
placeholderstring-Văn bản giữ chỗTất cả
className / classstring-Tên CSS classReact (className), Vue/Angular (class)

CommandList

PropKiểuMặc địnhMô tảFrameworks
className / classstring-Tên CSS classReact (className), Vue/Angular (class)

CommandGroup

PropKiểuMặc địnhMô tảFrameworks
headingstring-Tiêu đề nhómTất cả
className / classstring-Tên CSS classReact (className), Vue/Angular (class)

CommandItem

PropKiểuMặc địnhMô tảFrameworks
disabledbooleanfalseVô hiệu hóa itemReact, Vue, Angular
onSelect(event: Event) => void-Được gọi khi item được chọnReact, Vue, Angular
className / classstring-Tên CSS classReact (className), Vue/Angular (class)

Components

  • Command - Container command chính
  • CommandInput - Ô tìm kiếm
  • CommandList - Danh sách các item có thể cuộn
  • CommandEmpty - Hiển thị khi không có kết quả
  • CommandGroup - Nhóm các item liên quan
  • CommandItem - Item command riêng lẻ
  • CommandSeparator - Dấu phân cách trực quan
  • CommandShortcut - Hiển thị phím tắt bàn phím

Examples

Command Dialog

tsx
const [open, setOpen] = useState(false)

useEffect(() => {
  const down = (e: KeyboardEvent) => {
    if (e.key === 'k' && (e.metaKey || e.ctrlKey)) {
      e.preventDefault()
      setOpen((open) => !open)
    }
  }
  document.addEventListener('keydown', down)
  return () => document.removeEventListener('keydown', down)
}, [])

return (
  <CommandDialog open={open} onOpenChange={setOpen}>
    <CommandInput placeholder="Type a command or search..." />
    <CommandList>
      <CommandEmpty>No results found.</CommandEmpty>
      <CommandGroup heading="Suggestions">
        <CommandItem>Calendar</CommandItem>
      </CommandGroup>
    </CommandList>
  </CommandDialog>
)

Accessibility

  • Keyboard navigation with arrow keys
  • Type-ahead search filtering
  • Proper ARIA labels and roles

Tác giả

Bùi Trọng Hiếu (kevinbui)

Giấy phép

MIT © 2025 Bùi Trọng Hiếu (kevinbui)

Phát hành theo Giấy phép MIT.