# Palette

One surface for finding a thing or running a command by typing, the same in every product.

Live variants: https://design.arcadia.is/components/palette

```tsx
import {
  Palette, PaletteInput, PaletteFilters, PaletteList, PaletteGroup, PaletteItem,
  PaletteEmpty, PaletteFooter, MatchText,
} from "@arcadia/ui";
```

Search for Hana, or use the arrow keys to choose a recent result.

```tsx
import { useState } from "react";
import {
  Button, Palette, PaletteInput, PaletteFilters, PaletteList, PaletteEmpty,
  PaletteFooter, KeyLegend,
} from "@arcadia/ui";

function Search() {
  const [open, setOpen] = useState(false);
  const [instant, setInstant] = useState(false);
  const [query, setQuery] = useState("");
  const [filters, setFilters] = useState<string[]>([]);
  const [selected, setSelected] = useState("");
  const { status, found } = useFound(query, filters);
  const set = (key: string, on: boolean) => setFilters((f) => (on ? [...f, key] : f.filter((k) => k !== key)));
  return (
    <>
      <Button icon="search" variant="secondary" aria-haspopup="dialog"
        onClick={(e) => { setInstant(e.detail === 0); setOpen(true); }}>Open search</Button>
      <span {...stylex.props(cmp.result)} role="status">{selected ? "Opened: " + selected : "No result opened"}</span>
      <Palette open={open} onOpenChange={setOpen} label="Search" instant={instant}
        onOpenChangeComplete={(o) => { if (!o) { setQuery(""); setFilters([]); } }}>
        <PaletteInput value={query} onValueChange={setQuery} placeholder={PLACEHOLDER} />
        <PaletteFilters onRemoveLast={() => setFilters((f) => f.slice(0, -1))}>
          <Chips on={filters} drop={(key) => set(key, false)} />
          <Filter on={filters} set={set} />
        </PaletteFilters>
        <PaletteList status={status}>
          {found == null ? <Recent open={setSelected} /> : shown(found) ? <Results query={query} found={found} open={setSelected} /> : <PaletteEmpty query={query} />}
        </PaletteList>
        <PaletteFooter><KeyLegend hints={KEYS} /></PaletteFooter>
      </Palette>
    </>
  );
}
```

## Accessibility

The field is a combobox that owns the list: `aria-controls` names it and `aria-activedescendant` the lit row, so the focus stays in the field. The list is a listbox while it holds rows, each group a group named by its label, each row an option; a disabled row is `aria-disabled` and says why in its tooltip. Once the typing rests the count of results is said politely, and a failure at once. The palette is named by its `label`, a hidden title.

- ↓ + ↑: Move the light, round from one end to the other
- Page Down + Page Up: Move it by a page
- Enter: Run the lit row; with ⌘ or Ctrl, the row may open in a new tab
- Esc: Close, whatever the field holds
- Tab: Go to the filters and the footer, and back to the field
- Backspace: In the empty field, drop the last filter

## API reference

### Palette

- `open` (`boolean`, required)
- `onOpenChange` (`(open: boolean, details: OpenChangeDetails) => void`, required)
- `onOpenChangeComplete` (`(open: boolean) => void`)
- `label` (`string`, required): Its name ("Search"), a hidden title.
- `instant` (`boolean`): A shortcut's open: there in one frame.
- `modal` (`boolean`, default `true`): `false`: set down open on a page.
- `container` (`PortalContainer`): Where a surface's portal mounts. Default: `document.body`. Pass a themed subtree to keep its `data-theme`, or a transformed stage to contain `position: fixed` overlays (previews): a modal set down in one stays in it, shown in place, out of the browser's top layer.
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).

### PaletteInput

- `value` (`string`, required)
- `onValueChange` (`(value: string, details: { event: Event }) => void`, required)

Rest props go to `<input>` props.

### PaletteFilters

- `onRemoveLast` (`() => void`): Backspace in the empty field.
- `label` (`string`, default `"Filters"`)
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).
- `children` (`ReactNode`): `Chip`s and a menu's trigger.

### PaletteList

- `status` (`"ready" | "loading" | "error"`, default `"ready"`): `loading` keeps the last results; `error` drops them.
- `onRetry` (`() => void`): The failure's "Try again".
- `error` (`ReactNode`, default `"Could not search"`): What failed.
- `label` (`string`, default `"Results"`)
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).

### PaletteGroup

- `label` (`ReactNode`, required): What its rows are ("Cards").
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).

### PaletteItem

- `label` (`ReactNode`, required): A `MatchText` where the query found it.
- `description` (`ReactNode`): A line under it: the line it was found by.
- `leading` (`IconName | ReactNode`): A glyph, a mark, or a picture at `size="text"`.
- `meta` (`ReactNode`): Where it is, when.
- `shortcut` (`ShortcutSpec`): Its own keys, in the meta's place.
- `onSelect` (`(event: PaletteSelectEvent) => void`)
- `keepOpen` (`boolean`, default `false`)
- `disabled` (`boolean | string`, default `false`): A sentence says why, in its tooltip.
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).

### PaletteEmpty

- `query` (`string`, required)
- `children` (`ReactNode`): Other words ("No cards match Bug").

### PaletteFooter

- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).
- `children` (`ReactNode`): A `KeyLegend`, drawn only where a fine pointer is.

### MatchText

- `ranges` (`readonly (readonly [number, number])[]`): What a search found, each `[start, end)`.
- `query` (`string`): Or the query: its words, found where a word starts.
