커맨드 (Command)
검색과 빠른 작업 실행을 위한 커맨드 메뉴예요.
출처: 문서
본문
<script lang="ts">
import CalculatorIcon from "@lucide/svelte/icons/calculator";
import CalendarIcon from "@lucide/svelte/icons/calendar";
import CreditCardIcon from "@lucide/svelte/icons/credit-card";
import SettingsIcon from "@lucide/svelte/icons/settings";
import SmileIcon from "@lucide/svelte/icons/smile";
import UserIcon from "@lucide/svelte/icons/user";
import * as Command from "$lib/components/ui/command/index.js";
</script>
<Command.Root class="max-w-sm rounded-lg border">
<Command.Input placeholder="Type a command or search..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group heading="Suggestions">
<Command.Item>
<CalendarIcon />
<span>Calendar</span>
</Command.Item>
<Command.Item>
<SmileIcon />
<span>Search Emoji</span>
</Command.Item>
<Command.Item disabled>
<CalculatorIcon />
<span>Calculator</span>
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Settings">
<Command.Item>
<UserIcon />
<span>Profile</span>
<Command.Shortcut>P</Command.Shortcut>
</Command.Item>
<Command.Item>
<CreditCardIcon />
<span>Billing</span>
<Command.Shortcut>B</Command.Shortcut>
</Command.Item>
<Command.Item>
<SettingsIcon />
<span>Settings</span>
<Command.Shortcut>S</Command.Shortcut>
</Command.Item>
</Command.Group>
</Command.List>
</Command.Root>
View Code
소개
<Command /> 컴포넌트는 cmdk에서 영감을 받은 Bits UI의 Command 컴포넌트를 사용해요.
설치
pnpm dlx shadcn-svelte@latest add command
npx shadcn-svelte@latest add command
bun x shadcn-svelte@latest add command
사용법
<script lang="ts">
import * as Command from "$lib/components/ui/command/index.js";
</script>
<Command.Root>
<Command.Input placeholder="Type a command or search..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group heading="Suggestions">
<Command.Item>Calendar</Command.Item>
<Command.Item>Search Emoji</Command.Item>
<Command.Item>Calculator</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Settings">
<Command.Item>Profile</Command.Item>
<Command.Item>Billing</Command.Item>
<Command.Item>Settings</Command.Item>
</Command.Group>
</Command.List>
</Command.Root>
Basic
다이얼로그 안에 띄우는 간단한 커맨드 메뉴예요.
<script lang="ts">
import * as Command from "$lib/components/ui/command/index.js";
import { Button } from "$lib/components/ui/button/index.js";
let open = $state(false);
</script>
<div class="flex flex-col gap-4">
<Button onclick={() => (open = true)} variant="outline" class="w-fit"
>Open Menu</Button
>
<Command.Dialog bind:open>
<Command.Input placeholder="Type a command or search..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group heading="Suggestions">
<Command.Item>Calendar</Command.Item>
<Command.Item>Search Emoji</Command.Item>
<Command.Item>Calculator</Command.Item>
</Command.Group>
</Command.List>
</Command.Dialog>
</div>
View Code
Shortcuts
<script lang="ts">
import CreditCardIcon from "@lucide/svelte/icons/credit-card";
import SettingsIcon from "@lucide/svelte/icons/settings";
import UserIcon from "@lucide/svelte/icons/user";
import * as Command from "$lib/components/ui/command/index.js";
import { Button } from "$lib/components/ui/button/index.js";
let open = $state(false);
</script>
<div class="flex flex-col gap-4">
<Button onclick={() => (open = true)} variant="outline" class="w-fit"
>Open Menu</Button
>
<Command.Dialog bind:open>
<Command.Input placeholder="Type a command or search..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group heading="Settings">
<Command.Item>
<UserIcon />
<span>Profile</span>
<Command.Shortcut>P</Command.Shortcut>
</Command.Item>
<Command.Item>
<CreditCardIcon />
<span>Billing</span>
<Command.Shortcut>B</Command.Shortcut>
</Command.Item>
<Command.Item>
<SettingsIcon />
<span>Settings</span>
<Command.Shortcut>S</Command.Shortcut>
</Command.Item>
</Command.Group>
</Command.List>
</Command.Dialog>
</div>
View Code
Groups
그룹, 아이콘, 구분선이 함께 있는 커맨드 메뉴예요.
<script lang="ts">
import CalculatorIcon from "@lucide/svelte/icons/calculator";
import CalendarIcon from "@lucide/svelte/icons/calendar";
import CreditCardIcon from "@lucide/svelte/icons/credit-card";
import SettingsIcon from "@lucide/svelte/icons/settings";
import SmileIcon from "@lucide/svelte/icons/smile";
import UserIcon from "@lucide/svelte/icons/user";
import * as Command from "$lib/components/ui/command/index.js";
import { Button } from "$lib/components/ui/button/index.js";
let open = $state(false);
</script>
<div class="flex flex-col gap-4">
<Button onclick={() => (open = true)} variant="outline" class="w-fit"
>Open Menu</Button
>
<Command.Dialog bind:open>
<Command.Input placeholder="Type a command or search..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group heading="Suggestions">
<Command.Item>
<CalendarIcon />
<span>Calendar</span>
</Command.Item>
<Command.Item>
<SmileIcon />
<span>Search Emoji</span>
</Command.Item>
<Command.Item>
<CalculatorIcon />
<span>Calculator</span>
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Settings">
<Command.Item>
<UserIcon />
<span>Profile</span>
<Command.Shortcut>P</Command.Shortcut>
</Command.Item>
<Command.Item>
<CreditCardIcon />
<span>Billing</span>
<Command.Shortcut>B</Command.Shortcut>
</Command.Item>
<Command.Item>
<SettingsIcon />
<span>Settings</span>
<Command.Shortcut>S</Command.Shortcut>
</Command.Item>
</Command.Group>
</Command.List>
</Command.Dialog>
</div>
View Code
Scrollable
여러 아이템을 담은 스크롤 가능한 커맨드 메뉴예요.
<script lang="ts">
import BellIcon from "@lucide/svelte/icons/bell";
import CalculatorIcon from "@lucide/svelte/icons/calculator";
import CalendarIcon from "@lucide/svelte/icons/calendar";
import CircleQuestionMarkIcon from "@lucide/svelte/icons/circle-question-mark";
import ClipboardPasteIcon from "@lucide/svelte/icons/clipboard-paste";
import CodeIcon from "@lucide/svelte/icons/code";
import CopyIcon from "@lucide/svelte/icons/copy";
import CreditCardIcon from "@lucide/svelte/icons/credit-card";
import FileTextIcon from "@lucide/svelte/icons/file-text";
import FolderIcon from "@lucide/svelte/icons/folder";
import FolderPlusIcon from "@lucide/svelte/icons/folder-plus";
import HouseIcon from "@lucide/svelte/icons/house";
import ImageIcon from "@lucide/svelte/icons/image";
import InboxIcon from "@lucide/svelte/icons/inbox";
import LayoutGridIcon from "@lucide/svelte/icons/layout-grid";
import ListIcon from "@lucide/svelte/icons/list";
import PlusIcon from "@lucide/svelte/icons/plus";
import ScissorsIcon from "@lucide/svelte/icons/scissors";
import SettingsIcon from "@lucide/svelte/icons/settings";
import TrashIcon from "@lucide/svelte/icons/trash";
import UserIcon from "@lucide/svelte/icons/user";
import ZoomInIcon from "@lucide/svelte/icons/zoom-in";
import ZoomOutIcon from "@lucide/svelte/icons/zoom-out";
import * as Command from "$lib/components/ui/command/index.js";
import { Button } from "$lib/components/ui/button/index.js";
let open = $state(false);
</script>
<div class="flex flex-col gap-4">
<Button onclick={() => (open = true)} variant="outline" class="w-fit"
>Open Menu</Button
>
<Command.Dialog bind:open>
<Command.Input placeholder="Type a command or search..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group heading="Navigation">
<Command.Item>
<HouseIcon />
<span>Home</span>
<Command.Shortcut>H</Command.Shortcut>
</Command.Item>
<Command.Item>
<InboxIcon />
<span>Inbox</span>
<Command.Shortcut>I</Command.Shortcut>
</Command.Item>
<Command.Item>
<FileTextIcon />
<span>Documents</span>
<Command.Shortcut>D</Command.Shortcut>
</Command.Item>
<Command.Item>
<FolderIcon />
<span>Folders</span>
<Command.Shortcut>F</Command.Shortcut>
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Actions">
<Command.Item>
<PlusIcon />
<span>New File</span>
<Command.Shortcut>N</Command.Shortcut>
</Command.Item>
<Command.Item>
<FolderPlusIcon />
<span>New Folder</span>
<Command.Shortcut>N</Command.Shortcut>
</Command.Item>
<Command.Item>
<CopyIcon />
<span>Copy</span>
<Command.Shortcut>C</Command.Shortcut>
</Command.Item>
<Command.Item>
<ScissorsIcon />
<span>Cut</span>
<Command.Shortcut>X</Command.Shortcut>
</Command.Item>
<Command.Item>
<ClipboardPasteIcon />
<span>Paste</span>
<Command.Shortcut>V</Command.Shortcut>
</Command.Item>
<Command.Item>
<TrashIcon />
<span>Delete</span>
<Command.Shortcut></Command.Shortcut>
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="View">
<Command.Item>
<LayoutGridIcon />
<span>Grid View</span>
</Command.Item>
<Command.Item>
<ListIcon />
<span>List View</span>
</Command.Item>
<Command.Item>
<ZoomInIcon />
<span>Zoom In</span>
<Command.Shortcut>+</Command.Shortcut>
</Command.Item>
<Command.Item>
<ZoomOutIcon />
<span>Zoom Out</span>
<Command.Shortcut>-</Command.Shortcut>
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Account">
<Command.Item>
<UserIcon />
<span>Profile</span>
<Command.Shortcut>P</Command.Shortcut>
</Command.Item>
<Command.Item>
<CreditCardIcon />
<span>Billing</span>
<Command.Shortcut>B</Command.Shortcut>
</Command.Item>
<Command.Item>
<SettingsIcon />
<span>Settings</span>
<Command.Shortcut>S</Command.Shortcut>
</Command.Item>
<Command.Item>
<BellIcon />
<span>Notifications</span>
</Command.Item>
<Command.Item>
<CircleQuestionMarkIcon />
<span>Help & Support</span>
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Tools">
<Command.Item>
<CalculatorIcon />
<span>Calculator</span>
</Command.Item>
<Command.Item>
<CalendarIcon />
<span>Calendar</span>
</Command.Item>
<Command.Item>
<ImageIcon />
<span>Image Editor</span>
</Command.Item>
<Command.Item>
<CodeIcon />
<span>Code Editor</span>
</Command.Item>
</Command.Group>
</Command.List>
</Command.Dialog>
</div>
View Code
더 알아보기 (Learn more)
원문 문서에서는 bits-ui의 Command 문서와 API 레퍼런스 링크로 컴포넌트의 동작 원리와 전체 prop 목록을 더 살펴볼 수 있어요. 설치 방법이나 다른 컴포넌트와의 조합이 궁금하면 shadcn-svelte 공식 문서를 참고해 보세요.