INFINITYUI
HomeComponentsDocsTemplates
Star

Getting Started

  • Introduction
  • Installation
  • CLI
  • Audit

Navigation

  • Spotlight Navbar
  • Glass DockNEW
  • Animated Tab Bar
  • Circle Menu
  • Magnet Tabs
  • Animated Sidebar
  • Apple Spotlight
  • Page TOC RailNEW

Text

  • Flip Text
  • Glitch Text
  • Liquid Text
  • Flip Fade Text
  • Mask Cursor Effect

Cards

  • Glow Border Card
  • Testimonials Card
  • Interactive Book
  • Trading CardsNEW
  • Hover Image
  • Chain of ThoughtNEW
  • Masonry Grid
  • Image Pile
  • Staggered Grid

Inputs

  • AI InputNEW
  • OTP Input
  • Leave Rating

Buttons

  • Social Flip Button
  • Creepy Button

Loaders

  • Jelly Loader
  • Rolling Ball Scroll
  • Glowing Scroll

Backgrounds

  • Light Lines
  • Perspective Grid
  • Liquid Ocean
  • Eagle Vision
  • Flow Scroll
  • Horizontal Scroll

Overlays

  • PersonaNEW
  • Infinite Moving Cards
  • Masked Avatars
  • Stacked Logos
  • Icon Wheel
  • Pixelated CarouselNEW
  • Pixelated Image Trail
  • Flip Scroll
  • Interactive Folder
  • Animated Folder IconNEW
  • Stack Scroll
  • Rubik Cube
Inputs

Leave Rating

#input#rating#stars

How would you rate this?

implementedInfinityUI component

This component is fully implemented in InfinityUI and wired into the docs and registry flow.

Installation

Use the registry command to add the component source, and install any package dependencies if needed.

Infinity Registry

Install the component source into your project with the shadcn CLI.

npx shadcn@latest add https://infinityui-pearl.vercel.app/r/leave-rating
Package Dependencies

Install the npm packages used by this component source.

npm install @iconify/react framer-motion

Component Code

Copy and paste this code into your component file.

tsx
"use client";

import { useState } from "react";
import { motion } from "framer-motion";
import { Icon } from "@iconify/react";

interface LeaveRatingProps {
    onRate?: (rating: number) => void;
    maxStars?: number;
}

export function LeaveRating({ onRate, maxStars = 5 }: LeaveRatingProps) {
    const [hovered, setHovered] = useState(0);
    const [rating, setRating] = useState(0);
    const [submitted, setSubmitted] = useState(false);

    const display = hovered || rating;

    const labels: Record<number, string> = {
        1: "Poor", 2: "Fair", 3: "Good", 4: "Great", 5: "Amazing!"
    };

    const handleRate = (star: number) => {
        setRating(star);
        setSubmitted(true);
        onRate?.(star);
    };

    if (submitted) {
        return (
            <motion.div
                initial={{ opacity: 0, scale: 0.8 }}
                animate={{ opacity: 1, scale: 1 }}
                className="flex flex-col items-center gap-3 p-8"
            >
                <div className="w-14 h-14 rounded-full bg-emerald-500/20 border border-emerald-500/40 flex items-center justify-center">
                    <Icon icon="solar:check-circle-bold-duotone" className="text-emerald-400" fontSize={32} />
                </div>
                <p className="text-white font-semibold">Thanks for your rating!</p>
                <div className="flex gap-1">
                    {Array.from({ length: rating }).map((_, i) => (
                        <Icon key={i} icon="solar:star-bold" className="text-amber-400" fontSize={20} />
                    ))}
                </div>
                <button onClick={() => { setSubmitted(false); setRating(0); }} className="text-xs text-zinc-500 hover:text-zinc-300 mt-1">Rate again</button>
            </motion.div>
        );
    }

    return (
        <div className="flex flex-col items-center gap-4 p-6">
            <p className="text-white font-medium text-sm">How would you rate this?</p>
            <div className="flex items-center gap-2">
                {Array.from({ length: maxStars }, (_, i) => i + 1).map((star) => (
                    <motion.button
                        key={star}
                        onMouseEnter={() => setHovered(star)}
                        onMouseLeave={() => setHovered(0)}
                        onClick={() => handleRate(star)}
                        whileHover={{ scale: 1.3 }}
                        whileTap={{ scale: 0.9 }}
                        className="focus:outline-none"
                        aria-label={`Rate ${star} stars`}
                    >
                        <Icon
                            icon={star <= display ? "solar:star-bold" : "solar:star-linear"}
                            className={`transition-colors duration-150 ${star <= display ? "text-amber-400" : "text-zinc-600"}`}
                            fontSize={32}
                        />
                    </motion.button>
                ))}
            </div>
            <motion.p
                key={display}
                initial={{ opacity: 0, y: 4 }}
                animate={{ opacity: 1, y: 0 }}
                className="text-sm text-zinc-500 h-5"
            >
                {display ? labels[display] : ""}
            </motion.p>
        </div>
    );
}