Searchbar
The <smart-searchbar> component provides a debounced search input with an optional toggle button. It follows the Base + Standard + Wrapper pattern with an InjectionToken-based extension mechanism. It renders a default SearchbarStandardComponent which can be replaced via SEARCHBAR_STANDARD_COMPONENT_TOKEN. There is no preset for this component.
Usage
<smart-searchbar [options]="options" [(text)]="query" [(show)]="expanded" />
@if (query()) {
<p>Results for "{{ query() }}"</p>
}
The preview renders the code above in an application that registers every preset (provideSmartPresets(), see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Components
SearchbarComponent (<smart-searchbar>)
Main wrapper component. Renders SearchbarStandardComponent by default. When SEARCHBAR_STANDARD_COMPONENT_TOKEN is provided, renders the injected component via NgComponentOutlet, hands it options, show, text and class, and forwards its show and text changes back, so [(show)] and [(text)] on <smart-searchbar> keep working with any implementation.
SearchbarStandardComponent (<smart-searchbar-standard>)
Default concrete implementation. Renders:
- a Tailwind-styled
<input type="search">bound to the basecontrol(classgoes on this input), with theplaceholderpassed throughTranslatePipe, - a magnifying-glass SVG icon inside the input,
- while
show()isfalse: a toggle button with the same icon whenoptions.showToggleButtonistrue(a click shows the input again), otherwise nothing, smart:dark:*dark-mode classes.
Blurring the input while it is empty sets show to false.
SearchbarBaseComponent (abstract)
Abstract base directive for extending custom searchbar implementations. Exposes options, cssClass (alias class), the show and text models, a control signal wrapping an UntypedFormControl, and the setShow() / tryHide() methods (tryHide() hides the field only while it is empty). The base class wires control.valueChanges through debounceTime() in ngAfterViewInit and writes the settled value into the text model; the debounce is read once there, so a later change of options.debounceTime has no effect. A non-empty text coming from outside is copied into the control without being reported back.
API
Inputs
| Input | Type | Default | Description |
|---|---|---|---|
options | InputSignal<ISearchbarOptions | undefined> | - | Searchbar configuration |
show | ModelSignal<boolean> | true | Whether the input is visible (two-way bindable) |
text | ModelSignal<string> | required | Debounced search text (two-way bindable); bind it, e.g. [(text)] |
class | InputSignal<string> | '' | External CSS classes on the input (alias for cssClass) |
ISearchbarOptions
| Field | Type | Default | Description |
|---|---|---|---|
placeholder | string | 'search' | Placeholder of the input, passed through TranslatePipe (a translation key or text). |
debounceTime | number | 1000 | Milliseconds the typing has to settle before text changes; read once, after the view is created. |
showToggleButton | boolean | - | While show is false, renders a magnifying-glass button that shows the input again. |
label | string | - | Not read by the built-in implementations; available to a custom implementation (e.g. as the input's aria-label). |
size | SmartSize | - | Not read by the built-in implementations; available to a custom implementation. |
color | SmartColor | - | Not read by the built-in implementations; available to a custom implementation. |
SEARCHBAR_STANDARD_COMPONENT_TOKEN
InjectionToken that allows replacing the default SearchbarStandardComponent with a custom implementation. Provide a component class extending SearchbarBaseComponent under SEARCHBAR_STANDARD_COMPONENT_TOKEN; every <smart-searchbar> below that injector renders it.
Extending the base class
import {
ChangeDetectionStrategy,
Component,
computed,
signal,
ViewEncapsulation,
} from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import {
ISearchbarOptions,
SEARCHBAR_STANDARD_COMPONENT_TOKEN,
SearchbarBaseComponent,
SearchbarComponent,
} from '@smartsoft001/angular';
@Component({
selector: 'docs-custom-searchbar',
template: `
@if (show()) {
<div [class]="containerClasses()">
<span class="docs-searchbar__icon" aria-hidden="true">⚲</span>
<input
type="search"
class="docs-searchbar__input"
[formControl]="control()"
[placeholder]="options()?.placeholder ?? 'Search'"
[attr.aria-label]="options()?.label ?? 'Search'"
(blur)="tryHide()"
/>
</div>
} @else if (options()?.showToggleButton) {
<button
type="button"
class="docs-searchbar__toggle"
aria-label="Show the search field"
(click)="setShow()"
>
⚲
</button>
}
`,
imports: [ReactiveFormsModule],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CustomSearchbarComponent extends SearchbarBaseComponent {
// The base class owns the debounced control.valueChanges subscription that
// feeds `text` - do not subscribe again here. `cssClass()` holds the `class`
// given to <smart-searchbar>.
readonly containerClasses = computed(() => {
const classes = ['docs-searchbar'];
const extra = this.cssClass();
if (extra) classes.push(extra);
return classes.join(' ');
});
}
@Component({
selector: 'docs-searchbar-custom-example',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [SearchbarComponent],
// The token swaps the standard searchbar for the custom one everywhere below
// this component, so consumers keep writing `<smart-searchbar>`.
providers: [
{
provide: SEARCHBAR_STANDARD_COMPONENT_TOKEN,
useValue: CustomSearchbarComponent,
},
],
// The wrapper forwards `show` and `text` both ways: the values reach the
// custom component, and its own changes come back to these signals.
template: `
<smart-searchbar [(show)]="show" [(text)]="text" [options]="options" />
@if (text()) {
<p>Results for "{{ text() }}"</p>
}
`,
})
export class SearchbarCustomExampleComponent {
readonly show = signal(true);
readonly text = signal('');
readonly options: ISearchbarOptions = {
placeholder: 'Search invoices',
label: 'Search invoices',
debounceTime: 300,
showToggleButton: true,
};
}
Source
The component lives in packages/shared/angular/src/lib/components/searchbar and is documented for Claude Code by the angular-components-searchbar skill.
SmartSearchbar is a search input whose text is reported once the typing settles (options.debounceTime, 1000 ms by default) through onTextChange. While empty, it hides itself on blur; with options.showToggleButton a magnifying-glass button shows it again. Both text and show can be controlled (text / onTextChange, show / onShowChange) or left to the component (defaultText, defaultShow = true). There is no preset.
Usage
import { useState } from 'react';
import { ISearchbarOptions, SmartSearchbar } from '@smartsoft001/react';
const options: ISearchbarOptions = {
placeholder: 'Search orders',
debounceTime: 300,
showToggleButton: true,
};
export function SearchbarUsageExample() {
// Both values are controlled: the text arrives once the typing settles.
const [query, setQuery] = useState('');
const [expanded, setExpanded] = useState(true);
return (
<>
<SmartSearchbar
options={options}
text={query}
onTextChange={setQuery}
show={expanded}
onShowChange={setExpanded}
/>
{query && <p>{`Results for "${query}"`}</p>}
</>
);
}
The preview renders the code above in an application that registers every preset (SMART_PRESET_COMPONENTS on SmartProvider, see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Exports
All from @smartsoft001/react.
| Export | Kind | What it is |
|---|---|---|
SmartSearchbar | component | Renders the implementation registered as components.searchbar on SmartProvider, SmartSearchbarStandard by default. |
SmartSearchbarStandard | component | The default searchbar: a search input bound to the hook's control, hidden on blur while empty. |
useSearchbar | hook | The behaviour every searchbar variant shares. |
Props and Types
SmartSearchbarProps
| Prop | Type | Default | Description |
|---|---|---|---|
options? | ISearchbarOptions | — | Placeholder, label, debounce and toggle button. |
className? | string | — | Classes on the input. |
show? | boolean | — | Whether the search field is shown. Leave it undefined for an uncontrolled searchbar that starts from defaultShow. |
defaultShow? | boolean | true | Initial show of an uncontrolled searchbar (default true). |
onShowChange? | (show: boolean) => void | — | Called when the search field is shown or hidden. |
text? | string | — | The search text, updated once the typing settles for options.debounceTime ms (1000 by default). Leave it undefined for an uncontrolled searchbar that starts from defaultText. |
defaultText? | string | '' | Initial text of an uncontrolled searchbar. |
onTextChange? | (text: string) => void | — | Called with the search text once the typing settles. |
ISearchbarOptions
| Field | Type | Default | Description |
|---|---|---|---|
placeholder? | string | 'search' | Placeholder of the input, passed through the translations (a key or text). |
label? | string | — | Not read by the built-in implementations; available to a custom implementation (e.g. as the input's aria-label). |
debounceTime? | number | 1000 | Milliseconds the typing has to settle before onTextChange. |
showToggleButton? | boolean | — | Renders a magnifying-glass button while the field is hidden. |
size? | SmartSize | — | Not read by the built-in implementations; available to a custom implementation. |
color? | SmartColor | — | Not read by the built-in implementations; available to a custom implementation. |
SmartColor, SmartSize are described in the react-provider skill.
Replacing the Implementation
SmartSearchbar renders the component registered under the 'searchbar' key of SmartProvider's components, and SmartSearchbarStandard when nothing is registered there. Every SmartSearchbar below the provider then renders the registered component, which receives the same props.
There is no preset for this component: register a component of your own that takes SmartSearchbarProps, as shown below, as components={{ searchbar: MySearchbar }}. Pass components as a stable object (a module constant or a memoised value), or the context changes on every render.
The useSearchbar hook
The behaviour every searchbar variant shares. The field is bound to control. Its changes reach text once they settle for options.debounceTime ms (1000 by default); a non-empty text coming from outside is copied into the control without being reported back. tryHide() (on blur) hides an empty searchbar, setShow() shows it again. show and text are controlled through show / onShowChange and text / onTextChange, or kept internally from defaultShow / defaultText.
| Returns | Type | Description |
|---|---|---|
show | boolean | Whether the field is shown (controlled or internal). |
text | string | The settled search text (controlled or internal). |
control | SmartFormControl<string | null> | The SmartFormControl the input is bound to (see useControlBinding). |
setShow | () => void | Shows the field. |
tryHide | () => void | Hides the field when it is empty (call on blur). |
import { useState } from 'react';
import {
cn,
ISearchbarOptions,
SmartProvider,
SmartSearchbar,
SmartSearchbarProps,
useControlBinding,
useSearchbar,
} from '@smartsoft001/react';
export function CustomSearchbar(props: SmartSearchbarProps) {
const { options, className } = props;
// useSearchbar owns the debounced control that feeds onTextChange and the
// show / hide state; useControlBinding binds the input to that control.
const { show, control, setShow, tryHide } = useSearchbar(props);
const binding = useControlBinding(control);
if (show) {
return (
<div className={cn('docs-searchbar', className)}>
<span className="docs-searchbar__icon" aria-hidden="true">
⚲
</span>
<input
type="search"
className="docs-searchbar__input"
placeholder={options?.placeholder ?? 'Search'}
aria-label={options?.label ?? 'Search'}
value={binding.value ?? ''}
onChange={(event) => binding.onChange(event.target.value)}
onBlur={() => {
binding.onBlur();
tryHide();
}}
/>
</div>
);
}
if (options?.showToggleButton) {
return (
<button
type="button"
className="docs-searchbar__toggle"
aria-label="Show the search field"
onClick={setShow}
>
⚲
</button>
);
}
return null;
}
// A module constant: a new object on every render would change the context.
const components = { searchbar: CustomSearchbar };
const options: ISearchbarOptions = {
placeholder: 'Search invoices',
label: 'Search invoices',
debounceTime: 300,
showToggleButton: true,
};
export function SearchbarCustomExample() {
const [show, setShow] = useState(true);
const [text, setText] = useState('');
// Every SmartSearchbar below the provider renders CustomSearchbar, which
// receives the same props, the change callbacks included.
return (
<SmartProvider components={components}>
<SmartSearchbar
options={options}
show={show}
onShowChange={setShow}
text={text}
onTextChange={setText}
/>
{text && <p>{`Results for "${text}"`}</p>}
</SmartProvider>
);
}
Styling
classNamegoes on the input; the standard input carriessmart:dark:variants.- A non-empty
textcoming from the parent is copied into the field without being reported back.
Source
The component lives in packages/shared/react/src/lib/components/searchbar and is documented for Claude Code by the react-components-searchbar skill.