Rating
A visual component for capturing or displaying rating values, usually represented by stars or other icons.
Usage
Basic usage example to quickly see how the Rating
component works.
This example gives a quick overview of how to implement the Rating
component in a simple use case.
Properties
Interactive configurator to explore customization options for the Rating
component.
Adjust visual and behavioral properties such as maximum value, fractions, and symbols to see how the component responds.
Custom Symbol
Demonstration of using custom icons or elements as rating symbols. Instead of default stars, you can define your own icons or any React nodes for the rating items.
Custom Fractions
Demonstration of configuring fractional steps for rating values. This example shows how to allow partial ratings, like half-stars or custom fractions.
Controlled
Example of a fully controlled Rating
component.
The rating value is stored in a local state and updated manually through the onChange handler.
"use client";
import { useState } from "react";
import { Rating } from "@/ui/rating";
function RatingDemo() {
const [value, setValue] = useState(0);
return <Rating value={value} onChange={setValue} />;
}
Read only
Example of a read-only
Rating
component.
The rating is fixed and cannot be changed by user interaction, useful for displaying existing review scores.
import { Rating } from "@/ui/rating";
function RatingDemo() {
return <Rating value={3.5} fractions={2} readOnly />;
}
API References
Styles API
Styles API | Type | Default | Annotation |
---|---|---|---|
unstyled? | boolean | false | if true , all default styles will be removed |
classNames? | { wrap?: string; inner?: string } | undefined | classNames={{ wrap: "", inner: "" }} |
styles? | { wrap?: CSSProperties; inner?: CSSProperties } | undefined | styles={{ wrap: {}, inner: {} }} |
Props API
Props API | Type | Default | Annotation |
---|---|---|---|
rating? | number | 0 | initial rating |
totalStars? | number | 5 | total number of stars |
onRatingChange? | (rating: number) => void | undefined | callback to receive ratings from users |
interactive? | boolean | false | if true , the star can be clicked to give a rating |
Source Codes
Full working code example, including necessary markup and styles. You can copy and paste this code directly to start using the component immediately.