<!-- index: /slotted/llms.txt -->
# multi-select

`data-ui="multi-select"` · Picks several options shown as tags.

## Import

```tsx
import { MultiSelect, MultiSelectContent, MultiSelectControl, MultiSelectGroup, MultiSelectItem, MultiSelectSeparator } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';
import {
  MultiSelect,
  MultiSelectContent,
  MultiSelectControl,
  MultiSelectItem,
} from '@slotted/react/multi-select';

const WAREHOUSES = [
  { value: 'RNO', label: 'Reno, NV' },
  { value: 'CMH', label: 'Columbus, OH' },
  { value: 'SAV', label: 'Savannah, GA' },
  { value: 'DFW', label: 'Dallas, TX' },
  { value: 'ONT', label: 'Ontario, CA' },
  { value: 'ABE', label: 'Allentown, PA' },
];

export default function MultiSelectBasic() {
  return (
    <Field>
      <FieldLabel>Stocking warehouses</FieldLabel>
      <MultiSelect>
        <MultiSelectControl placeholder="Add a warehouse" />
        <MultiSelectContent>
          {WAREHOUSES.map((warehouse) => (
            <MultiSelectItem key={warehouse.value} value={warehouse.value}>
              {warehouse.label}
            </MultiSelectItem>
          ))}
        </MultiSelectContent>
      </MultiSelect>
      <FieldDescription>
        Chai tea, 24 bags is reordered at each of these warehouses.
      </FieldDescription>
    </Field>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, rendered by MultiSelectControl | yes |
| `control` | div with role="grid" | yes |
| `chips` | div with role="row" | yes |
| `field` | span with role="gridcell" | yes |
| `input` | input with role="combobox" | yes |
| `counter` | span | no |
| `busy` | span | no |
| `clear` | button | no |
| `trigger` | button | yes |
| `arrow` | span | yes |
| `positioner` | div, rendered by MultiSelectContent | yes |
| `content` | div with role="listbox" and aria-multiselectable | yes |
| `chosen` | div | no |
| `group` | div with role="group" | no |
| `group-label` | div | no |
| `item` | div with role="option" | yes |
| `indicator` | span | yes |
| `icon` | span | no |
| `label` | span | yes |
| `match` | mark, the match part of the highlight component, inside label | no |
| `description` | span | no |
| `separator` | div with role="separator" | no |
| `empty` | div | no |
| `status` | div with role="status" | yes |
| `placeholder` | div holding skeleton rows | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `value` | `readonly string[]` | — | MultiSelect | The chosen values, in the order the reader chose them. Controlled. |
| `defaultValue` | `readonly string[]` | — | MultiSelect | The values an uncontrolled multi-select starts with, read once. A reset of the form it belongs to returns to them. |
| `inputValue` | `string` | — | MultiSelect | The text in the input. It is emptied whenever a value is committed. Controlled. |
| `defaultInputValue` | `string` | — | MultiSelect | The text an uncontrolled input starts with, read once. |
| `open` | `boolean` | — | MultiSelect | Whether the list is open. Controlled. |
| `defaultOpen` | `boolean` | — | MultiSelect | Whether an uncontrolled list starts open. |
| `highlightedValue` | `string \| null` | — | MultiSelect | The option the highlight is on, which is not a chosen option. Controlled. |
| `defaultHighlightedValue` | `string \| null` | — | MultiSelect | Where an uncontrolled highlight starts. |
| `items` | `MultiSelectItems` | — | MultiSelect | Values and labels declared up front, so a tag shows its label before the list has rendered its options. |
| `filter` | `MultiSelectFilter` | — | MultiSelect | Compares an option label with the query. When absent, the comparison is a locale-sensitive "contains" that ignores case and accents; null turns filtering off for options a server has already filtered. |
| `locale` | `string` | — | MultiSelect | The BCP 47 language of the filter and of the duplicate comparison. When absent, the language in force. |
| `validateValue` | `(text, values) => boolean` | — | MultiSelect | Whether typed text may become a value. Text it turns down is reported through onValueRefuse with the cause invalid. |
| `name` | `string` | — | MultiSelect | The submission name. One hidden input per value carries it. |
| `form` | `string` | — | MultiSelect | The id of the form the hidden inputs belong to, for a multi-select rendered outside it. |
| `total` | `number` | — | MultiSelect | The number of options in the whole collection, for aria-setsize on a windowed list. |
| `max` | `number \| null` | null | MultiSelect | The most values the set may hold; null means no limit. At the limit, every option not yet chosen is announced as unavailable. |
| `delimiters` | `readonly string[]` | , | MultiSelect | The characters that commit the typed text and split a paste. One list serves both. |
| `creatable` | `boolean` | false | MultiSelect | Whether typed text may become a value. |
| `editable` | `boolean` | false | MultiSelect | Whether Enter on a focused tag turns it into a text field. |
| `reorderable` | `boolean` | false | MultiSelect | Whether Alt with ArrowLeft, ArrowRight, Home or End moves the focused tag along the row. |
| `allowDuplicates` | `boolean` | false | MultiSelect | Whether the same value may be held twice. |
| `addOnPaste` | `boolean` | true | MultiSelect | Whether a paste that contains delimiters becomes several values. |
| `commitOnBlur` | `boolean` | true | MultiSelect | Whether typed text is committed when focus leaves the control. |
| `overflow` | `'wrap' \| 'collapse'` | wrap | MultiSelect | What the control does with more tags than fit: wrap grows by rows up to three, then scrolls; collapse keeps one row and counts the rest in a +N tag. |
| `chosenOptions` | `'show' \| 'hide'` | show | MultiSelect | Whether a chosen option stays in the list with a check, or leaves it. |
| `clearable` | `boolean` | true | MultiSelect | Whether the clear button is drawn while the control holds a value or typed text. |
| `closeOnSelect` | `boolean` | false | MultiSelect | Whether choosing an option closes the list. Off by default, because a set is built with several choices. |
| `closeOnEscape` | `boolean` | true | MultiSelect | Whether Escape closes the list. |
| `closeOnOutsidePress` | `boolean` | true | MultiSelect | Whether a press outside the list closes it. |
| `loop` | `boolean` | false | MultiSelect | Whether the highlight wraps from the last option to the first, and back. |
| `highlightMatch` | `boolean` | true | MultiSelect | Whether the span the query matched is marked. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | MultiSelect | The step of the control scale. Inside a field, an unset size follows the field. |
| `variant` | `'outline' \| 'subtle' \| 'plain'` | outline | MultiSelect | The frame: outline draws an edge, subtle a filled ground, plain neither until hover or focus. |
| `side` | `'bottom' \| 'top' \| 'inline-start' \| 'inline-end' \| 'left' \| 'right'` | bottom | MultiSelect | The side of the control the list opens on. |
| `align` | `'start' \| 'center' \| 'end'` | start | MultiSelect | How the list aligns along that side. |
| `width` | `'anchor' \| 'content'` | content | MultiSelect | content keeps the list at least as wide as the control and widens it to its longest option; anchor pins it to the width of the control. |
| `mount` | `'active' \| 'lazy' \| 'eager'` | active | MultiSelect | When the list exists in the DOM: only while open (active), from the first open on (lazy), or from the start (eager). |
| `strategy` | `'absolute' \| 'fixed'` | absolute | MultiSelect | The CSS position the list uses. |
| `openOn` | `'input' \| 'focus' \| 'manual'` | input | MultiSelect | Which gestures open the list besides the trigger and the arrow keys: typing or a press on the input (input), also any focus on the input (focus), or none, so typing filters without opening (manual). |
| `sideOffset` | `number` | 4 | MultiSelect | The distance in pixels between the control and the list. |
| `alignOffset` | `number` | 0 | MultiSelect | The shift in pixels along the aligned edge. |
| `collisionPadding` | `number` | 8 | MultiSelect | How close in pixels the list may come to the edge of the viewport before it moves. |
| `flip` | `boolean` | true | MultiSelect | Whether the list moves to the opposite side when it does not fit. |
| `shift` | `boolean` | true | MultiSelect | Whether the list slides along its side to stay in view. |
| `hideWhenDetached` | `boolean` | true | MultiSelect | Whether the list hides when the control scrolls out of its clipping container. |
| `portal` | `boolean` | true | MultiSelect | Whether the list is portalled to the end of the document. |
| `fullWidth` | `boolean` | true | MultiSelect | Whether the control fills the width of its container. |
| `disabled` | `boolean` | false | MultiSelect | Whether the control is disabled: it takes no input, draws no remove buttons and leaves the tab order. |
| `readOnly` | `boolean` | false | MultiSelect | Whether the values can be read and copied but not changed. The input stays focusable; the list, the remove buttons and the clear button are withheld. |
| `required` | `boolean` | false | MultiSelect | Whether an empty set is invalid. |
| `invalid` | `boolean` | false | MultiSelect | Whether the control is drawn in the error state, with aria-invalid on the input. |
| `loading` | `boolean` | false | MultiSelect | Whether options are on their way: the spinner replaces the chevron and the list shows placeholder rows. |
| `messages` | `Partial<MultiSelectMessages>` | — | MultiSelect | Replaces any of the phrases the multi-select writes: clear, trigger, chosen ({count}), count ({count}), counter ({count}, {max}), create ({query}), empty, loading, remove ({label}) and reorder ({label}, {position}, {count}). Phrases left out come from the language in force. |
| `render` | `(props) => ReactNode` | — | MultiSelectControl | Replaces the frame element. The props it receives include the ref that measures the frame for the narrow layout, which the replacement has to keep. |
| `chosen` | `boolean` | true | MultiSelectContent | Whether the band of chosen values is drawn at the top of the list. |
| `empty` | `ReactNode` | — | MultiSelectContent | What the list shows when no option matches, in place of the empty message. |
| `status` | `ReactNode` | — | MultiSelectContent | What the live region says, in place of the number of matching options. |
| `value` | `string` | — | MultiSelectItem | The key of the option, which is what enters the set. |
| `disabled` | `boolean` | false | MultiSelectItem | Whether the option can be chosen. |
| `description` | `ReactNode` | — | MultiSelectItem | A second line under the label. |
| `icon` | `ReactNode` | — | MultiSelectItem | Any element, drawn in the first column of the option. |
| `textValue` | `string` | — | MultiSelectItem | The text the filter compares, for an option whose children are not plain text. |
| `position` | `number` | — | MultiSelectItem | The place of the option in a windowed collection, for aria-posinset. |
| `label` | `ReactNode` | — | MultiSelectGroup | The name of the group. |
| `MultiSelectDefaultsProvider` | `component` | — | Application | Sets any axis once for every multi-select below it. The words come from the language in force, set by SlottedProvider. A prop set on one multi-select still wins. |
| `render` | `(props) => ReactNode` | — | MultiSelectControl, MultiSelectContent | On MultiSelectControl, replaces the frame and receives the ref that measures it; a replacement that drops the ref loses the narrow layout. On MultiSelectContent, replaces the layer and receives its attributes and the options. |
| `ref` | `Ref<MultiSelectHandle>` | — | MultiSelect | focus(), open(), close(), scrollToKey(value) and setValue(values, reason): the actions a prop cannot express. |
| `native props` | `ComponentPropsWithRef` | — | MultiSelectControl, MultiSelectContent | Props written on MultiSelectControl reach the input, such as aria-label and placeholder; props written on MultiSelectContent reach the listbox. On the input, your onKeyDown, onChange, onClick, onFocus, onBlur and onPaste run first, and calling preventDefault stops the multi-select from handling that event. |
| `data-*` | `attributes` | — | every part | Every state is an attribute to style: data-open, data-disabled, data-readonly, data-invalid, data-required, data-loading, data-selected, data-current and data-complete. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onValueChange` | `(value: readonly string[], request: ChangeRequest<readonly string[]>)` | Before the set changes. request.reason is select for an option, submit for typed text, remove for one value, clear for all of them, keyboard for a reorder, and programmatic for the handle and for a form reset. request.cancel() refuses the change. |
| `onValueRefuse` | `(values: readonly string[], details: { cause: "duplicate" \| "limit" \| "invalid" })` | Values were turned away, and details.cause says why. Nothing is held and nothing can be cancelled: the values never entered the set. |
| `onInputValueChange` | `(value: string, request: ChangeRequest<string>)` | Before the typed text changes. request.reason is keyboard while typing, select or submit when a commit empties it, clear for Escape or the clear button, blur when leaving the control commits it, and programmatic for a form reset. |
| `onOpenChange` | `(open: boolean, request: ChangeRequest<boolean>)` | Before the list opens or closes. request.reason is pointer, keyboard, escape, outsidePress, select, dismiss, blur or programmatic. |
| `onHighlightedValueChange` | `(value: string \| null, request: ChangeRequest<string \| null>)` | Before the highlight moves. request.reason is pointer, keyboard or programmatic. Moving the highlight chooses nothing. |
| `onLoadMore` | `()` | The last option scrolled into view, so the next page can be fetched. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `variant` | `outline`, `subtle`, `plain` | `outline` |
| `side` | `bottom`, `top`, `inline-start`, `inline-end`, `left`, `right` | `bottom` |
| `align` | `start`, `center`, `end` | `start` |
| `width` | `anchor`, `content` | `content` |
| `mount` | `active`, `lazy`, `eager` | `active` |
| `strategy` | `absolute`, `fixed` | `absolute` |
| `openOn` | `input`, `focus`, `manual` | `input` |
| `overflow` | `wrap`, `collapse` | `wrap` |
| `chosenOptions` | `show`, `hide` | `show` |
| `creatable` | `true`, `false` | `false` |
| `editable` | `true`, `false` | `false` |
| `reorderable` | `true`, `false` | `false` |
| `allowDuplicates` | `true`, `false` | `false` |
| `commitOnBlur` | `true`, `false` | `true` |
| `addOnPaste` | `true`, `false` | `true` |
| `clearable` | `true`, `false` | `true` |
| `loop` | `true`, `false` | `false` |
| `highlightMatch` | `true`, `false` | `true` |
| `closeOnSelect` | `true`, `false` | `false` |
| `closeOnEscape` | `true`, `false` | `true` |
| `closeOnOutsidePress` | `true`, `false` | `true` |
| `flip` | `true`, `false` | `true` |
| `shift` | `true`, `false` | `true` |
| `hideWhenDetached` | `true`, `false` | `true` |
| `portal` | `true`, `false` | `true` |
| `fullWidth` | `true`, `false` | `true` |
| `disabled` | `true`, `false` | `false` |
| `readOnly` | `true`, `false` | `false` |
| `required` | `true`, `false` | `false` |
| `invalid` | `true`, `false` | `false` |
| `loading` | `true`, `false` | `false` |

