Skip to Content
Cristal

Forms

Text field

Text, email, password or number input.

<cr-input>CrInput

Examples

The label is required and is part of the component. The hint text (hint) and the error message (error) are shown between the label and the field, and read by screen readers.

import { CrInput } from '@cristal-ui/react';

export default function InputBasic() {
  return (
    <>
      <CrInput label="Email" type="email" name="email" hint="Format: name@domain.com" autocomplete="email" required />
      <CrInput label="Name" error="Name is required. Enter at least 2 characters." />
      <CrInput label="Reference" value="REF-001" disabled />
    </>
  );
}

Automatic validation

Input errors appear on their own (in English by default, in French with lang="fr") when the user leaves the field after changing it, or when they try to submit the form. They disappear as soon as the input is fixed.

CaseMessage
Empty required fieldThis field is required.
Malformed type="email"Enter a valid email address, for example name@domain.com.
Malformed type="url"Enter a valid web address, for example https://example.com.
Non-numeric type="number"Enter a number.

On submit, focus moves to the first invalid field, and its message is read by screen readers. For an error coming from the server ("this address is already in use"), pass your own message with error: it replaces the automatic message.

Animated error

error-animation (errorAnimation in React) animates the error when it appears, then again on every rejected submit, even when the message is the same. Pick an animation, then click Continue with the field empty or an incomplete address.

Create your account

We will send you a sign-in link.

Continue

'use client';

import { useState } from 'react';
import { CrBox, CrButton, CrInput, CrSegmented } from '@cristal-ui/react';

const animations = [
  ['zigzag', 'Zigzag'],
  ['pop', 'Badge'],
  ['drop', 'Sign'],
  ['echo', 'Echo'],
  ['jitter', 'Letters'],
  ['wobble', 'Jelly'],
] as const;

export default function InputErrorAnimation() {
  const [animation, setAnimation] = useState(0);
  const [sent, setSent] = useState('');

  return (
    <CrBox display="flex" direction="column" gap="xl" align="center">
      <CrSegmented label="Error animation" fit value={String(animation)} onCrChange={(event) => setAnimation(Number(event.detail))}>
        {animations.map(([value, label], index) => (
          <option key={value} value={index}>
            {label}
          </option>
        ))}
      </CrSegmented>
      <form
        style={{ display: 'grid', gap: 24, width: 'min(100%, 360px)', boxSizing: 'border-box', padding: 32, borderRadius: 'var(--cr-radius-l)', background: 'var(--cr-color-surface)', boxShadow: 'var(--cr-shadow-m)' }}
        onSubmit={(event) => {
          event.preventDefault();
          setSent(String(new FormData(event.currentTarget).get('email')));
        }}
      >
        <div>
          <h3 style={{ margin: 0 }}>Create your account</h3>
          <p style={{ margin: '4px 0 0', color: 'var(--cr-color-text-muted)' }}>We will send you a sign-in link.</p>
        </div>
        <CrInput label="Email" name="email" type="email" autocomplete="email" required errorAnimation={animations[animation][0]} onCrInput={() => setSent('')} />
        <CrButton type="submit" fullWidth>
          Continue
        </CrButton>
        <p role="status" style={{ margin: 0 }}>
          {sent && `Link sent to ${sent}.`}
        </p>
      </form>
    </CrBox>
  );
}
error-animationEffect
noneno animation (default)
zigzagthe bottom edge of the field snaps into a buzzing red zigzag, then settles flat
popa red "!" badge springs out at the end of the field with a few sparks, and stays
dropthe message drops in and swings from its top-left corner, like a hanging sign
echothe border flashes red twice and sends out two widening echoes
jitterthe letters of the message hop and shiver one after another
wobblethe field rocks like jelly, then settles

The same prop exists on the text area, one-time code, select, tags, payment card, checkbox, radio group, date picker, quantity picker and file upload. On the checkbox and the radio group, the zigzag runs under the box and its label, the badge sits after them and the whole block wobbles; on the one-time code, the badge follows the last box and the echoes wrap all the boxes; on the file upload, the animation plays on every rejected file.

