Saurabh

Star Rating

Component 1

About

A star rating where a new score waves down the row: stars hop and fill in turn, the chosen one pops with sparks, and lowering it drains back.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Star Rating to your project and fits it to your theme.

Code

How Star Rating is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client"

import { createContext, useContext, useEffect, useRef, useState, type RefObject } from "react"
import { StarRating } from "@/components/ui/beui-star-rating"
import { useInView, useReducedMotion } from "motion/react"

// ---- inlined from components/previews/micro-interactions/autoplay.ts ----
/**
 * Lets a parent decide when previews play. The micro interactions gallery
 * provides it, so its cards start still and play only from their play
 * button. Without it a preview plays on its own.
 */
export const AutoplayContext = createContext<{ playing: boolean } | null>(null);

/**
 * Plays a preview: calls `tick` every `interval` ms while the stage is on
 * screen and the tab is visible. Under an AutoplayContext it plays only
 * while `playing` is set; on its own it plays unless someone is hovering or
 * focused inside it. Off entirely with reduced motion. Spread the returned
 * props on the element that wraps the preview.
 */
export function useAutoplay(tick: () => void, interval: number) {
  const ref = useRef<HTMLDivElement>(null);
  const inView = useInView(ref as RefObject<Element>, { amount: 0.5 });
  const reduce = useReducedMotion();
  const control = useContext(AutoplayContext);
  const [paused, setPaused] = useState(false);
  // The latest tick, so the timer never restarts when the callback changes.
  const tickRef = useRef(tick);
  useEffect(() => {
    tickRef.current = tick;
  });

  const running = control ? control.playing : !paused;

  useEffect(() => {
    if (!inView || !running || reduce) return;
    // The first step plays right away, so pressing play answers at once.
    if (control) tickRef.current();
    const id = window.setInterval(() => {
      if (document.visibilityState === "visible") tickRef.current();
    }, interval);
    return () => window.clearInterval(id);
  }, [inView, running, reduce, interval, control]);

  return {
    ref,
    onPointerEnter: () => setPaused(true),
    onPointerLeave: () => setPaused(false),
    onFocus: () => setPaused(true),
    onBlur: () => setPaused(false),
  };
}

// Up and down the row, so both the fill wave and the drain show.
const SEQUENCE = [4, 2, 5, 3, 1];

export function StarRatingPreview() {
  const [rating, setRating] = useState(0);
  const stage = useAutoplay(
    () => setRating((value) => SEQUENCE[(SEQUENCE.indexOf(value) + 1) % SEQUENCE.length]),
    1800,
  );

  return (
    <div {...stage}>
      <StarRating sound value={rating} onValueChange={setRating} size={28} />
    </div>
  );
}

export default StarRatingPreview

Dependencies

More by Saurabh

View all
  • BeUI Tilt Card

    BeUI Tilt Card

    407
  • BeUI Expandable Tabs

    BeUI Expandable Tabs

    395
  • Animated Sidebar

    Animated Sidebar

    351
  • BeUI File Upload

    BeUI File Upload

    321
  • BeUI Bouncy Accordion

    BeUI Bouncy Accordion

    232
  • Loader

    Loader

    231
  • BeUI Expanable Action Bar

    BeUI Expanable Action Bar

    226
  • BeUI Create Menu

    BeUI Create Menu

    212