Primitives
The Combobox primitive is a combination of a dropdown and an input field. It allows users to select from a list of options while filtering the list based on their input.
Import the Combobox primitives from ng-primitives/combobox.
import {
NgpCombobox,
NgpComboboxButton,
NgpComboboxDropdown,
NgpComboboxInput,
NgpComboboxOption,
NgpComboboxPortal,
} from 'ng-primitives/combobox';
Assemble the combobox directives in your template.
<div ngpCombobox>
<input ngpComboboxInput />
<button ngpComboboxButton>▼</button>
<div ngpComboboxDropdown>
@for (option of options; track option) {
<div ngpComboboxOption [ngpComboboxOptionValue]="option">{{ option }}</div>
}
</div>
</div>
You can also create a combobox without an input field, which is useful for select-like behavior with keyboard navigation:
<div ngpCombobox>
<button ngpComboboxButton>{{ selectedOption || 'Select an option' }} ▼</button>
<div ngpComboboxDropdown>
@for (option of options; track option) {
<div ngpComboboxOption [ngpComboboxOptionValue]="option">{{ option }}</div>
}
</div>
</div>
When no input is present, the combobox element itself becomes focusable and supports full keyboard navigation.
Create a reusable component that uses the NgpCombobox directive.
This example demonstrates a combobox without an input field. The combobox element itself becomes focusable.
The combobox supports a "Select All" option for multiple selection mode, allowing users to select or deselect all options at once.
To implement Select All, use the special value 'all' for your Select All option:
<div ngpComboboxOptionValue="all" ngpComboboxOption>Select All</div>
When dealing with large datasets (thousands of items), you can use TanStack Virtual or other virtualization libraries to efficiently render only the visible options, improving performance:
Options without a value do not perform any selection by default. You can use this to implement custom behavior, such as clearing the selection. These options are still included in keyboard navigation.
Generate a reusable combobox component using the Angular CLI.
ng g ng-primitives:primitive combobox
path: The path at which to create the component file.prefix: The prefix to apply to the generated component selector.componentSuffix: The suffix to apply to the generated component class name.fileSuffix: The suffix to apply to the generated component file name. Defaults to component.exampleStyles: Whether to include example styles in the generated component file. Defaults to true.The following directives are available to import from the ng-primitives/combobox package:
The main container for the combobox.
The following data attributes are applied to the ngpCombobox directive:
| Attribute | Description |
|---|---|
data-open |
Applied when the combobox is open. |
data-disabled |
Applied when the combobox is disabled. |
data-multiple |
Applied when the combobox is in multiple mode. |
data-hover |
Applied when the combobox is hovered. |
data-press |
Applied when the combobox is pressed. |
data-focus |
Applied when the combobox has focus within it. |
data-invalid |
Applied when the combobox is invalid. |
data-valid |
Applied when the combobox is valid. |
data-touched |
Applied when the combobox has been touched. |
data-pristine |
Applied when the combobox is pristine (not modified). |
data-dirty |
Applied when the combobox has been modified. |
data-pending |
Applied when the combobox is pending (e.g., async validation). |
data-disabled |
Applied when the combobox is disabled. |
When no ngpComboboxInput is present, the combobox element itself receives:
tabindex="0" to make it focusable via keyboard navigationtabindex="-1" when disabled or when an input is presentThe button that toggles the combobox dropdown.
The following data attributes are applied to the ngpComboboxButton directive:
| Attribute | Description |
|---|---|
data-open |
Applied when the combobox is open. |
data-disabled |
Applied when the combobox is disabled. |
data-multiple |
Applied when the combobox is in multiple mode. |
The dropdown that contains the combobox options.
The following CSS custom properties are applied to the ngpComboboxDropdown directive:
| Property | Description |
|---|---|
--ngp-combobox-transform-origin |
The transform origin for the dropdown animation. |
--ngp-combobox-width |
The width of the combobox dropdown. |
--ngp-combobox-input-width |
The width of the combobox input field. |
--ngp-combobox-button-width |
The width of the combobox button. |
The input field for the combobox.
The following data attributes are applied to the ngpComboboxInput directive:
| Attribute | Description |
|---|---|
data-open |
Applied when the combobox is open. |
data-disabled |
Applied when the combobox is disabled. |
data-multiple |
Applied when the combobox is in multiple mode. |
data-invalid |
Applied when the input is invalid. |
data-valid |
Applied when the input is valid. |
data-touched |
Applied when the input has been touched. |
data-pristine |
Applied when the input is pristine (not modified). |
data-dirty |
Applied when the input has been modified. |
data-pending |
Applied when the input is pending (e.g., async validation). |
data-disabled |
Applied when the input is disabled. |
The individual options within the combobox dropdown.
The following data attributes are applied to the ngpComboboxOption directive:
| Attribute | Description |
|---|---|
data-selected |
Applied when the option is selected. |
data-active |
Applied when the option is active. |
data-disabled |
Applied when the option is disabled. |
The portal for rendering the combobox dropdown in an overlay.
The ngpComboboxDropdown primitive adds a CSS custom property --ngp-combobox-transform-origin to the element that can be used to animate the menu from the trigger element.
The ngpComboboxDropdown will also add the data-enter and data-exit attributes to the element when it is being added or removed from the DOM. This can be used to trigger animations.
:host[data-enter] {
animation: fade-in 0.2s ease-in-out;
}
:host[data-exit] {
animation: fade-out 0.2s ease-in-out;
}
You can configure the default options for all comboboxes in your application by using the provideComboboxConfig function in a providers array.
import { provideComboboxConfig } from 'ng-primitives/combobox';
bootstrapApplication(AppComponent, {
providers: [provideComboboxConfig({ placement: 'bottom', container: document.body })],
});
Adheres to the WAI-ARIA guidelines for comboboxes.
The combobox supports comprehensive keyboard navigation whether or not an input field is present:
When no ngpComboboxInput is present, the combobox container becomes focusable and supports:
Copyright © 2026 Angular Primitives
This site is powered by Netlify