<!-- generated by scripts/gen-docs.mjs from docs/system/emoji-picker.md. Edit the source and rerun. -->
# EmojiPicker

## Description
Finds one emoji among nearly two thousand, by its name, a word people use for it, its group or what you picked lately, and picks it with a press or the keyboard.

`import { EmojiPicker, EmojiPickerPopover } from "@/components/cobalt/ui/emoji-picker"`, source `registry/cobalt/ui/emoji-picker.tsx`, status `ready-with-gaps`.
Foundation: `Base UI Popover`. Traps checked: `trap/menu-navigation`, `trap/overlay-focus-return`, `trap/overlay-no-max-height`, `trap/scroll-chain`, `trap/button-icon-name`, `trap/current-unmarked`, `trap/live-region-double`, `trap/select-async`, `trap/touch-input-zoom`.

`trap/menu-navigation` is answered by the WAI-ARIA grid: one cell in the tab order, arrows, Home, End, Page Up and Page Down (Keyboard). `trap/overlay-focus-return` by `EmojiPickerPopover`, which returns focus to its trigger, or to `finalFocus` such as the composer's field. `trap/overlay-no-max-height` by the grid's fixed 256px with its own scroll. `trap/scroll-chain` by `overscroll-behavior: contain` on the grid. `trap/button-icon-name` by every cell's name, the emoji's Unicode name, and the group buttons' labels. `trap/current-unmarked` by `aria-current` and a fill on the group being read. `trap/live-region-double` by one status line for search results. `trap/select-async` by the data loading on hover of the trigger and a spinner only after 250ms. `trap/touch-input-zoom` by the search field's 16px text on a phone.

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Base UI `Popover` for `EmojiPickerPopover`: placement, outside press, Escape, focus in and back | The data: 1,870 emoji up to Unicode 15 from emojibase-data 17 (MIT; names and keywords from Unicode CLDR), trimmed by `scripts/emoji-data.mjs` to 96 KB, 33 KB gzipped, in `registry/cobalt/lib/emoji-data.ts` and loaded on first use as its own chunk; Tag's ranked search; a recent row kept in this browser, Popular until there is one; a group bar that follows the scroll, its mark sliding to the group being read; the grid pattern on the keyboard; a line naming the emoji under the pointer or focus, else the one Enter picks, else the first of the group being read, so it is never blank; six skin tones, chosen from a hand beside the search and applied in code to the 293 emoji that take one, kept in this browser. Dropped from Tag: the reaction quick row inside the picker and the outside filter field |

## Examples
The picker on its own, with what was picked under it.

Real uses, 0 call sites (`rg -n "<EmojiPicker\b" app registry --glob '!registry/cobalt/ui/*'`): `Reactions` and `Composer` open it through `EmojiPickerPopover`.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/emoji-picker/default.tsx` | default | The picker on its own: search by name or keyword, jump to a group, walk the grid with the arrows, pick a skin tone |
| `docs/system/examples/emoji-picker/in-popover.tsx` | composition:Popover | In a popover from a button; it loads its emoji when the pointer reaches the button and closes on a pick |

Example: The picker on its own: search by name or keyword, jump to a group, walk the grid with the arrows, pick a skin tone (`docs/system/examples/emoji-picker/default.tsx`)

```tsx
import { useState } from "react";

import { EmojiPicker } from "@/components/cobalt/ui/emoji-picker";
import { TooltipProvider } from "@/components/cobalt/ui/tooltip";

export default function Example() {
  const [picked, setPicked] = useState<string[]>([]);
  return (
    <TooltipProvider>
      <div className="flex max-w-80 flex-col gap-3">
        <div className="rounded-3xl bg-popover p-2 shadow-floating">
          <EmojiPicker onPick={(emoji) => setPicked((list) => [emoji, ...list].slice(0, 8))} />
        </div>
        <p className="text-sm text-muted-foreground" aria-live="polite">
          {picked.length ? `Picked ${picked.join(" ")}` : "Nothing picked yet."}
        </p>
      </div>
    </TooltipProvider>
  );
}
```

Example: In a popover from a button; it loads its emoji when the pointer reaches the button and closes on a pick (`docs/system/examples/emoji-picker/in-popover.tsx`)

```tsx
import { useState } from "react";

import { EmojiIcon } from "@/components/cobalt/icon";
import { Button } from "@/components/cobalt/ui/button";
import { EmojiPickerPopover } from "@/components/cobalt/ui/emoji-picker";
import { TooltipProvider } from "@/components/cobalt/ui/tooltip";

