Skip to Content
Cristal

Forms

Search

Animated search field, with recent searches, a rolling placeholder and a "no results" state.

<cr-search>CrSearch

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:

ValueEffect
outlinebordered pill; on entry, the border takes the primary colour with a halo (default)
filledtinted background and a bottom line, which thickens on entry
underlineonly a bottom line, for a discreet field in a header
expanda 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

MomentAnimation
Entering the fieldIcon 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 placeholderWordsPlaceholder Roll: the last word of the placeholder rolls up every second ("Search lamps", then "chairs"…)
Empty field, with recentRecent Chips: recent searches show as chips; clicking one flies it into the field and runs the search
noResultsNo 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 by label, 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 onCrSubmit and submits the parent form; with name, the text is sent with the form.

API

Properties

PropertyTypeDefaultDescription
disabledbooleanfalseDésactive le champ.
label (obligatoire)string—Nom du champ lu par les lecteurs d'écran (le champ n'a pas de libellé visible).
namestring—Nom envoyé avec le formulaire.
noResultsbooleanfalseAffiche l'état « aucun résultat » pour le texte recherché.
placeholderstring—Texte indicatif affiché quand le champ est vide (« Rechercher »).
placeholderWordsstring[]—Mots qui défilent après le texte indicatif, un par seconde (« Rechercher chaussures », puis « sacs »…), au plus quatre fois.
recentstring[]—Recherches récentes, en puces sous le champ vide ; un clic fait voler la puce dans le champ.
valuestring''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

Reactevent.detailDescription
onCrInputstringÉmis à chaque frappe, au choix d'une recherche récente et à l'effacement ; detail : le texte.
onCrSubmitstringÉmis sur Entrée ; detail : le texte. Le formulaire parent est aussi envoyé.

Slots

SlotDescription
defaultLes résultats, affichés sous le champ.

CSS parts

To style it from outside: cr-search::part(name) { … }

PartDescription
chipUne recherche récente.
controlLe champ.
emptyL'état « aucun résultat ».
iconLe bouton loupe / flèche.

On this page