Components
Star Rating
Display a star rating out of 5.0
Basic examples
Sizes
<> <div> <StarRatingV2 rating={0} /> </div> <div> <StarRatingV2 rating={2.5} /> </div> <div> <StarRatingV2 rating={5} /> </div> <div> <StarRatingV2 rating={0} size="medium" /> </div> <div> <StarRatingV2 rating={2.5} size="medium" /> </div> <div> <StarRatingV2 rating={5} size="medium" /> </div> <div> <StarRatingV2 rating={0} size="large" /> </div> <div> <StarRatingV2 rating={2.5} size="large" /> </div> <div> <StarRatingV2 rating={5} size="large" /> </div> </>
StarRatingV2 with inline text
<div className="flex"> <StarRatingV2 rating={2.7} size="large" /> <span className="ml3 b"> 13 reviews </span> </div>
Event listeners
The hoverRating, onStarClick, onStarHover, and onMouseLeave props make it possible to build an interactive StarRatingV2 component. Providing onStarClick or onStarHover switches the component into its interactive mode.
function StarRatingExample() { const [rating, setRating] = React.useState(3); const [hoverRating, setHoverRating] = React.useState(undefined); return ( <StarRatingV2 size="large" rating={rating} hoverRating={hoverRating} onStarClick={value => { setRating(value); console.log(`StarRatingV2: Clicked on ${value}`); }} onStarHover={value => { setHoverRating(value); console.log(`StarRatingV2: Hovered over ${value}`); }} onMouseLeave={() => { setHoverRating(undefined); console.log('StarRatingV2: onMouseLeave'); }} /> ); }
Props
StarRatingV2
ratingrequiredNumber from 0-5 at increments of 0.5. Numbers between these steps will be rounded.
TypenumberhoverRatingNumber from 0-5 at increments of 1.
hoverRatingtrumpsratingwith respect to star highlighting.Type0 | 1 | 2 | 3 | 4 | 5Default0sizeThe size of the component when rendered.
Type'small' | 'medium' | 'large'Default'small'onStarClickFunction that is called when a user clicks on a star. The function is supplied a single parameter: the value of the clicked star. Providing
onStarClickoronStarHoverswitches the component into its interactive mode.Type(value: number) => voidonStarHoverFunction that is called when a user hovers over a star. The function is supplied a single parameter: the value of the hovered star. Providing
onStarClickoronStarHoverswitches the component into its interactive mode.Type(value: number) => voidonMouseLeaveFunction that is called when a user mouses away from the
StarRatingV2component.Type() => voidaccessibilityLabelOverrides the auto-generated
"{n} stars out of 5 star rating"label for assistive technologies. Useful for context-specific labels (e.g."Rate this pro").TypestringnameForm
namefor the underlying radios when the component is interactive. Defaults to a unique per-instance value so multipleStarRatingV2s on the same page do not form a single shared radio group. Only set this for forms that need to coordinate with other inputs.TypestringdataTestIdA selector hook into the React component for use in automated testing environments.
Typestring