# Search bar

The **search bar** allows users to quickly find content using free text.

## Usage

![Search bar](<../images/search-bar.png>)

### When to use

- Use it if a full text search is required.

### Best practices

- Ignore punctuation within searches.
- Combine with the [typeahead behavior](<https://element.siemens.io/components/sorting-filtering/typeahead/>) to assist users when searching.
- Don't use wildcard encoding for more complex search statements.
- Consider adding other search capabilities according to project needs (e.g. use quotes to search for exact matches).
- The placement of the *Search Bar* depends on the layout of the viewport. See the [list-details](<https://element.siemens.io/components/layout-navigation/list-details/>) for placement guidance. See [data table](<https://element.siemens.io/components/lists-tables-trees/overview/>) for search placement within a data table.

## Design

### Elements

![Search bar elements](<../images/search-bar-elements.png>)

> 1. Search icon, 2. Content, 3. Container, 4. Clear button

### Interaction states

The search bar container color should always be in contrast with its surrounding background. Use `background-2` or `background-1` accordingly.

![search bar states](<../images/search-bar-states.png>)

## Code

Use a native input with `siSearchBarInput` inside `si-search-bar`. Bind a reactive `FormControl` or a Signal Forms field to the input to manage its value, validation, and disabled state. Observe form value changes to trigger searches and debounce them in application code when needed.

Set `[showIcon]="true"` on `si-search-bar` to display a search icon. The icon is hidden by default.

### Usage

```
import {
  SiSearchBarComponent,
  SiSearchBarInputDirective
} from '@siemens/element-ng/search-bar';

@Component({
  imports: [SiSearchBarComponent, SiSearchBarInputDirective, ...]
})
```

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { Component, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, ReactiveFormsModule, Validators } from '@angular/forms';
import { SiSearchBarComponent, SiSearchBarInputDirective } from '@siemens/element-ng/search-bar';
import { LOG_EVENT } from '@siemens/live-preview';
import { debounceTime, filter } from 'rxjs';

@Component({
  selector: 'app-sample',
  imports: [SiSearchBarComponent, SiSearchBarInputDirective, ReactiveFormsModule],
  templateUrl: './si-search-bar.html',
  host: { class: 'p-5' }
})
export class SampleComponent {
  protected readonly logEvent = inject(LOG_EVENT);

  control = new FormControl('', {
    nonNullable: true,
    validators: Validators.pattern(/^[^*?]*$/)
  });
  base0Control = new FormControl('', {
    nonNullable: true,
    validators: Validators.pattern(/^[^*?]*$/)
  });

  constructor() {
    for (const control of [this.control, this.base0Control]) {
      control.valueChanges
        .pipe(
          filter(() => control.valid),
          debounceTime(400),
          takeUntilDestroyed()
        )
        .subscribe(this.logEvent);
    }
  }
}
```

Template:

```html
<h4>Variant - base-1 (using FormControl)</h4>
<div class="pb-8 mx-4 background-0">
  <si-search-bar [showIcon]="true">
    <input siSearchBarInput placeholder="Placeholder" [formControl]="control" />
  </si-search-bar>
</div>

<h4>Variant - base-0</h4>
<div class="py-8 px-4 background-1 clearfix">
  <si-search-bar colorVariant="base-0" [showIcon]="true">
    <input siSearchBarInput placeholder="Placeholder" [formControl]="base0Control" />
  </si-search-bar>
</div>
```

### Projected input

Apply input settings directly to the projected input. Keep appearance settings, such as `showIcon` and `colorVariant`, on `si-search-bar`:

```
<si-search-bar [showIcon]="true">
  <input
    siSearchBarInput
    aria-label="Search users"
    placeholder="Search users"
    maxlength="40"
    [readonly]="readonly"
    [disabled]="disabled"
  />
</si-search-bar>
```

Provide an accessible name with `aria-label` or an associated `<label>`; the placeholder does not replace a label. Native attributes such as `autocomplete` can also be set directly on the input.

### Migrate deprecated inputs and outputs

Run the following schematic to migrate the deprecated inputs and outputs of `si-search-bar` to a projected input. Use `--path` to limit the migration to a directory.

```
ng g @siemens/element-ng:migrate-search-bar
```

The schematic:

- Moves `placeholder`, `maxlength`, `readonly`, `disabled`, and `value`, as well as form, typeahead, and `aria-*` bindings, to a projected `<input siSearchBarInput>`.
- Replaces `(searchChange)` with a Signal Forms field in the component class. The `debounce` rule replaces `debounceTime`, the `pattern` rule replaces `prohibitedCharacters`, and an `effect` calls the former handler when the search term changes.
- Adds `SiSearchBarInputDirective`, and `FormField` if needed, to the imports of the component or NgModule.

By default, the search bar debounces form value changes by 400 ms. A projected input updates its form value immediately, so the schematic logs each migrated search bar with a form binding that loses this debounce. Debounce the value changes in your code where needed.

The schematic also logs search bars that need a manual migration, for example if the `(searchChange)` handler uses template variables or is inside a `@for` block or conditional control flow (`@if`, `@switch`, or `*ngIf`). Conditional handlers stay unchanged to preserve template type narrowing.

### Search Bar - Input Value

Set the initial value on the projected input's `FormControl` or Signal Forms model. The clear button resets the bound form value to an empty string.

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { Component, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { SiSearchBarComponent, SiSearchBarInputDirective } from '@siemens/element-ng/search-bar';
import { LOG_EVENT } from '@siemens/live-preview';
import { debounceTime } from 'rxjs';

@Component({
  selector: 'app-sample',
  imports: [SiSearchBarComponent, SiSearchBarInputDirective, ReactiveFormsModule],
  templateUrl: './si-search-bar-value.html',
  host: { class: 'p-5' }
})
export class SampleComponent {
  logEvent = inject(LOG_EVENT);
  control = new FormControl('hello123', { nonNullable: true });

  constructor() {
    this.control.valueChanges
      .pipe(debounceTime(400), takeUntilDestroyed())
      .subscribe(this.logEvent);
  }
}
```

Template:

```html
<si-search-bar>
  <input siSearchBarInput placeholder="Placeholder" [formControl]="control" />
</si-search-bar>
```

### [SiSearchBarComponent](<https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L47-L325>)

**Kind:** component

**Selector:** `si-search-bar`

```
import { SiSearchBarComponent } from '@siemens/element-ng/search-bar';
```

```
component SiSearchBarComponent implements OnInit, OnDestroy, ControlValueAccessor, OnChanges
```

#### Input Properties

<table><thead><tr><th>Name</th><th>Type</th><th>Default</th><th>Description</th></tr></thead> <tbody><tr id="SiSearchBarComponent.clearButtonAriaLabel"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.clearButtonAriaLabel" id="t:SiSearchBarComponent.clearButtonAriaLabel"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L177-L177">clearButtonAriaLabel</a></td> <td class="api-column-type"><code><a href="index.html#t:TranslatableString">TranslatableString</a></code></td> <td class="api-column-default"><code>$localize`:@@SI_SEARCH_BAR.CLEAR_BUTTON:Clear`</code></td><td class="api-column-description"><p>Aria label for the clear button.</p></td></tr><tr id="SiSearchBarComponent.colorVariant"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.colorVariant" id="t:SiSearchBarComponent.colorVariant"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L138-L138">colorVariant</a></td> <td class="api-column-type"><code><a href="index.html#t:BackgroundColorVariant">BackgroundColorVariant</a></code></td> <td class="api-column-default"><code>'base-1'</code></td><td class="api-column-description"><p>Color to use for component background</p></td></tr><tr id="SiSearchBarComponent.debounceTime"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.debounceTime" id="t:SiSearchBarComponent.debounceTime"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L69-L69">debounceTime</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></code></td> <td class="api-column-default"><code>400</code></td><td class="api-column-description"><p>Time unit change of search input takes effect.</p><p>Deprecated: Use a content-projected input and debounce its form value changes in application code, for example with RxJS <code>debounceTime</code> on <code>FormControl.valueChanges</code>:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput [formControl]=&quot;searchControl&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre><pre><code class="language-typescript">searchControl.valueChanges.pipe(debounceTime(400));</code></pre></td></tr><tr id="SiSearchBarComponent.disabled"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.disabled" id="t:SiSearchBarComponent.disabled"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L153-L153">disabled</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></td> <td class="api-column-default"><code>false</code></td><td class="api-column-description"><p>Whether the input is disabled.</p><p>Deprecated: Use a content-projected input with <code>disabled</code> applied directly to it:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput disabled&gt;&#10;&lt;/si-search-bar&gt;</code></pre></td></tr><tr id="SiSearchBarComponent.maxlength"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.maxlength" id="t:SiSearchBarComponent.maxlength"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L167-L167">maxlength</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></code></td> <td class="api-column-default"><code>undefined</code></td><td class="api-column-description"><p>Defines the maximum length of the search input.</p><p>Deprecated: Use a content-projected input with <code>maxlength</code> applied directly to it:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput maxlength=&quot;100&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre></td></tr><tr id="SiSearchBarComponent.placeholder"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.placeholder" id="t:SiSearchBarComponent.placeholder"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L100-L100">placeholder</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code></td> <td class="api-column-default"><code>''</code></td><td class="api-column-description"><p>Define search input placeholder.</p><p>Deprecated: Use a content-projected input with <code>placeholder</code> applied directly to it:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput placeholder=&quot;Search&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre></td></tr><tr id="SiSearchBarComponent.prohibitedCharacters"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.prohibitedCharacters" id="t:SiSearchBarComponent.prohibitedCharacters"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L87-L87">prohibitedCharacters</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined">undefined</a></code></td> <td class="api-column-default"></td><td class="api-column-description"><p>Prohibited characters restricting search.</p><p>Deprecated: Use a reactive <code>FormControl</code> or Signal Forms on a content-projected input to validate prohibited characters:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput [formControl]=&quot;searchControl&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre><p>Or with Signal Forms:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput [formField]=&quot;searchForm.search&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre></td></tr><tr id="SiSearchBarComponent.readonly"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.readonly" id="t:SiSearchBarComponent.readonly"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L132-L132">readonly</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></td> <td class="api-column-default"><code>false</code></td><td class="api-column-description"><p>Whether the input can be focused but not edited.</p><p>Deprecated: Use a content-projected input with <code>readonly</code> applied directly to it:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput readonly&gt;&#10;&lt;/si-search-bar&gt;</code></pre></td></tr><tr id="SiSearchBarComponent.showIcon"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.showIcon" id="t:SiSearchBarComponent.showIcon"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L106-L106">showIcon</a></td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></td> <td class="api-column-default"><code>false</code></td><td class="api-column-description"><p>Display search icon before search input.</p></td></tr><tr id="SiSearchBarComponent.value"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.value" id="t:SiSearchBarComponent.value"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L118-L118">value</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined">undefined</a></code></td> <td class="api-column-default"></td><td class="api-column-description"><p>Define search input content.</p><p>Deprecated: Use a content-projected input with <code>value</code> applied directly to it:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput value=&quot;Search term&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre></td></tr></tbody></table>

#### Output Properties

<table><thead><tr><th>Name</th><th>Type</th><th>Description</th></tr></thead> <tbody><tr id="SiSearchBarComponent.searchChange"> <td class="api-column-name"><span data-api-type-target-for="SiSearchBarComponent.searchChange" id="t:SiSearchBarComponent.searchChange"></span><a href="https://github.com/siemens/element/blob/main/projects/element-ng/search-bar/si-search-bar.component.ts#L196-L196">searchChange</a> (deprecated)</td> <td class="api-column-type"><code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code></td> <td class="api-column-description"><p>Output callback event will provide you with search term if search input changes.</p><p>Deprecated: Use a content-projected input and observe <code>FormControl.valueChanges</code> or the field value signal with Signal Forms:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput [formControl]=&quot;searchControl&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre><p>Or with Signal Forms:</p><pre><code class="language-html">&lt;si-search-bar&gt;&#10;  &lt;input siSearchBarInput [formField]=&quot;searchForm.search&quot;&gt;&#10;&lt;/si-search-bar&gt;</code></pre></td></tr></tbody></table>

### Types Documentation

### [BackgroundColorVariant](<https://github.com/siemens/element/blob/main/projects/element-ng/common/models/color-variant.model.ts#L8-L8>)

**Kind:** type alias

```
import type { BackgroundColorVariant } from '@siemens/element-ng/common';
```

Use this type with filter components `colorVariant` to render component variants based on background of container

```
type BackgroundColorVariant = 'base-1' | 'base-0'
```

### [TranslatableString](<https://github.com/siemens/element/blob/main/projects/element-translate-ng/translate-types/global.types.ts#L18-L18>)

**Kind:** type alias

```
import type { TranslatableString } from '@siemens/element-translate-ng/translate-types';
```

Represents a translatable string. This can either be a translation key, e.g. `ACTIONS.EDIT` that will be automatically translated when displayed on the UI or a pre-translated string, e.g. `Edit`. Equivalent to a normal string in usage and functionality.

```
type TranslatableString = string & Translatable
```
