Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Rating for Astro

Astro star displays for a score, with an optional control to set it.

Default

Interactive

Installation

bunx --bun shadcn@latest add @gnostic/rating

Usage

import { Rating, RatingValue } from "@/components/ui/rating"
<Rating value={4.5} />
<Rating interactive name="rating" value={0} label="Rating">
  <RatingValue />
</Rating>

Rating defaults to 5 when no value is provided. A fraction fills part of the next star.

Examples

Interactive Rating

Set interactive to let the visitor choose a score. Pass value={0} to start with empty stars. Give the control a name when the page submits a form, and a label that matches the visible caption.

Put RatingValue inside Rating to show the saved score beside the stars. It takes no score prop.

Composition

RatingValue is optional. Place it inside the Rating it should show. The rating reads its own range control and writes that score into the value text. One rating has one set of listeners. A second rating on the page does not share them.

A value outside Rating does not update. A hover preview changes the stars only. The number changes when the score is saved.

API Reference

RatingProps

PropType
valuenumber

Score to show. A fraction fills part of the next star.

Typenumber
interactive?boolean

Let the visitor set the score. The component then renders a range control. A click on a star sets a whole number of stars. Arrow keys change that control by one star.

Typeboolean
Defaultfalse
max?number

Number of stars. Values below 1 become 1.

Typenumber
Default5
name?string

Form field name for the range control. Used only when `interactive` is set. The submitted value is a number from 0 through `max`.

Typestring
label?string

Accessible name for the range control. Used only when `interactive` is set. Use the same words in a visible caption.

Typestring
Default"Rating"
disabled?boolean

Block changes to the score. Used only when `interactive` is set. A disabled control is not submitted.

Typeboolean
Defaultfalse
starColor?string

Color of the stars.

Typestring
Default"var(--primary)"
as?HTMLTag

The type of the element to render.

TypeHTMLTag
Default"div"

Div attributes such as class and id pass through. The root element can change with as. The default element is div.

RatingValue

RatingValue has no score prop. Element attributes such as class and id pass through. The root element can change with as. The default element is span. Place it inside Rating.

Read the installed source for the component source.

When to use Rating

Use Rating to show a score. Place the score, scale, and review count in nearby text when they matter. Keep the displayed value consistent with the source data.

Set interactive when the visitor must choose a score. The range control stores the value and sends input and change events.

Keyboard and behavior

A static rating is an image with the text “4 out of 5”. It is not a control.

Set interactive to add a range control. Tab reaches that control. Arrow keys change the score by one star. A pointer click on a star sets that score. Move the pointer across the stars to preview a score. The control name comes from label. RatingValue shows the saved score, not the preview.

A click or an arrow key sets a whole number of stars. The minimum value is 0.

Theme and layout

Check filled and unfilled stars in both modes. Do not use color alone to show the difference. Filled stars use the primary color. Empty stars use a muted color.