Skip to Content
Cristal

Forms

Rating

Star rating, to pick or to display.

<cr-rating>CrRating

Example

cr-rating shows one star per possible rating, from 1 to max (5 by default), under a visible label. A click or the arrow keys pick the rating: stars fill in a wave up to it, and the chosen star bounces. On hover, the stars show the rating a click would give.

Submitted rating: none

'use client';

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

export default function RatingBasic() {
  const [rating, setRating] = useState(0);

  return (
    <CrBox display="flex" direction="column" gap="lg">
      <CrRating label="Rate the delivery" name="review" value={rating} onCrChange={(event) => setRating(event.detail)} />
      <p>Submitted rating: {rating ? `${rating} out of 5` : 'none'}</p>
      <CrRating label="Customer rating" value={4} readonly />
      <CrRating label="Order rating (already sent)" value={2} disabled />
    </CrBox>
  );
}

In a form

The rating is submitted under the component name; nothing is submitted while no star is chosen (value at 0). onCrChange reports every new rating.

With readonly, the rating is displayed and cannot change, for an average for instance. Half stars are not supported: a decimal value is rounded.

If the user asked their system to reduce motion, the stars fill all at once and the chosen star does not bounce.

Accessibility

  • The component follows the radio group pattern: a group named by the visible label, one radio button per star, named "3 stars out of 5", and a single Tab stop, on the chosen rating (or the first star).
  • Right and Up Arrow raise the rating, Left and Down Arrow lower it, wrapping around after the last one; Home and End pick the lowest and the highest.
  • When read-only, the stars form an image named by the rating ("Customer rating: 4 out of 5"), with nothing focusable.
  • A full star is filled, an empty star only has its outline: the rating does not rely on colour alone. The outline stands out from the background with a contrast of at least 3:1.

API

Properties

PropertyTypeDefaultDescription
disabledbooleanfalseDésactive la notation.
label (obligatoire)string—Titre affiché au-dessus des étoiles, et nom du groupe pour les lecteurs d'écran (obligatoire).
maxnumber5Nombre d'étoiles, donc note maximale.
namestring—Nom envoyé avec le formulaire.
readonlybooleanfalseAffiche la note sans permettre de la changer.
valuenumber0Note choisie, de 0 (aucune) à max ; arrondie à l'entier.

Events

Reactevent.detailDescription
onCrChangenumberÉmis quand l'utilisateur change la note ; detail : la nouvelle note.

CSS parts

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

PartDescription
labelLe titre.
starUne étoile (un bouton radio, sauf en lecture seule).

On this page