export default function Example() {
  const [status, setStatus] = useState<string | null>(null);
  return (
    <TooltipProvider>
      <div className="flex items-center gap-3">
        <EmojiPickerPopover
          side="bottom"
          onPick={setStatus}
          trigger={
            <Button variant="secondary">
              <EmojiIcon data-icon="inline-start" aria-hidden="true" />
              Set a status
            </Button>
          }
        />
        <span className="text-sm text-muted-foreground">{status ? `Your status: ${status}` : "No status"}</span>
      </div>
    </TooltipProvider>
  );
}
```

## Variants
Not applicable: the picker has one look; `EmojiPickerPopover` is the same picker in a popover.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| loading | first open, before the data arrives | type a search | the grid busy, and a spinner after 250ms | test |
| browsing | data loaded, no search | scroll, jump to a group, pick | sticky group names; the bar marks the group being read | by hand |
| searching | words in the search | pick, Enter picks the first | the results in one group, and "12 emoji" read once | by hand |
| no results | a search that finds nothing | change the words | "No emoji match" and the words, read once | by hand |
| cell focused | arrows or Tab into the grid | press Enter or Space to pick | the outline and a fill on the cell; its name under the grid | by hand |
| toning | the hand beside the search pressed | pick a tone, or Escape | six hands in place of the search row, at its height, a fill under the current one | by hand |

### State precedence
- Searching and browsing: searching wins. The bar stays, marks nothing, and pressing a group clears the search.
- Loading and searching: loading wins. The words stay and the results show when the data arrives.
- Toning and searching: toning wins the search row until a tone is picked or Escape; the words stay and come back with it.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| the popover opens or closes | announce | `popup-motion`, `--t-pop` | opacity | the same fade |
| a cell or group under the pointer | input | `pressable`, `--t-press` | background-color | the same |
| the group being read changes, or the tone | input | `useIndicator`, `--t-layout` | transform, width of the mark | the mark moves at once |

## Props

<!-- Props generated from registry/cobalt/ui/emoji-picker.tsx with TypeScript by scripts/props-table.mjs -->
### EmojiPicker

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `onPick` | `(emoji: string) => void` |  | The emoji at the person's skin tone, after it is added to their recent row. |
| `className?` | `string` |  |  |
| `labels?` | `Partial<EmojiPickerLabels>` |  |  |
| `searchRef?` | `RefObject<HTMLInputElement>` |  | The search field, for a popover's first focus. |

### EmojiPickerPopover

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `onPick` | `(emoji: string) => void` |  |  |
| `trigger` | `ReactElement<unknown, string \| JSXElementConstructor<any>>` |  | The button that opens it, such as an `IconButton`. |
| `align?` | `"center" \| "start" \| "end"` | `"start"` |  |
| `finalFocus?` | `RefObject<HTMLElement>` |  | Where the focus goes when it closes, such as the composer's field. |
| `labels?` | `Partial<EmojiPickerLabels>` |  |  |
| `side?` | `"top" \| "bottom"` | `"top"` |  |

`EmojiPicker` takes `onPick`, `searchRef` for a first focus, and `labels` for every string it speaks. `EmojiPickerPopover` takes `trigger`, `onPick`, `side`, `align`, `finalFocus` and `labels`. `registry/cobalt/lib/emoji.ts` holds `loadEmoji`, `searchEmoji`, `withTone` and the recent row for a host that builds its own picker.

## Usage

### When to use
- Reacting to a message with any emoji.
- Putting an emoji into what you write.

### When not to use
- Choosing among a few icons for a category. Use SegmentedControl instead, or RadioGroup.
- A quick row of the same six reactions. Use Reactions instead.

### Rules
- `rule/emoji-picker-load-on-intent`: When placing the picker, open it through `EmojiPickerPopover` or call `loadEmoji` on hover of your own trigger, instead of importing the data with the page, because the data is 33 KB gzipped that most visits never use. Evidence: measured 96 KB raw and 33 KB gzipped for `registry/cobalt/lib/emoji-data.ts`. Check: review of every import of `emoji-data`.
  - Don't: `import { EMOJI_GROUPS } from "@/lib/cobalt/emoji-data"` in a page
  - Do: `<EmojiPickerPopover trigger={button} onPick={react} />`
- `rule/emoji-picker-focus-back`: When the picker puts an emoji into a field, pass the field as `finalFocus`, because the person picked it to keep writing. Evidence: principle heuristic: user control and freedom, focus returns where the work is. Check: review of every `EmojiPickerPopover` beside a field.
  - Don't: `<EmojiPickerPopover trigger={button} onPick={insert} />` beside a composer
  - Do: `<EmojiPickerPopover trigger={button} onPick={insert} finalFocus={field} />`

### Content
- Follows `rule/writing-tooltip-is-the-name`.
- `rule/emoji-picker-group-names`: When naming groups, use sentence case with "and", such as "Food and drink", instead of the ampersand of Unicode's names, because the system writes words, not symbols. Evidence: person D12. Check: test `emoji.test.ts` checks the eight group names.
  - Don't: `"Food & Drink"`
  - Do: `"Food and drink"`

### Anti-slop
- `rule/emoji-picker-no-scale-hover`: When the pointer rests on a cell, fill it with `--muted`, instead of scaling the emoji up, because nothing in the system scales and a growing glyph covers its neighbours. Evidence: person D36. Check: review of picker cells for `scale`.
  - Don't: `className="hover:scale-125"`
  - Do: `<EmojiPicker onPick={pick} />`

### Limits
- `rule/emoji-picker-unicode-15`: When adding emoji, keep to Unicode emoji 15 and older, instead of the newest set, because emoji newer than a reader's system draw as empty boxes. Evidence: measured 44 emoji newer than 15 in emojibase-data 17, left out by scripts/emoji-data.mjs. Check: test `emoji.test.ts`.
  - Don't: `rows.filter((row) => row.version <= 17)`
  - Do: `node scripts/emoji-data.mjs <emojibase-data>`

## Accessibility
Rests on the WAI-ARIA grid pattern with native buttons in its cells, and Base UI `Popover` around it.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Arrow Down or Enter | the search, empty | moves into the grid | the first cell | by hand |
| Enter | the search, with words | picks the first result | the trigger or `finalFocus` | by hand |
| Arrow Left and Arrow Right | a cell | moves one cell, across rows; mirrored right to left | that cell | by hand |
| Arrow Up and Arrow Down | a cell | moves one row, across groups, to the nearest column | that cell; Up from the first row goes to the search | by hand |
| Home and End | a cell | the row's first or last cell; with Control, the grid's | that cell | by hand |
| Page Up and Page Down | a cell | five rows | that cell | by hand |
| Enter or Space | a cell | picks it | the trigger or `finalFocus`, the popover closed | by hand |
| Tab | the search | moves to the skin-tone hand, then the bar, then the grid | the next control | by hand |
| Enter or Space | the skin-tone hand | shows the six tones in the search row | the current tone | by hand |
| Arrow Left and Arrow Right | a tone | moves the choice; mirrored right to left | that tone | by hand |
| Enter, Space or Escape | a tone | Enter or Space picks it; Escape keeps the tone it had | the skin-tone hand | by hand |
| Escape | anywhere in the popover | closes it | the trigger | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| search | textbox | `labels.search` | none | "Search emoji" | test |
| group bar | navigation | `labels.picker` | `aria-current` on the group being read | each group's name | test |
| grid | grid | `labels.picker` | `aria-busy` while loading | "Emoji, grid" | test |
| group | rowgroup | its visible name, or "Search results" | none | the group's name on entering it | by hand |
| cell | gridcell holding a button | the emoji's name | roving `tabindex` | "thumbs up, button" | by hand |
| results | status | its text | visually hidden | "12 emoji" or "No emoji match", once per search | test |
| skin tone | button | "Skin tone:" and the tone | `aria-expanded` false; it gives way to the tones | "Skin tone: Medium, button, collapsed" | test |
| tones | radiogroup of radio buttons | `labels.tone`; each tone its name | `aria-checked` on the current one, roving `tabindex` | "Medium light, radio button, 2 of 6" | by hand |

## Tokens
| Part | State | Token |
|---|---|---|
| popover | open | `--popover`, `--r-3xl`, `--elev-floating`, `--pad-popup`, `--t-pop` |
| cell | hover, focus | `--muted`, `--r-md` |
| group name, name line | all | `--muted-foreground` |
| group bar, tones | current | a `--muted` mark under it that slides on `--t-layout`, `--foreground` |

## Related
- Reactions: what most picks become.
- Composer: types a pick at the caret.
- CommandPalette: a search over records and commands, not emoji.
