Skip to Content
Cristal

Feedback

Alert and toast

Message for information, success, warning or error, placed in the page or as a toast in a corner.

<cr-alert><cr-toaster>CrAlertCrToaster

cr-alert shows a message with an icon and an optional heading. Where you place it decides how it behaves: in the page, it is an alert banner; inside a cr-toaster, it is a toast that closes by itself.

variantUse
infoneutral information (default)
successan action succeeded
warningsomething to watch
dangeran error; read immediately by screen readers

In the page

Placed in the page flow, the alert stays until it is closed. Its icon draws itself; when inserted after the page has loaded (an error on submit, for instance), it unfolds in height. When already there at page load, it does not move, so the page does not shift. With dismissible, a Close button folds it back then hides it, and onCrClose reports it.

Version 2.4 adds PDF export.Changes saved.3 items left in stock.Send
'use client';

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

export default function AlertBasic() {
  const [error, setError] = useState(0);

  return (
    <CrBox display="flex" direction="column" gap="sm">
      <CrAlert heading="New version">Version 2.4 adds PDF export.</CrAlert>
      <CrAlert variant="success">Changes saved.</CrAlert>
      <CrAlert variant="warning" heading="Low stock">
        3 items left in stock.
      </CrAlert>
      {error > 0 && (
        <CrAlert key={error} variant="danger" heading="Sending failed" dismissible onCrClose={() => setError(0)}>
          Check your connection and try again.
        </CrAlert>
      )}
      <CrButton variant="outline" onClick={() => setError((count) => count + 1)}>
        Send
      </CrButton>
    </CrBox>
  );
}

As a toast

Put a single CrToaster area in the page (fixed at the bottom right) and add one CrAlert per message to it: it slides in from the corner, a bar empties for duration milliseconds (5000 by default), then it closes. Remove it from your list in onCrClose. duration and the bar only exist inside a cr-toaster.

With duration={0}, the toast stays until it is closed: prefer it for an error, which must leave time to read and act. The Close button is always there; Escape closes it too, as does dragging it sideways.

InformationSaveSend
'use client';

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

const messages = {
  info: 'A new version is available.',
  success: 'Changes saved.',
  danger: 'Sending failed: check your connection.',
} as const;

type Variant = keyof typeof messages;

export default function AlertToaster() {
  const [toasts, setToasts] = useState<{ id: number; variant: Variant }[]>([]);
  const show = (variant: Variant) => setToasts((list) => [...list, { id: Date.now(), variant }]);

  return (
    <CrBox display="flex" gap="sm">
      <CrButton variant="secondary" onClick={() => show('info')}>
        Information
      </CrButton>
      <CrButton onClick={() => show('success')}>Save</CrButton>
      <CrButton variant="outline" onClick={() => show('danger')}>
        Send
      </CrButton>
      <CrToaster>
        {toasts.map(({ id, variant }) => (
          <CrAlert key={id} variant={variant} duration={variant === 'danger' ? 0 : 5000} onCrClose={() => setToasts((list) => list.filter((toast) => toast.id !== id))}>
            {messages[variant]}
          </CrAlert>
        ))}
      </CrToaster>
    </CrBox>
  );
}

open is true by default and every close sets it back to false, in the page as in a toaster. A single, always-present toast can therefore be driven with open: set your state back to false in onCrClose.

If the user asked their system to reduce motion, the alert appears and disappears at once, the toast appears without sliding and fades out, and the icon is already drawn. The remaining-time bar keeps emptying, slowly and smoothly: it tells how long is left to read.

Accessibility

  • The message is a live region: role="status" (read politely) for info, success and warning, role="alert" (read immediately) for danger.
  • In the page, insert the alert when the error happens so that it is announced; when present at page load, it is read with the page, without interrupting.
  • As a toast, the region exists, empty, before the message so the announcement is not lost. The toast does not take focus: the user can keep working.
  • Time stops while the pointer hovers the toast or focus is inside it (WCAG 2.2.1); the bar stops with it. Dragging always has a keyboard alternative (Escape when focus is inside) and a single-click one (the Close button).
  • The type does not rely on colour alone: each type has its own icon (i, ✓, ! in a triangle, ! in a circle), and a prefix read by screen readers ("Error: ", "Warning: "…) comes before the message.
  • The Close button is named "Close" ("Fermer" in French).
  • The text keeps its full colour while arriving: the alert unfolds, the toast slides, neither fades in.

API

Properties

PropertyTypeDefaultDescription
dismissiblebooleanfalseAjoute un bouton Fermer qui replie et masque le bandeau. Dans un cr-toaster, le bouton est toujours là.
durationnumber5000Dans un cr-toaster : durée d'affichage en millisecondes avant la fermeture automatique ; 0 : le message reste jusqu'à ce qu'on le ferme. Sans effet dans la page.
headingstring—Titre affiché en gras au-dessus du message.
openbooleantrueAffiche le message ; à false, il se ferme (replié dans la page, glissé hors du coin dans un cr-toaster). Chaque fermeture le remet à false.
variant"danger" | "info" | "success" | "warning"'info'Type de message : info, success (réussite), warning (avertissement) ou danger (erreur, lue aussitôt par les lecteurs d'écran).

Events

Reactevent.detailDescription
onCrClosevoidÉmis à chaque fermeture : bouton Fermer, Échap, glissement, fin du temps ou open à false.

Slots

SlotDescription
defaultLe message.

CSS parts

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

PartDescription
alertLa boîte du message.
barLa barre du temps restant, dans un cr-toaster.
closeLe bouton Fermer.
headingLe titre.

CrToaster API

Properties

PropertyTypeDefaultDescription

Slots

SlotDescription
defaultLes cr-alert.

CSS parts

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

PartDescription

On this page