# Search Trigger

> A search field that opens your search instead of taking text: the topStart of the standard top bar, opening a CommandPalette.

Component · Navigation · Halo kit · [Live page](https://ui.twentyfirst.media/halo/components/SearchTrigger/)

A search field that opens your search instead of taking text: the `topStart` of the standard top
bar, opening a `CommandPalette`. It looks like `SearchField` and says it opens a dialog.

- `shortcut="⌘K"` shows the key hint (hidden on touch screens); `bindShortcut` opens it with
  ⌘/Ctrl + the hint's last key, except while the reader types in a field.
- On phones the top bar shows `topStartCompact` instead: pass an `IconButton` that opens the same
  search.

```tsx
<AppShell navigation="sidebar" topbar
  topStart={<SearchTrigger placeholder="Search…" shortcut="⌘K" bindShortcut onClick={() => setOpen(true)} />}
  topStartCompact={<IconButton icon="search" label="Search" variant="ghost" onClick={() => setOpen(true)} />}
  …
/>
<CommandPalette open={open} onClose={() => setOpen(false)} items={pages} />
```

## Example

```tsx
import * as React from "react";
import { CommandPalette, SearchTrigger } from "@twentyfirstmedia/halo";

export default function Preview() {
  const [open, setOpen] = React.useState(false);
  const [picked, setPicked] = React.useState("");
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 12, maxWidth: 420 }}>
      <SearchTrigger
        placeholder="Search orders, customers, pages…"
        shortcut="⌘K"
        bindShortcut
        onClick={() => setOpen(true)}
      />
      <p style={{ margin: 0, color: "var(--ink-muted)" }}>
        {picked ? `Opened ${picked}` : "Click it or press ⌘K."}
      </p>
      <CommandPalette
        open={open}
        onClose={() => setOpen(false)}
        placeholder="Search orders, customers, pages…"
        items={["Orders", "Customers", "Invoices", "Settings"].map((label) => ({
          group: "Pages",
          label,
          run: () => setPicked(label),
        }))}
      />
    </div>
  );
}
```

## Props

### SearchTrigger

| Prop | Type | Notes |
| --- | --- | --- |
| `placeholder?` | `string` | The text in the field (default "Search"). |
| `label?` | `string` | Accessible name instead of the visible text (keep the visible text in it). |
| `shortcut?` | `string` | The key hint, e.g. "⌘K" (hidden on touch screens). |
| `bindShortcut?` | `boolean` | ⌘/Ctrl + the hint's last key opens it too, except while typing in a field. |
| `onClick?` | `() => void` | Opens your search, e.g. a `CommandPalette`. |
| `className?` | `string` | Extra class on the root element. |
| `style?` | `CSSProperties` | Inline style on the root element. |

### CommandPalette

| Prop | Type | Notes |
| --- | --- | --- |
| `items` | `PaletteItem[]` | Required |
| `onClose?` | `() => void` |  |
| `placeholder?` | `string` |  |
| `inline?` | `boolean` |  |
| `loading?` | `boolean` | Results are on their way (async search): shows a loading row. |
| `empty?` | `ReactNode` | Shown when nothing matches (default: "Nothing matches …"). |
| `onQueryChange?` | `(query: string) => void` | Called with the query as the user types, for searching on a server. |
| `open?` | `boolean` | Optional: with `open` the component stays mounted while it animates out; without it, render it only while open. |
| `className?` | `string` | Extra class on the root element. |
| `style?` | `CSSProperties` | Inline style on the root element. |
