All components

Star Rating

A component that displays ratings with stars and an optional label. This component builds trust with visual feedback.

Pro Marketing Figma ReactFree
Open in New Tab

components/shadcncraft/pro-marketing/star-rating.tsx

import { Star } from "lucide-react";
import * as React from "react";

import { cn } from "@/lib/utils";

type StarRatingProps = {
  value: number;
  max?: number;
  label?: string;
  size?: "sm" | "md" | "lg";
  orientation?: "vertical" | "horizontal";
  /**
   * Precision for partial stars. If set, the component will fill each star
   * proportionally (e.g., 3.2 -> 60% of the 4th star). Defaults to true.
   */
  allowPartial?: boolean;
  containerClassName?: string;
};

export function StarRating({
  value,
  max = 5,
  label,
  size = "lg",
  orientation = "vertical",
  allowPartial = true,
  className,
  containerClassName,
  ...props
}: StarRatingProps & Omit<React.ComponentProps<"div">, "children">) {
  const safeMax = Math.max(1, Math.floor(max));
  const clampedValue = Math.max(0, Math.min(value, safeMax));

  return (
    <div
      data-size={size}
      data-orientation={orientation}
      data-slot="star-rating"
      className={cn(
        "flex flex-col items-center gap-1.5",
        "data-[orientation=horizontal]:flex-row",
        "data-[size=lg]:[--star-size:calc(--spacing(5))] data-[size=md]:[--star-size:calc(--spacing(4))] data-[size=sm]:[--star-size:calc(--spacing(3))]",
        containerClassName
      )}
      aria-label={`${clampedValue} out of ${safeMax} stars`}
      role="img"
      {...props}
    >
      <div className={cn("flex items-center gap-0.5", className)}>
        {Array.from({ length: safeMax }).map((_, index) => {
          const starIndex = index + 1;
          const filledRatio = allowPartial
            ? Math.max(0, Math.min(1, clampedValue - index))
            : clampedValue >= starIndex
              ? 1
              : 0;

          return (
            <span key={index} className="relative inline-flex" aria-hidden="true">
              {/* Base (unfilled) star */}
              <Star className="size-(--star-size)" />
              {/* Filled overlay */}
              {filledRatio > 0 ? (
                <span
                  className="absolute inset-0 overflow-hidden"
                  style={{ width: `${filledRatio * 100}%` }}
                >
                  <Star fill="currentColor" className="size-(--star-size)" />
                </span>
              ) : null}
            </span>
          );
        })}
      </div>

      {label && (
        <span data-slot="star-rating-label" className="text-xs text-muted-foreground">
          {label}
        </span>
      )}
    </div>
  );
}

Star Rating is available as part of our Pro and Pro + React Design Systems. Get Star Rating

Benefits

Placeholder title

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Esse rerum voluptatum id. Facilis commodi explicabo recusandae.

Structured Layouts

Clear grids, consistent spacing, and predictable hierarchy.

Marketing + App Coverage

Landing pages, dashboards, onboarding, pricing, e-commerce, and more.

Theme Compatible

Works seamlessly with semantic tokens and theming.

Production-Ready States

Designed with real content, tokenized spacing, and real interaction patterns.

Figma + React Parity (Pro + React)

Design in Figma and ship with aligned React components.

Composable by Design

Mix blocks together to create complete pages without layout conflicts.

Frequently Asked Questions

Quick FAQs to get you started.
Still have questions? See all FAQs, or get support.

Yes. They follow shadcn/ui structure and are built for real products.

SBIOTJ

Join 3,000+ builders shipping with shadcncraft

Design and assemble full pages faster with production-ready blocks built for real products.

  • Production-ready blocks and components
  • Clean React and Tailwind parity
  • Built for SaaS, marketing, and ecommerce teams
Get the design system
Join Now CTA

Real support from the team behind shadcncraft

Get help within 24 hours from the people who build and maintain the system.

Email
Prefer a direct line? Send us a message and we'll get back to you as soon as possible.
Discord
Get quick support, share feedback, or connect with other builders.
Feedback
Got something to say about anything shadcncraft? We'd love to hear it.