# Search composer

A search field with its focus scope inside it: web, academic, social or files, switchable from the keyboard, with recency at the end.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Search composer. A science reporter checks a reader's question: @academic turns into the scope, the past year narrows it, then it searches. Preview: https://chatcn.dev/preview/search-composer-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/search-composer
```

## Usage

```tsx
import { SearchComposer } from "@/components/ui/chatcn/pro/search-composer"

<SearchComposer
  scope={scope}
  onScopeChange={setScope}
  onSubmit={({ query, scope, recency }) => search(query, { scope, recency })}
  status={status}
  onStop={stop}
/>
```

The scope is a token at the start of the field, so what you are searching is never a setting hidden somewhere else. It sets the placeholder and the line under the field, and recency sits at the end for scopes that have it (files do not by default). Option or Alt with a number switches scope without leaving the text, and `@academic ` or `@papers ` typed at the start turns into the token. On a phone the query takes the first row and the scope, recency and Search share the second. `searchScopes` and `searchRecencies` are exported to extend or reorder; pass your own with `scopes` and `recencies`. Everything else is the free `PromptInput` in its one row form: IME safe Enter, paste, Stop in the send footprint.

## Examples

### States

Example: States. Each scope, a recency filter, files without recency, searching, a long query, a narrow field and disabled. Preview: https://chatcn.dev/preview/search-composer-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `scopes` | `SearchScope[]` | `{ id, label, icon?, placeholder?, description?, recency? }` |
| `scope`, `defaultScope`, `onScopeChange` | `string` | Defaults to the first scope |
| `recencies` | `SearchRecency[]` | `{ id, label, short? }`. The first means no filter |
| `recency`, `defaultRecency`, `onRecencyChange` | `string` |  |
| `onSubmit` | `({ query, scope, recency? }) => void` | `recency` is left out for scopes without it |
| `hideFootnote` | `boolean` | Drop the scope line under the field |
| `value`, `defaultValue`, `onValueChange` | `string` | The draft, controlled or not |
| `status`, `onStop` | `"idle" | "submitting" | "streaming"`, `() => void` | Send becomes Stop in the same footprint |
| `disabled` | `boolean` |  |
| `labels` | `Partial<SearchComposerLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Option 1 to 9, Alt 1 to 9 | Switch scope from the field |
| @ and a scope, then Space | Turn the word into the scope token |
| Enter | Search. Shift Enter adds a line |
| Escape | Stop a running search |

## Accessibility

- The scope token is a menu button named with the current scope, for example Search in: Academic.
- Scopes are radio items with their shortcut shown; picking one returns focus to the field.
- Switching scope from the keyboard is announced politely.
- The field's name includes the scope, so it is heard with every focus.

## Motion

Menus open from their trigger in 180 ms like every kit menu. Switching scope never animates because it is mostly done from the keyboard; the token and placeholder change in place.

Source: https://chatcn.dev/docs/components/search-composer