# Combobox

> Pick one option from a list you can filter by typing.

Page: https://uicontext.com/docs/components/combobox

Group: Overlays.

## When to use

Pick one option from a list you can filter by typing. Use [Multi-Select](https://uicontext.com/docs/components/multi-select.md) instead when people pick more than one.

## Usage

```ts
import { Picker } from "./ui/uicontext/components/Picker.ts"

const Message = UiSchema.TaggedEnum({ PickedStatus: { value: Schema.String } })

export const StatusPicker = (h: Html.H<typeof Message.Type>, status: string) =>
	Picker(h, {
		props: { name: "Status", options: statuses, selected: status, empty: statuses[0]! },
		onEmit: { RequestedChange: ({ value }) => Message.PickedStatus({ value }) },
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### An issue's properties

The example the page opens with: Combobox in “An issue's properties”.

```ts title="src/demo.ts" source="www/src/examples/combobox/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Picker } from "./ui/uicontext/components/Picker.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"
import { people, priorities, statuses, unassigned } from "./options.ts"

const styles = stylex.create({
	// a properties panel: a label column and a picker column
	panel: {
		display: "grid",
		gridTemplateColumns: "6rem minmax(0, 1fr)",
		alignItems: "center",
		columnGap: space.md,
		rowGap: space.xs,
		inlineSize: "min(100%, 20rem)",
	},
	label: { fontSize: font.sm, color: color.muted },
})

const Message = UiSchema.TaggedEnum({
	PickedStatus: { value: Schema.String },
	PickedPriority: { value: Schema.String },
	PickedAssignee: { value: Schema.String },
})

export const IssueProperties = Html.component({
	name: "IssueProperties",
	model: Schema.Struct({ status: Schema.String, priority: Schema.String, assignee: Schema.NullOr(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ status: "doing", priority: "high", assignee: null }),
	update: {
		PickedStatus: ($, { value }) => $.evolve({ status: value }),
		PickedPriority: ($, { value }) => $.evolve({ priority: value }),
		PickedAssignee: ($, { value }) => $.evolve({ assignee: value }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.panel)).children(
			h.span.attrs(sx(styles.label)).children("Status"),
			Picker(h, {
				key: "status",
				props: { agentId: "issue-eng-142-status", name: "Status", options: statuses, selected: model.status, empty: statuses[0]! },
				onEmit: { RequestedChange: ({ value }) => Message.PickedStatus({ value }) },
			}),
			h.span.attrs(sx(styles.label)).children("Priority"),
			Picker(h, {
				key: "priority",
				props: { agentId: "issue-eng-142-priority", name: "Priority", options: priorities, selected: model.priority, empty: priorities[0]! },
				onEmit: { RequestedChange: ({ value }) => Message.PickedPriority({ value }) },
			}),
			h.span.attrs(sx(styles.label)).children("Assignee"),
			Picker(h, {
				key: "assignee",
				props: { agentId: "issue-eng-142-assignee", name: "Assignee", options: people, selected: model.assignee, empty: unassigned },
				onEmit: { RequestedChange: ({ value }) => Message.PickedAssignee({ value }) },
			}),
		),
})
```

### Compact, in a list

With `compact` only the glyph shows, for a row's status or priority. Its accessible name still says the property and its value.

```ts title="src/in-a-row.ts" source="www/src/examples/combobox/in-a-row.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Picker } from "./ui/uicontext/components/Picker.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"
import { priorities, statuses } from "./options.ts"

const styles = stylex.create({
	list: { display: "grid", inlineSize: "min(100%, 30rem)", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card },
	row: {
		display: "flex",
		// a grid item sizes to its nowrap title unless told it may be narrower
		minInlineSize: 0,
		alignItems: "center",
		gap: space.xs,
		paddingInline: space.md,
		minBlockSize: 40,
		borderTopWidth: { default: 1, ":first-child": 0 },
		borderTopStyle: "solid",
		borderTopColor: color.line,
	},
	id: { flexShrink: 0, inlineSize: "4.5rem", fontSize: font.sm, color: color.faint },
	title: { flexGrow: 1, minInlineSize: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", fontSize: font.base },
})

const Issue = Schema.Struct({ id: Schema.String, title: Schema.String, status: Schema.String, priority: Schema.String })
type Issue = typeof Issue.Type

const issues: ReadonlyArray<Issue> = [
	{ id: "ENG-142", title: "Sign-in link expires before the email arrives", status: "doing", priority: "urgent" },
	{ id: "ENG-139", title: "Export to CSV drops the last row", status: "todo", priority: "medium" },
	{ id: "ENG-131", title: "Webhook retries ignore the backoff setting", status: "review", priority: "low" },
]

const Message = UiSchema.TaggedEnum({
	PickedStatus: { id: Schema.String, value: Schema.String },
	PickedPriority: { id: Schema.String, value: Schema.String },
})

export const IssueRows = Html.component({
	name: "IssueRows",
	model: Schema.Struct({ issues: Schema.Array(Issue) }),
	message: Message,
}).make({
	init: () => ({ issues }),
	update: {
		PickedStatus: ($, { id, value }) => $.evolve({ issues: (list) => list.map((i) => (i.id === id ? { ...i, status: value } : i)) }),
		PickedPriority: ($, { id, value }) => $.evolve({ issues: (list) => list.map((i) => (i.id === id ? { ...i, priority: value } : i)) }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.list)).children(
			model.issues.map((issue) =>
				h.div.key(issue.id).attrs(sx(styles.row)).children(
					// compact: only the glyph shows; the name and value are its accessible name
					Picker(h, {
						key: `priority:${issue.id}`,
						props: { agentId: `priority:${issue.id}`, name: "Priority", options: priorities, selected: issue.priority, empty: priorities[0]!, compact: true },
						onEmit: { RequestedChange: ({ value }) => Message.PickedPriority({ id: issue.id, value }) },
					}),
					h.span.attrs(sx(styles.id)).children(issue.id),
					Picker(h, {
						key: `status:${issue.id}`,
						props: { agentId: `status:${issue.id}`, name: "Status", options: statuses, selected: issue.status, empty: statuses[0]!, compact: true },
						onEmit: { RequestedChange: ({ value }) => Message.PickedStatus({ id: issue.id, value }) },
					}),
					h.span.attrs(sx(styles.title)).children(issue.title),
				)),
		),
})
```

### Read-only and disabled

`readOnly` opens the list to show the choices without picking; `disabled` doesn't open at all.

```ts title="src/locked.ts" source="www/src/examples/combobox/locked.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Picker } from "./ui/uicontext/components/Picker.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"
import { people, statuses, unassigned } from "./options.ts"

const styles = stylex.create({
	panel: {
		display: "grid",
		gridTemplateColumns: "6rem minmax(0, 1fr)",
		alignItems: "center",
		columnGap: space.md,
		rowGap: space.xs,
		inlineSize: "min(100%, 20rem)",
	},
	label: { fontSize: font.sm, color: color.muted },
})

const Message = UiSchema.TaggedEnum({ PickedStatus: { value: Schema.String } })

export const LockedProperties = Html.component({
	name: "LockedProperties",
	model: Schema.Struct({}),
	message: Message,
}).make({
	init: () => ({}),
	update: { PickedStatus: ($) => $.model },
	view: (h) =>
		h.div.attrs(sx(styles.panel)).children(
			h.span.attrs(sx(styles.label)).children("Status"),
			// a guest can open the list to see the choices, but not pick one
			Picker(h, {
				key: "status",
				props: { agentId: "locked-status", name: "Status", options: statuses, selected: "review", empty: statuses[0]!, readOnly: true },
				onEmit: { RequestedChange: ({ value }) => Message.PickedStatus({ value }) },
			}),
			h.span.attrs(sx(styles.label)).children("Assignee"),
			// the issue is archived: nothing to open
			Picker(h, {
				key: "assignee",
				props: { agentId: "locked-assignee", name: "Assignee", options: people, selected: "omar", empty: unassigned, disabled: true },
				onEmit: { RequestedChange: ({ value }) => Message.PickedStatus({ value }) },
			}),
		),
})
```

### Sample data

The examples above import these files.

```ts title="src/options.ts" source="www/src/examples/combobox/options.ts"
import type { PickerOption } from "./ui/uicontext/components/Picker.ts"
import { color } from "./ui/uicontext/components/tokens.stylex.ts"

// An issue's status, priority and assignee, as an issue tracker defines them.

export const statuses: ReadonlyArray<PickerOption> = [
	{ value: "backlog", label: "Backlog", glyph: { _tag: "Ring", color: color.muted, progress: 0, dashed: true, label: "Backlog" } },
	{ value: "todo", label: "Todo", glyph: { _tag: "Ring", color: color.muted, progress: 0, label: "Todo" } },
	{ value: "doing", label: "In progress", glyph: { _tag: "Ring", color: color.caution, progress: 0.5, label: "In progress" } },
	{ value: "review", label: "In review", glyph: { _tag: "Ring", color: color.info, progress: 0.75, label: "In review" } },
	{ value: "done", label: "Done", glyph: { _tag: "Ring", color: color.done, progress: 1, mark: "check", label: "Done" } },
	{ value: "canceled", label: "Canceled", glyph: { _tag: "Ring", color: color.muted, progress: 1, mark: "cross", label: "Canceled" } },
]

export const priorities: ReadonlyArray<PickerOption> = [
	{ value: "none", label: "No priority", glyph: { _tag: "Text", text: "–" } },
	{ value: "urgent", label: "Urgent", glyph: { _tag: "Bars", level: 3, alert: true, label: "Urgent" } },
	{ value: "high", label: "High", glyph: { _tag: "Bars", level: 3, label: "High" } },
	{ value: "medium", label: "Medium", glyph: { _tag: "Bars", level: 2, label: "Medium" } },
	{ value: "low", label: "Low", glyph: { _tag: "Bars", level: 1, label: "Low" } },
]

export const people: ReadonlyArray<PickerOption> = [
	{ value: "maya", label: "Maya Chen", glyph: { _tag: "Person", id: "maya", name: "Maya Chen", agent: false } },
	{ value: "omar", label: "Omar Haddad", glyph: { _tag: "Person", id: "omar", name: "Omar Haddad", agent: false } },
	{ value: "scout", label: "Scout (agent)", glyph: { _tag: "Person", id: "scout", name: "Scout", agent: true } },
]

export const unassigned: PickerOption = { value: "", label: "Unassigned", glyph: { _tag: "Text", text: "○" } }
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `agentId` | `string` | This picker's id for agents, unique on the page: "issue-ENG-12-status" |
| `agent` | `boolean` | False when the picker sits inside a component whose own contract already exposes this value (a list's cell editor): the picker then registers no contract, so agents see one entry, not two |
| `name` (required) | `string` | What the property is, for the filter's placeholder and screen readers: "Status" |
| `options` (required) | `PickerOption[]` | The choices, each with a value, a label and a glyph. |
| `selected` (required) | `string \| null` | The picked option's value, or null for none. The caller keeps it. |
| `empty` (required) | `PickerOption` | Shown on the button when nothing is selected |
| `compact` | `boolean` | An icon-only button (a row in a list), or the full row with its label (a properties panel) |
| `disabled` | `boolean` | The value can't be changed or the list opened, as when the plan or a permission rules it out |
| `readOnly` | `boolean` | The list opens to show the choices, but none can be picked: someone else owns the value |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedChange` | `{ value: string }` | The person or an agent picked an option; store its value and pass it back as `selected` |

**Agent actions**

| Action | Description |
| --- | --- |
| `PickedOption` | Choose one of the choices by its value (see page_query) |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Enter, Space, Down arrow | Open the list |
| A–Z | Filter the options |
| Down arrow, Up arrow | Move the highlight |
| Enter | Pick the highlighted option |
| Escape | Close without picking |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

- The trigger is a button that names the field and its value, such as "Status: In progress". Opening it puts focus in a filter field over the list.
- Typing filters, the arrow keys move, Enter picks and Escape closes; the chosen option is marked checked.
- Agents read the current value and the choices, and pick one by value.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/combobox
```

With npm: `npx shadcn@latest add checkfu/uicontext/combobox`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/combobox`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/combobox`.

It writes these files:

- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Motion.ts`
- `src/ui/uicontext/components/Picker.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/agent/Agent.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Picker.ts](https://github.com/checkfu/uicontext/blob/main/src/Picker.ts)

## Used in

- [Helpdesk](https://uicontext.com/blocks/helpdesk.md)

## Related

- [Dialog](https://uicontext.com/docs/components/dialog.md) (`dialog`): A modal window with a title, a description and actions.
- [Menu](https://uicontext.com/docs/components/menu.md) (`menu`): A dropdown menu of actions, checkbox and radio items, shortcuts and submenus.
- [Context Menu](https://uicontext.com/docs/components/context-menu.md) (`context-menu`): The menu a right click or long press opens over rows, files and cards.
- [Sheet](https://uicontext.com/docs/components/sheet.md) (`sheet`): A panel that slides over the page from a side or the bottom.
- [Toast](https://uicontext.com/docs/components/toast.md) (`toast`): Short notifications with an optional action like Undo.
- [Popover](https://uicontext.com/docs/components/popover.md) (`popover`): Content that opens beside its trigger and closes when you click away.
