Combobox
A select to filter: an input over a list of options, one or several of which are its value.
Usage
import { Component, ViewEncapsulation } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup, ScComboboxPortal, ScComboboxTrigger, ScInput, ScInputGroup, ScInputGroupAddon, scComboboxMatches,} from '@semantic-components/ui';import { SiCheckIcon, SiChevronDownIcon } from '@semantic-icons/lucide-icons';
/** A select to filter: type to narrow the frameworks, the arrows and Enter (or a click) pick one. */@Component({ selector: 'app-combobox-usage-demo', imports: [ ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup, ScComboboxPortal, ScComboboxTrigger, ScInput, ScInputGroup, ScInputGroupAddon, SiCheckIcon, SiChevronDownIcon, ], template: ` <div scCombobox #cb="scCombobox" class="w-56"> <div scInputGroup> <input scInput scComboboxInput placeholder="Select a framework" aria-label="Framework" /> <div scInputGroupAddon side="inline-end"> <button scComboboxTrigger> <svg siChevronDownIcon></svg> <span class="sr-only">Show the list</span> </button> </div> </div> <ng-template scComboboxPortal> <div scComboboxPopup> <div scComboboxEmpty>No items found.</div> <div scComboboxList aria-label="Frameworks"> @for (framework of matching(cb.query()); track framework) { <div scComboboxItem [value]="framework"> {{ framework }} <span scComboboxItemIndicator><svg siCheckIcon></svg></span> </div> } </div> </div> </ng-template> </div> `, host: { class: 'flex w-full justify-center' }, encapsulation: ViewEncapsulation.None,})export class ComboboxUsageDemo { private readonly frameworks = [ 'Next.js', 'SvelteKit', 'Nuxt.js', 'Remix', 'Astro', ];
private matching(query: string): string[] { return this.frameworks.filter((name) => scComboboxMatches(name, query)); }}Anatomy
Base UI's Combobox, on Angular Aria's Combobox and Listbox. Its value is one of its options, never free text (that is an autocomplete). The input goes in our input group, with a trigger and a clear button at its end; the popup hangs from it. You filter the options, by the text typed since the list opened: query().
import { ScCombobox, ScComboboxInput, ScComboboxTrigger, ScComboboxClear, ScComboboxPortal, ScComboboxPopup, ScComboboxEmpty, ScComboboxList, ScComboboxGroup, ScComboboxGroupLabel, ScComboboxSeparator, ScComboboxItem, ScComboboxItemIndicator, scComboboxMatches,} from '@semantic-components/ui';<div scCombobox #cb="scCombobox" [formField]="…"> <div scInputGroup> <input scInput scComboboxInput /> <div scInputGroupAddon side="inline-end"> <button scComboboxTrigger>…</button> <button scComboboxClear>…</button> </div> </div> <ng-template scComboboxPortal> <div scComboboxPopup> <div scComboboxEmpty>…</div> <div scComboboxList aria-label="…"> <div scComboboxGroup> <div scComboboxGroupLabel>…</div> @for (item of matching(cb.query()); track item) { <div scComboboxItem [value]="item"> … <span scComboboxItemIndicator>…</span> </div> } </div> <div scComboboxSeparator></div> </div> </div> </ng-template></div>Several values (multiple): chips, then the input.
import { ScComboboxMultiple, ScComboboxChips, ScComboboxChip, ScComboboxChipRemove } from '@semantic-components/ui';<div scCombobox multiple [(value)]="values"> <div scComboboxChips> @for (value of values(); track value) { <span scComboboxChip [value]="value"> … <button scComboboxChipRemove>…</button> </span> } <input scComboboxInput aria-label="…" /> </div> <ng-template scComboboxPortal>…</ng-template></div>A trigger showing the value, the search inside the popup: the trigger takes the field's look by itself, and in a field its label names it and the popup. Angular Aria renders a combobox's list from a template, so the list goes in a list content template beside the search.
import { ScComboboxValue, ScComboboxListTemplate } from '@semantic-components/ui';<div scField> <label scLabel>…</label> <div scCombobox #cb="scCombobox"> <button scComboboxTrigger> <span scComboboxValue placeholder="…"></span> … </button> <ng-template scComboboxPortal> <div scComboboxPopup> <div scInputGroup><input scInput scComboboxInput aria-label="…" /></div> <div scComboboxEmpty>…</div> <ng-template scComboboxListTemplate> <div scComboboxList aria-label="…">…</div> </ng-template> </div> </ng-template> </div></div>Accessibility
- The input is a combo box, the options a list box: focus stays in the input while the highlight moves (aria-activedescendant). Name the input (a label in a field, or aria-label) and the list.
- Down opens the list; Up and Down move the highlight (round with loopFocus), Home and End to its ends; Enter picks; Escape closes, and the input shows the value again.
- Several values: Backspace in the empty input removes the last chip; the arrow towards the chips reaches their remove buttons, the arrows move between them, Backspace or Delete removes one. One tab stop.
- The chevron and the clear button are not tab stops: the keyboard opens with Down and clears by emptying the input.
- With the search inside the popup, the trigger opens a dialog; focus goes to the search, and back to the trigger on Escape or a pick. In a field, the trigger is named by the label and the value it shows, the dialog by the label; without one, name them (aria-labelledby with the value's id on the trigger: an aria-label alone leaves its text out of its name).
- Invalid once touched (aria-invalid on the input, or the trigger), with Signal Forms on the combobox.
Examples
Multiple
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScComboboxChip, ScComboboxChipRemove, ScComboboxChips, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList,Clear
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxClear, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup,Groups
import { Component, ViewEncapsulation } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxGroup, ScComboboxGroupLabel, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList,Custom items
Value: FR
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup, ScComboboxPortal,Invalid
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form, required } from '@angular/forms/signals';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup,Disabled
import { Component, ViewEncapsulation } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup, ScComboboxPortal,Auto highlight
import { Component, ViewEncapsulation } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup, ScComboboxPortal,Popup
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxListTemplate, ScComboboxPopup,Input group
import { Component, ViewEncapsulation } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxGroup, ScComboboxGroupLabel, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList,Form
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form, required } from '@angular/forms/signals';import { ScButton, ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList,Async search
import { Component, ViewEncapsulation, effect, signal, viewChild,} from '@angular/core';import { ScCombobox, ScComboboxEmpty,RTL
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxItemIndicator, ScComboboxList, ScComboboxPopup,API reference
Each part takes class, merged over the style (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScCombobox | div[scCombobox] | One value (null when none): [formField] or [(value)]. [(inputValue)], [(open)], itemToStringLabel (a value's label: the options' texts are only known once the list has opened), isItemEqualToValue, autoHighlight, highlightItemOnHover, loopFocus, openOnInputClick. exportAs scCombobox: query() (the text typed since it opened, to filter by), selectedLabel(), show(), close(), clear(). |
ScComboboxMultiple | div[scCombobox][multiple] | Several values: an array. The same inputs; picking adds or removes and keeps the list open. |
ScComboboxInput | input[scComboboxInput] | The input (Aria's Combobox). With scInput in a scInputGroup: the field. After the chips: the field of several values. Inside the popup: the search a trigger opens. |
ScComboboxTrigger | button[scComboboxTrigger] | Opens and closes the list: the style's button (variant, size). Beside the input, the chevron (ghost, icon-xs) — not a tab stop. With no input in the field, the field itself (outline, as wide as the combobox), holding a scComboboxValue; in a scField, named by its label and its value. |
ScComboboxValue | span[scComboboxValue] | In a trigger: the selected value's label, or its placeholder; your content shows the value your way. |
ScComboboxClear | button[scComboboxClear] | Clears the value: the style's button, shown only while there is a value, in the trigger's place. Write its icon and name it. |
ScComboboxChips, ScComboboxChip, ScComboboxChipRemove | div[…], span[scComboboxChip], button[…] | The field of several values, a chip for each (value), and its remove button (write its icon and name it). |
ScComboboxPortal, ScComboboxPopup | ng-template[…], div[scComboboxPopup] | The popup, rendered while open: as wide as its field, below it (above without room). data-empty while no option is rendered. With the search inside, a dialog named by the field's label (or its aria-label). |
ScComboboxListTemplate | ng-template[scComboboxListTemplate] | The list, rendered by the search beside it: only when the search is inside the popup. |
ScComboboxList | div[scComboboxList] | The options (Aria's Listbox), scrolling past its height; its scrollbar shown (hideScrollbar hides it, as shadcn). Name it. |
ScComboboxItem, ScComboboxItemIndicator | div[scComboboxItem], span[…] | An option (Aria's Option): value, label (its text when not set), disabled; data-active="true" highlighted. The indicator, round your check icon, shows on a selected one. |
ScComboboxGroup, ScComboboxGroupLabel, ScComboboxSeparator | div[…] | Options in a group named by its label; a line between groups. |
ScComboboxEmpty, ScComboboxStatus | div[…] | Your text when no option is rendered; a text read out and not shown, for the results or a search running. |
scComboboxMatches | function | (text, query) => boolean: a match, accents and case ignored — for filtering your options by query(). |