Primitives

Search

The search primitive is a form control that allows users to enter a search query and clear the input.

Import

Import the Search primitives from ng-primitives/search.

import { NgpSearch, NgpSearchClear } from 'ng-primitives/search';

Usage

Assemble the search directives in your template.

<div ngpFormField>
  <label ngpLabel>Label</label>
  <div ngpSearch>
    <input ngpInput type="search" />
    <button ngpSearchClear ngpButton aria-label="Clear search">Clear</button>
  </div>
</div>

Reusable Component

Create a search component that uses the NgpSearch directive.

import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Search } from './search';

@Component({
  selector: 'app-root',
  imports: [Search, FormsModule],
  template: `
    <app-search [(ngModel)]="query" placeholder="Search customers" />
  `,
})
export default class App {
  readonly query = signal<string>('');
}

Schematics

Generate a reusable search component using the Angular CLI.

ng g ng-primitives:primitive search

Options

  • 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.

API Reference

The following directives are available to import from the ng-primitives/search package:

NgpSearch

The `NgpSearch` directive is a container for the search field components.

  • Selector: [ngpSearch]
  • Exported As: ngpSearch

Data Attributes

Attribute Description
data-empty Applied when the input is empty.

NgpSearchClear

The `NgpSearchClear` directive is can be added to a button to clear the search field on click.

  • Selector: [ngpSearchClear]
  • Exported As: ngpSearchClear

Attribute Description
data-empty Applied when the search field is empty.

Accessibility

Adheres to the Search WAI-ARIA design pattern.

Keyboard Interaction

  • Esc - Clears the search field.

Copyright © 2025 Angular Primitives