Screen readers still get the message as one sentence (with jitter, the hopping letters are a hidden copy), and it stays readable at every frame: it moves, but never fades in from transparent. When the user asks for less motion (prefers-reduced-motion), nothing moves: the red border and the message appear at once, with no sparks or echoes. In forced colors mode, the field in error gets a system-coloured outline.

Password strength and completion

  • With strength (for type="password"), a four-segment meter fills up and changes color while typing; the level is written out and announced: "Strength: Good". The score takes into account length, upper and lower case, digits and symbols.
  • With completions, the rest of the first suggestion that extends the input is shown in gray behind the text. Right Arrow or a click on it accepts it: it is filled in, and "Filled in for you" is shown. The field is marked aria-autocomplete="inline" and the suggestion is read with the field description.
import { CrInput } from '@cristal-ui/react';

const emails = ['mia@mail.com', 'mia.smith@company.com', 'lucas@example.com'];

export default function InputExtras() {
  return (
    <>
      <CrInput label="New password" type="password" autocomplete="new-password" strength />
      <CrInput label="Email" type="email" completions={emails} hint="Type “mia”, then press the right arrow to accept the suggestion" />
    </>
  );
}

In a form

Cristal fields take part in the parent <form> like native fields: their value is submitted under their name, required blocks submission, type="reset" restores their initial value, and Enter in a text field submits the form.

Try submitting the empty form, typing an incomplete email address, then filling everything in.

SubmitReset
'use client';

import { useState } from 'react';
import { CrButton, CrCheckbox, CrInput, CrSelect } from '@cristal-ui/react';

export default function FormComplete() {
  const [sent, setSent] = useState('');

  return (
    <form
      className="flex flex-col gap-4"
      onSubmit={(event) => {
        event.preventDefault();
        setSent(JSON.stringify(Object.fromEntries(new FormData(event.currentTarget))));
      }}
    >
      <CrInput label="Email" name="email" type="email" required />
      <CrSelect label="Country" name="country" placeholder="Choose a country" required>
        <option value="us">United States</option>
        <option value="gb">United Kingdom</option>
        <option value="ca">Canada</option>
      </CrSelect>
      <CrCheckbox label="I accept the terms" name="terms" required />
      <div className="flex gap-2">
        <CrButton type="submit">Submit</CrButton>
        <CrButton type="reset" variant="secondary">
          Reset
        </CrButton>
      </div>
      {sent && <output>Submitted data: {sent}</output>}
    </form>
  );
}

Accessibility

  • Label linked to the field: clicking the label puts the cursor in the field.
  • hint and error linked with aria-describedby; error adds aria-invalid="true" and a red border (the error does not rely on color alone: the message is displayed).
  • The asterisk on required fields is hidden from screen readers, which already announce "required".
  • Use autocomplete (email, name, tel…) to help autofill.

API

Properties

PropertyTypeDefaultDescription
autocompletestring—Indication de remplissage automatique du navigateur.
completionsstring[]—Suggestions : la suite de la première qui prolonge la saisie s'affiche en gris ; flèche droite ou clic pour l'accepter.
disabledbooleanfalseDésactive le champ.
errorstring—Message d'erreur ; remplace le message automatique de validation.
errorAnimation"drop" | "echo" | "jitter" | "none" | "pop" | "wobble" | "zigzag"'none'Animation de l'erreur quand elle apparaît et à chaque envoi refusé : none (par défaut), zigzag, pop, drop, echo, jitter ou wobble.
hintstring—Texte d'aide, lu par les lecteurs d'écran.
label (obligatoire)string—Libellé affiché au-dessus du champ.
namestring—Nom envoyé avec le formulaire.
placeholderstring—Texte indicatif affiché quand le champ est vide.
requiredbooleanfalseRend la saisie obligatoire.
strengthbooleanfalseAffiche une jauge de sécurité sous le champ (pour type="password").
type"email" | "number" | "password" | "search" | "tel" | "text" | "url"'text'Type de saisie.
valuestring''Valeur du champ.

Events

Reactevent.detailDescription
onCrChangestringÉmis quand le champ perd le focus après modification ; detail : la valeur.
onCrInputstringÉmis à chaque frappe ; detail : la valeur.

CSS parts

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

PartDescription
controlLe champ natif.
errorLe message d'erreur.
hintLe texte d'aide.
labelLe libellé.

On this page