Skip to content
ProLifetime access for $199, paid once, every future release included
Truffle UI

Rating Badge

ProNew

A compact trust badge with the source mark, score, stars and review count, inline or floating in a corner.

Installation

pnpm dlx shadcn@latest add @viewport/rating-badge

First time? Add the @viewport registry to components.json. Setup takes one minute.

Usage

The code behind the preview above comes with Truffle Pro.

Props

PropTypeDefault
rating*number

Average rating, from 0 to `max`.

—
totalnumber

Number of reviews behind the rating. `0` shows the empty state.

—
maxnumber

Top of the scale.

5
sourceBrand

Review site the rating comes from. Its mark leads the badge.

—
sourcesBrand[]

Several sites, shown as a stack of marks after the count.

—
variant"stars" | "score" | "card"

`stars` is score, stars and count in a row; `score` is one star and "4.8/5"; `card` stacks a title, a large score and the count.

"stars"
size"sm" | "md" | "lg""md"
layout"inline" | "floating"

`inline` sits in the text flow; `floating` pins the badge to a corner.

"inline"
position"bottom-left" | "bottom-right" | "top-left" | "top-right"

Corner used by the floating layout.

"bottom-left"
anchor"viewport" | "parent"

What the floating badge pins to: the viewport, or the nearest positioned ancestor.

"viewport"
titlestring

Title line of the `card` variant. Defaults to "Google rating" for a Google source.

—
formatTotal(total: number) => string

Text for the count, e.g. to translate it.

defaultTotal
hrefstring

Turns the badge into a link, e.g. to your reviews page.

—
onClick() => void

Turns the badge into a button.

—
onDismiss() => void

Adds a close button to the floating badge. Called once it has been dismissed.

—
animatedboolean

Count the score up and fill the stars when the badge scrolls into view.

true
loadingboolean

Shows a skeleton of the same size while the rating loads.

false
colorstring

Star color. Any CSS color.

"#f5b301"
classNamestring—

Accessibility

  • The whole badge reads as one phrase: “Rated 4.5 out of 5 from 1,720 reviews on Google”.
  • With `href` it is a link and with `onClick` a button; otherwise it is a single image.
  • The floating badge has a named Dismiss button that shows on hover, on focus and always on touch screens.
  • While loading it is a status named “Loading rating”. Under reduced motion the score and stars appear filled, with no count-up.