Forms
Search
Animated search field, with recent searches, a rolling placeholder and a "no results" state.
Example
Type in the field, pick a recent search, or look for a word that is not in the list ("xyzzy") to see the "no results" state. Results are children of CrSearch: they are shown under the field.
'use client';
import { useState } from 'react';
import { CrSearch } from '@cristal-ui/react';
const products = ['Lamp', 'Chair', 'Rug', 'Sofa', 'Vase', 'Clock'];
export default function SearchBasic() {
const [query, setQuery] = useState('');
const found = products.filter((product) => product.toLowerCase().includes(query.trim().toLowerCase()));
return (
<CrSearch
label="Search for a product"
placeholder="Search"
placeholderWords={['lamps', 'chairs', 'rugs']}
recent={['lamp', 'chair', 'rug']}
value={query}
noResults={found.length === 0}
onCrInput={(event) => setQuery(event.detail)}
style={{ width: '100%', maxWidth: 420 }}
>
{query && found.length > 0 && (
<ul>
{found.map((product) => (
<li key={product}>{product}</li>
))}
</ul>
)}
</CrSearch>
);
}Styles
The variant property changes how the field looks:
| Value | Effect |
|---|---|
outline | bordered pill; on entry, the border takes the primary colour with a halo (default) |
filled | tinted background and a bottom line, which thickens on entry |
underline | only a bottom line, for a discreet field in a header |
expand | a round magnifier button that stretches into a field when entered, and closes back when left empty |
import { CrBox, CrSearch } from '@cristal-ui/react';
const variants = ['outline', 'filled', 'underline', 'expand'] as const;
export default function SearchVariants() {
return (
<CrBox display="flex" direction="column" gap="xl" style={{ width: '100%', maxWidth: 420 }}>
{variants.map((variant) => (
<CrSearch key={variant} variant={variant} label={`Search, ${variant} style`} placeholder="Search" />
))}
</CrBox>
);
}What moves
| Moment | Animation |
|---|---|
| Entering the field | Icon to Arrow: the magnifier becomes an arrow (the circle fades, the handle swings into a shaft, the head is drawn); clicking the arrow clears the search |
Empty field, with placeholderWords | Placeholder Roll: the last word of the placeholder rolls up every second ("Search lamps", then "chairs"…) |
Empty field, with recent | Recent Chips: recent searches show as chips; clicking one flies it into the field and runs the search |
noResults | No Results: the magnifier shakes its head, a "?" pops up inside, with "No results for "…"" and a tip |
CrLoader variant="search" (see Loading) shows a gliding magnifier while searching.
If the user asked their system to reduce motion, the magnifier switches icon without transition, the placeholder does not roll, chips do not fly and the "no results" magnifier stays still.
Accessibility
- The field is a search field (
type="search") named bylabel, which is required since there is no visible label. - The arrow is a "Clear the search" button; while the magnifier is shown, it is decorative and out of the Tab order. Escape also clears the search.
- Recent searches are a list of buttons, titled "Recent".
- The "no results" state is announced by a status region.
- The placeholder word changes at most four times and then stops: nothing moves for more than 5 seconds unless the user asked for it.
- Enter emits
onCrSubmitand submits the parent form; withname, the text is sent with the form.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Désactive le champ. |
label (obligatoire) | string | — | Nom du champ lu par les lecteurs d'écran (le champ n'a pas de libellé visible). |
name | string | — | Nom envoyé avec le formulaire. |
noResults | boolean | false | Affiche l'état « aucun résultat » pour le texte recherché. |
placeholder | string | — | Texte indicatif affiché quand le champ est vide (« Rechercher »). |
placeholderWords | string[] | — | Mots qui défilent après le texte indicatif, un par seconde (« Rechercher chaussures », puis « sacs »…), au plus quatre fois. |
recent | string[] | — | Recherches récentes, en puces sous le champ vide ; un clic fait voler la puce dans le champ. |
value | string | '' | Texte recherché. |
variant | "expand" | "filled" | "outline" | "underline" | 'outline' | Style : outline (pilule bordée, par défaut), filled (fond teinté, trait en bas), underline (trait en bas seulement) ou expand (bouton loupe rond qui s'ouvre en champ quand on y entre). |
Events
| React | event.detail | Description |
|---|---|---|
onCrInput | string | Émis à chaque frappe, au choix d'une recherche récente et à l'effacement ; detail : le texte. |
onCrSubmit | string | Émis sur Entrée ; detail : le texte. Le formulaire parent est aussi envoyé. |
Slots
| Slot | Description |
|---|---|
| default | Les résultats, affichés sous le champ. |
CSS parts
To style it from outside: cr-search::part(name) { … }
| Part | Description |
|---|---|
chip | Une recherche récente. |
control | Le champ. |
empty | L'état « aucun résultat ». |
icon | Le bouton loupe / flèche. |