## Rules

- The input is the tab stop of the control. The tags, their remove buttons and the trigger carry tabindex="-1", as the combobox and grid patterns ask; the clear button, while it is drawn, is in the tab order.
- The tags are a layout grid with one row, which leaves ArrowDown and ArrowUp to the list. Focus lands on a tag rather than on its remove button, and Delete and Backspace do what the button does. The grid carries the number of chosen values as its name.
- The list sets aria-multiselectable, and selection does not follow the highlight: moving through the options chooses nothing until Enter or a press. This departs from the note in the combobox pattern, as every implementation surveyed does.
- Changes are announced as well as drawn: a value added with the count now held, a value removed with its label and the count left, a value moved with its new position, and the limit when it is reached. The status row announces how many options match the query.

## Tokens

`--slotted-border-subtle`, `--slotted-border-width-thin`, `--slotted-control-border-color`, `--slotted-control-border-width`, `--slotted-control-focus-ring-offset`, `--slotted-control-font-family`, `--slotted-control-font-size-lg`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-gap-lg`, `--slotted-control-gap-md`, `--slotted-control-gap-sm`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-lg`, `--slotted-control-icon-size-md`, `--slotted-control-icon-size-sm`, `--slotted-control-inner-size-lg`, `--slotted-control-inner-size-md`, `--slotted-control-inner-size-sm`, `--slotted-control-invalid-edge-width`, `--slotted-control-padding-inline-lg`, `--slotted-control-padding-inline-md`, `--slotted-control-padding-inline-sm`, `--slotted-control-radius`, `--slotted-control-shadow`, `--slotted-control-shadow-focus-geometry`, `--slotted-control-surface`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-disabled-background`, `--slotted-disabled-border`, `--slotted-disabled-foreground`, `--slotted-duration-medium`, `--slotted-duration-short`, `--slotted-easing-enter`, `--slotted-easing-exit`, `--slotted-easing-standard`, `--slotted-focus-ring-color`, `--slotted-focus-ring-color-invalid`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-font-family-sans`, `--slotted-font-size-xs`, `--slotted-font-weight-medium`, `--slotted-font-weight-semibold`, `--slotted-line-height-md`, `--slotted-line-height-sm`, `--slotted-multi-select-action-active-background`, `--slotted-multi-select-action-color`, `--slotted-multi-select-action-hover-background`, `--slotted-multi-select-action-size`, `--slotted-multi-select-arrow-color`, `--slotted-multi-select-autofill-background`, `--slotted-multi-select-background`, `--slotted-multi-select-border-color`, `--slotted-multi-select-color`, `--slotted-multi-select-content-border-color`, `--slotted-multi-select-content-padding-block`, `--slotted-multi-select-content-padding-inline`, `--slotted-multi-select-content-radius`, `--slotted-multi-select-content-shadow`, `--slotted-multi-select-content-text`, `--slotted-multi-select-content-text-muted`, `--slotted-multi-select-counter-color`, `--slotted-multi-select-counter-complete-color`, `--slotted-multi-select-disabled-background`, `--slotted-multi-select-disabled-border-color`, `--slotted-multi-select-disabled-color`, `--slotted-multi-select-field-basis`, `--slotted-multi-select-field-min`, `--slotted-multi-select-group-label-height`, `--slotted-multi-select-highlight`, `--slotted-multi-select-highlight-active`, `--slotted-multi-select-highlight-ring`, `--slotted-multi-select-hover-border-color`, `--slotted-multi-select-icon-size-min`, `--slotted-multi-select-invalid-border-color`, `--slotted-multi-select-invalid-edge-width`, `--slotted-multi-select-invalid-focus-ring-color`, `--slotted-multi-select-item-radius`, `--slotted-multi-select-measure`, `--slotted-multi-select-open-border-color`, `--slotted-multi-select-placeholder-color`, `--slotted-multi-select-plain-hover-background`, `--slotted-multi-select-radius`, `--slotted-multi-select-readonly-background`, `--slotted-multi-select-selected-color`, `--slotted-multi-select-separator-color`, `--slotted-multi-select-shadow`, `--slotted-multi-select-subtle-background`, `--slotted-multi-select-subtle-border-color`, `--slotted-multi-select-subtle-hover-background`, `--slotted-multi-select-surface`, `--slotted-overlay-max-block-size`, `--slotted-radius-lg`, `--slotted-radius-none`, `--slotted-shadow-md`, `--slotted-space-1`, `--slotted-space-2`, `--slotted-space-5`, `--slotted-surface-raised`, `--slotted-surface-sunken`, `--slotted-target-comfortable`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-tone-accent-border`, `--slotted-tone-accent-surface`, `--slotted-tone-accent-text`, `--slotted-tone-danger-border`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`, `--slotted-z-index-popover`

## Examples

See [multi-select.examples.md](./multi-select.examples.md).
