# Search

A search field and its results in one panel, to find and run a command.

Inline command search: a field and its results in one raised card. Typing filters (on the label and the item's keywords), the arrow keys move a highlight that glides between options, under the pointer and the keyboard alike, Enter selects, Escape clears. Behaviour, focus and ARIA come from the Zag combobox machine kept open, through Ark UI in React, Vue and Svelte. Rows can come in groups under a label, lead with an icon and end with a note and the command's own shortcut (the real Kbd); the command palette block is this panel in a bare dialog.

Kind: component · Category: Navigation · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `combobox` machine, through Ark UI

## Add

```bash
npx fndn add search
```

It writes the folder `search/` into the project's ui folder (`index.ts`, `search.tsx`, `Search.vue`, `Search.svelte`, `search.recipe.ts`), and what it is built from: [Button](/components/button.md), [Empty State](/components/empty-state.md), [Kbd](/components/kbd.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Search } from "@/components/ui/search";
```

```vue
import { Search } from "@/components/ui/search";
```

```svelte
import { Search } from "$lib/components/ui/search";
```

## Parts

In the DOM the root carries `data-slot="search"` and each part `data-slot="search-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `search` |  |  |
| `panel` | `div` | `search-panel` |  | Raised card holding the field and the results |
| `bar` | `div` | `search-bar` |  | Field row (the combobox control). Focus changes nothing on it: no ring, the rule stays line; the caret is the focus indicator. The clear button and the results keep the standard :focus-visible ring. |
| `icon` | `span` | `search-icon` |  | Magnifier; decorative, never intercepts the pointer |
| `input` | `input` | `search-input` |  | role=combobox; fills the row; aria-activedescendant follows the highlight |
| `clear` | `button` | `search-clear` |  | The real Button, marked data-search="clear" (ghost, xs, square, muted) with an x icon, the aria-label and our tooltip carrying the same text. Shown while the query is non-empty (hidden attribute otherwise); not a tab stop, Escape does the same |
| `list` | `div` | `search-list` |  | role=listbox; scrolls past 8 rows; hidden while the empty state shows |
| `highlight` | `span` | `search-highlight` |  | Shared gliding highlight layer (`glideLayer` of `lib/glide.ts`); it glides for arrow-key moves too |
| `group` | `div` | `search-group` | optional, repeats | role=group around the rows of one group (items with the same `group`), in the order the groups first appear; rows without a group are listed with no group around them. Labelled by its group label (aria-labelledby). The highlight glides between neighbouring rows and moves at once across a group label. |
| `group-label` | `div` | `search-group-label` | optional, repeats | The group's name: the shared option row's group label (2xs/500 ink-2); never highlighted, never chosen |
| `item` | `div` | `search-item` | repeats | role=option; data-highlighted, data-disabled. Only matching rows are in the DOM; composes the shared option row (`lib/option.ts`: 30px, text.dense (12.5px), gap 8) |
| `item-icon` | `span` | `search-item-icon` | optional | Leading icon (14px, ink-3), decorative (aria-hidden) |
| `item-text` | `span` | `search-item-text` |  | Truncates at the end; the shared option row's text (`lib/option.ts`) |
| `item-meta` | `span` | `search-item-meta` | optional | Trailing muted note (a path, a category); the shared option row's meta (`lib/option.ts`) |
| `item-shortcut` | `kbd` | `search-item-shortcut` | optional | The command's own shortcut at the end of the row: the real Kbd (boxed), marked data-search="item-shortcut". It shows the shortcut that runs the command outside the search; inside it Enter runs it. |
| `empty` | `div` | `search-empty` |  | The system's empty-state (size block, centred) marked data-search="empty": icon, title, hint. Shown when a non-empty query matches nothing (hidden attribute otherwise) |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `{ value: string; label: string; group: string? (rows with the same group are listed together under it); icon: icon? (React an element, Vue / Svelte a component); meta?: string; shortcut: string[]? (keys, as Kbd takes them); keywords: string[]? (more words the query matches); disabled?: boolean }[]` |  | Commands in order. Strings are accepted as value = label. A query matches the label or any keyword, anywhere in the word, ignoring case; matches are listed best first (the label starts with the query, then a word of it does, then the label holds it, then a keyword), ties in their own order, each group under its label in the order its best match comes. |
| `query` | `string` | `""` | Bindable (React: `defaultQuery` and `onQueryChange`; Vue: `v-model:query`; Svelte: `bind:query`). The text in the field. |
| `limit` | `number` | `5` | Rows shown while the query is empty. A non-empty query shows every match. |
| `placeholder` | `string` | `"Search…"` |  |
| `label` | `string` | `"Search"` | Accessible name of the field. |
| `clearLabel` | `string` | `"Clear search"` |  |
| `emptyTitle` | `string` | `"No results found"` |  |
| `emptyHint` | `string` | `"Adjust your search to try again"` |  |
| `highlightFirst` | `boolean` | `false` | The first row starts highlighted, so Enter runs it at once (a command palette). Without it nothing is highlighted until a key or the pointer moves the highlight, or a query highlights its first match. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `queryChange` | (query: string) |
| `select` | (value: string, item) when an option is chosen with Enter or a click; the field takes the option's label |

## States

`bar-hover`, `focus-within`, `highlighted`, `item-press`, `item-disabled`, `clear-hover`, `clear-press`, `empty`, `grouped`

## Keyboard and accessibility

Role: combobox + listbox.

Keyboard:

- Typing filters the list; the first match is highlighted
- ArrowDown / ArrowUp move the highlight in the order the rows are drawn (groups included; wraps); Home / End jump to the first and last option
- Enter selects the highlighted option; the field takes its label
- Escape clears the query and the selection
- Focus never leaves the field: options are reached through aria-activedescendant, and the clear button is not a tab stop

Notes:

- The field carries the accessible name (label); the list is announced as a listbox whose result count is reported by the engine's live region.
- The highlight glides for arrow-key moves as well as under the pointer; focus never leaves the field.
- On coarse pointers the field is 16px so iOS does not zoom the page, and rows are 40px tall.
- A group is role=group named by its label; the label is never an option. An item's shortcut is read with the option (it names the key that runs the command elsewhere).

## Motion

- highlight: glides between neighbouring options over duration.move, under the pointer and the keyboard; across a group label it moves at once (it never sweeps over the label)
- rows: no entrance animation, the list changes on every keystroke
- empty state: the empty-state's own opacity fade over duration.base when it appears
- clear button: fades in when it appears after mount (the button's opacity transition, armed by ready); a search mounted with a query shows it at once; press is the button's (scale to motion.press-scale over duration.press)
- bar: background over duration.fast

## Tokens

`color.surface`, `color.line`, `color.hover`, `color.ink`, `color.ink-2`, `color.ink-3`, `shadow.raised`, `radius.card`, `text.sm`, `duration.fast`, `duration.base`, `text.2xs`

## Dependencies

- [Button](/components/button.md)
- [Empty State](/components/empty-state.md)
- [Kbd](/components/kbd.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Search the workspace

React · `workspace.tsx`

```tsx
import { Search, type SearchItem } from "@/components/ui/search";

/* one list for everything: the meta says what kind of result a row is */
const items: SearchItem[] = [
  { value: "project:web-app", label: "web-app", meta: "Project" },
  { value: "person:priya", label: "Priya Raman", meta: "Member" },
  { value: "action:new-project", label: "Create a project", meta: "Action" },
  { value: "project:billing-api", label: "billing-api", meta: "Project" },
  { value: "person:daniel", label: "Daniel Okafor", meta: "Member" },
  { value: "project:data-pipeline", label: "data-pipeline", meta: "Project" },
  { value: "action:invite", label: "Invite teammates", meta: "Action" },
  { value: "action:api-key", label: "Create an API key", meta: "Action" },
];

export default function Workspace() {
  return (
    <Search
      className="w-80"
      items={items}
      limit={3}
      label="Search Northwind"
      placeholder="Search projects, people and actions…"
    />
  );
}
```

Vue · `workspace.vue`

```vue
<script setup lang="ts">
import { Search, type SearchItem } from "@/components/ui/search";

/* one list for everything: the meta says what kind of result a row is */
const items: SearchItem[] = [
  { value: "project:web-app", label: "web-app", meta: "Project" },
  { value: "person:priya", label: "Priya Raman", meta: "Member" },
  { value: "action:new-project", label: "Create a project", meta: "Action" },
  { value: "project:billing-api", label: "billing-api", meta: "Project" },
  { value: "person:daniel", label: "Daniel Okafor", meta: "Member" },
  { value: "project:data-pipeline", label: "data-pipeline", meta: "Project" },
  { value: "action:invite", label: "Invite teammates", meta: "Action" },
  { value: "action:api-key", label: "Create an API key", meta: "Action" },
];
</script>

<template>
  <Search
    class="w-80"
    :items="items"
    :limit="3"
    label="Search Northwind"
    placeholder="Search projects, people and actions…"
  />
</template>
```

Svelte · `workspace.svelte`

```svelte
<script lang="ts">
  import { Search, type SearchItem } from "$lib/components/ui/search";

  /* one list for everything: the meta says what kind of result a row is */
  const items: SearchItem[] = [
    { value: "project:web-app", label: "web-app", meta: "Project" },
    { value: "person:priya", label: "Priya Raman", meta: "Member" },
    { value: "action:new-project", label: "Create a project", meta: "Action" },
    { value: "project:billing-api", label: "billing-api", meta: "Project" },
    { value: "person:daniel", label: "Daniel Okafor", meta: "Member" },
    { value: "project:data-pipeline", label: "data-pipeline", meta: "Project" },
    { value: "action:invite", label: "Invite teammates", meta: "Action" },
    { value: "action:api-key", label: "Create an API key", meta: "Action" },
  ];
</script>

<Search
  class="w-80"
  {items}
  limit={3}
  label="Search Northwind"
  placeholder="Search projects, people and actions…"
/>
```

### Commands in groups, with shortcuts

React · `grouped-commands.tsx`

```tsx
import { Gear, GitBranch, Moon, RocketLaunch, UserPlus } from "@phosphor-icons/react";
import { Search, type SearchItem } from "@/components/ui/search";

/* groups keep each kind of result together; a row can lead with an icon, end with the command's
 * shortcut, and be found by its keywords as well as its name */
const items: SearchItem[] = [
  { value: "project:web-app", label: "web-app", group: "Projects", meta: "Production", icon: <GitBranch data-slot="icon" weight="bold" /> },
  { value: "project:billing-api", label: "billing-api", group: "Projects", meta: "Staging", icon: <GitBranch data-slot="icon" weight="bold" /> },
  { value: "deploy", label: "Deploy to Production", group: "Actions", shortcut: ["⌘", "D"], keywords: ["ship", "release"], icon: <RocketLaunch data-slot="icon" weight="bold" /> },
  { value: "invite", label: "Invite teammates", group: "Actions", shortcut: ["⌘", "I"], icon: <UserPlus data-slot="icon" weight="bold" /> },
  { value: "settings", label: "Workspace settings", group: "Settings", keywords: ["billing", "members"], icon: <Gear data-slot="icon" weight="bold" /> },
  { value: "theme", label: "Switch to dark theme", group: "Settings", keywords: ["appearance", "night"], icon: <Moon data-slot="icon" weight="bold" /> },
];

export default function GroupedCommands() {
  return <Search className="w-80" items={items} limit={6} label="Search Northwind" placeholder="Search projects and commands…" />;
}
```

Vue · `grouped-commands.vue`

```vue
<script setup lang="ts">
import { PhGear, PhGitBranch, PhMoon, PhRocketLaunch, PhUserPlus } from "@phosphor-icons/vue";
import { Search, type SearchItem } from "@/components/ui/search";

/* groups keep each kind of result together; a row can lead with an icon, end with the command's
 * shortcut, and be found by its keywords as well as its name */
const items: SearchItem[] = [
  { value: "project:web-app", label: "web-app", group: "Projects", meta: "Production", icon: PhGitBranch },
  { value: "project:billing-api", label: "billing-api", group: "Projects", meta: "Staging", icon: PhGitBranch },
  { value: "deploy", label: "Deploy to Production", group: "Actions", shortcut: ["⌘", "D"], keywords: ["ship", "release"], icon: PhRocketLaunch },
  { value: "invite", label: "Invite teammates", group: "Actions", shortcut: ["⌘", "I"], icon: PhUserPlus },
  { value: "settings", label: "Workspace settings", group: "Settings", keywords: ["billing", "members"], icon: PhGear },
  { value: "theme", label: "Switch to dark theme", group: "Settings", keywords: ["appearance", "night"], icon: PhMoon },
];
</script>

<template>
  <Search class="w-80" :items="items" :limit="6" label="Search Northwind" placeholder="Search projects and commands…" />
</template>
```

Svelte · `grouped-commands.svelte`

```svelte
<script lang="ts">
  import Gear from "phosphor-svelte/lib/GearIcon";
  import GitBranch from "phosphor-svelte/lib/GitBranchIcon";
  import Moon from "phosphor-svelte/lib/MoonIcon";
  import RocketLaunch from "phosphor-svelte/lib/RocketLaunchIcon";
  import UserPlus from "phosphor-svelte/lib/UserPlusIcon";
  import { Search, type SearchItem } from "$lib/components/ui/search";

  /* groups keep each kind of result together; a row can lead with an icon, end with the command's
   * shortcut, and be found by its keywords as well as its name */
  const items: SearchItem[] = [
    { value: "project:web-app", label: "web-app", group: "Projects", meta: "Production", icon: GitBranch },
    { value: "project:billing-api", label: "billing-api", group: "Projects", meta: "Staging", icon: GitBranch },
    { value: "deploy", label: "Deploy to Production", group: "Actions", shortcut: ["⌘", "D"], keywords: ["ship", "release"], icon: RocketLaunch },
    { value: "invite", label: "Invite teammates", group: "Actions", shortcut: ["⌘", "I"], icon: UserPlus },
    { value: "settings", label: "Workspace settings", group: "Settings", keywords: ["billing", "members"], icon: Gear },
    { value: "theme", label: "Switch to dark theme", group: "Settings", keywords: ["appearance", "night"], icon: Moon },
  ];
</script>

<Search class="w-80" {items} limit={6} label="Search Northwind" placeholder="Search projects and commands…" />
```

### Run an action, one not allowed

React · `run-an-action.tsx`

```tsx
import { useState } from "react";
import { Search, type SearchItem } from "@/components/ui/search";

/* the meta carries the shortcut; an action you may not take stays listed, disabled */
const actions: SearchItem[] = [
  { value: "Deploy web-app to Production", label: "Deploy web-app to Production", meta: "⌘D" },
  { value: "Roll back to dpl_7f3a9c", label: "Roll back to dpl_7f3a9c" },
  { value: "Copy the latest commit", label: "Copy the latest commit", meta: "⌘⇧C" },
  { value: "Delete workspace", label: "Delete workspace", meta: "Owner only", disabled: true },
];

export default function RunAnAction() {
  const [ran, setRan] = useState("");
  return (
    <div className="flex w-80 flex-col gap-2">
      <Search items={actions} label="Run an action" placeholder="Run an action…" onSelect={setRan} />
      <p className="text-xs text-ink-2">{ran ? `Started: ${ran}` : "Pick an action, or type to filter."}</p>
    </div>
  );
}
```

Vue · `run-an-action.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Search, type SearchItem } from "@/components/ui/search";

/* the meta carries the shortcut; an action you may not take stays listed, disabled */
const actions: SearchItem[] = [
  { value: "Deploy web-app to Production", label: "Deploy web-app to Production", meta: "⌘D" },
  { value: "Roll back to dpl_7f3a9c", label: "Roll back to dpl_7f3a9c" },
  { value: "Copy the latest commit", label: "Copy the latest commit", meta: "⌘⇧C" },
  { value: "Delete workspace", label: "Delete workspace", meta: "Owner only", disabled: true },
];

const ran = ref("");
</script>

<template>
  <div class="flex w-80 flex-col gap-2">
    <Search :items="actions" label="Run an action" placeholder="Run an action…" @select="(value) => (ran = value)" />
    <p class="text-xs text-ink-2">{{ ran ? `Started: ${ran}` : "Pick an action, or type to filter." }}</p>
  </div>
</template>
```

Svelte · `run-an-action.svelte`

```svelte
<script lang="ts">
  import { Search, type SearchItem } from "$lib/components/ui/search";

  /* the meta carries the shortcut; an action you may not take stays listed, disabled */
  const actions: SearchItem[] = [
    { value: "Deploy web-app to Production", label: "Deploy web-app to Production", meta: "⌘D" },
    { value: "Roll back to dpl_7f3a9c", label: "Roll back to dpl_7f3a9c" },
    { value: "Copy the latest commit", label: "Copy the latest commit", meta: "⌘⇧C" },
    { value: "Delete workspace", label: "Delete workspace", meta: "Owner only", disabled: true },
  ];

  let ran = $state("");
</script>

<div class="flex w-80 flex-col gap-2">
  <Search items={actions} label="Run an action" placeholder="Run an action…" onselect={(value) => (ran = value)} />
  <p class="text-xs text-ink-2">{ran ? `Started: ${ran}` : "Pick an action, or type to filter."}</p>
</div>
```

### Nothing matches

React · `no-results.tsx`

```tsx
import { Search } from "@/components/ui/search";

const items = ["web-app", "billing-api", "mobile", "docs-site", "data-pipeline"];

export default function NoResults() {
  return (
    <Search
      className="w-80"
      items={items}
      defaultQuery="kubernetes"
      label="Search projects"
      placeholder="Search projects…"
      emptyTitle="No projects match"
      emptyHint="Search by project name, like web-app"
    />
  );
}
```

Vue · `no-results.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Search } from "@/components/ui/search";

const items = ["web-app", "billing-api", "mobile", "docs-site", "data-pipeline"];
const query = ref("kubernetes");
</script>

<template>
  <Search
    v-model:query="query"
    class="w-80"
    :items="items"
    label="Search projects"
    placeholder="Search projects…"
    emptyTitle="No projects match"
    emptyHint="Search by project name, like web-app"
  />
</template>
```

Svelte · `no-results.svelte`

```svelte
<script lang="ts">
  import { Search } from "$lib/components/ui/search";

  const items = ["web-app", "billing-api", "mobile", "docs-site", "data-pipeline"];
  let query = $state("kubernetes");
</script>

<Search
  bind:query
  class="w-80"
  {items}
  label="Search projects"
  placeholder="Search projects…"
  emptyTitle="No projects match"
  emptyHint="Search by project name, like web-app"
/>
```

## Recipe

`search.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { option } from "../lib/option";
import { glideLayer } from "../lib/glide";

/* search — inline command search: the field and its results share one raised card.
 * Behaviour comes from the Zag combobox machine, kept open (role=combobox on the field,
 * role=listbox on the list, data-highlighted on the option the keyboard or pointer is on).
 *
 * Focus: the field shows no ring and the row's rule stays `line`; the blinking caret
 * is the focus indicator. The row only drops its hover tint while you type in it: a field focused
 * at mount changes nothing, so nothing plays on first paint.
 *
 * Rows may come in groups, each under its label (the option row's group label); a row may lead
 * with an icon and end with a note (meta) and a keyboard shortcut (the real Kbd, boxed). The
 * highlight glides between neighbouring rows and moves at once across a group label.
 *
 * Results change on every keystroke, so they never animate in; one highlight (the shared
 * glideLayer of lib/glide.ts) glides between them, under the pointer and the keyboard alike. Rows compose the
 * shared option row (lib/option.ts). The clear button is a Button (ghost, xs, square, muted) and the
 * empty state is the system's EmptyState (block, centred), marked data-search="clear|empty"; only
 * their placement is passed here. */

const row = option();

export const search = tv({
  slots: {
    base: "flex w-full max-w-[288px] flex-col items-stretch",
    panel: "w-full overflow-hidden rounded-card bg-surface shadow-raised",
    /* field row — the input fills it, so a click anywhere in the row focuses the field. With no
     * rows and the empty state hidden too (nothing to list at all) its rule has nothing to divide. */
    bar: [
      "relative flex h-10 items-center border-b border-line motion-colors-fast",
      "hover:not-focus-within:bg-hover",
      "[&:has(~[data-slot=search-list][data-empty]~[data-slot=empty-state][hidden])]:border-b-transparent",
    ],
    /* the magnifier: decorative, never intercepts the pointer */
    icon: "absolute inset-y-0 start-3 my-auto shrink-0 size-[1em] text-[14px] leading-none align-[-0.125em] text-ink-3 pointer-events-none",
    /* the field takes the font of the page (font: inherit, written out so the size below wins);
     * under 16px iOS zooms the page when the field takes focus */
    input: [
      "w-full min-w-0 h-full m-0 border-0 py-0 ps-[34px] pe-10 bg-transparent text-ink",
      "[font-family:inherit] [font-style:inherit] [font-weight:inherit] [font-stretch:inherit] [font-variant:inherit] leading-[inherit]",
      "text-sm tracking-[inherit] text-start outline-none placeholder:text-ink-3",
      "pointer-coarse:text-[16px]",
    ],
    /* the clear Button at the end of the row; it fades in as it appears after mount (ready), and
     * is simply there when the search mounts with a query */
    clear: "absolute inset-y-0 end-2 my-auto",
    /* no rows (the engine marks the list data-empty): the list takes no room */
    list: [
      "relative flex flex-col max-h-[268px] overflow-y-auto overscroll-x-contain p-1 outline-none",
      "data-empty:p-0 pointer-coarse:max-h-[332px]",
    ],
    highlight: [glideLayer, "inset-x-1"],
    /* a run of rows under one label (role=group); the label never takes the highlight */
    group: "flex flex-col",
    groupLabel: row.label(),
    item: [row.base(), "shrink-0"],
    itemIcon: row.icon(),
    itemText: row.text(),
    itemMeta: row.meta(),
    /* the shortcut is the real Kbd (boxed), marked data-search="item-shortcut"; only its place here */
    itemShortcut: "shrink-0",
    /* the empty state is the real EmptyState (data-search="empty"), which styles itself */
    empty: "",
  },
  variants: {
    /* set after the first render: the clear button's fade-in is armed, so a search mounted with a
     * query shows it at once (nothing plays on first paint) */
    ready: { true: { clear: "starting:opacity-0" } },
  },
});

export type SearchRecipeProps = VariantProps<typeof search>;
```
