import type { HTMLAttributes, SVGProps } from "react"; import { useId } from "react"; import { cx } from "@/utils/cx"; export const getStarProgress = (starPosition: number, rating: number, maxRating: number = 5) => { // Ensure rating is between 0 and 5 const clampedRating = Math.min(Math.max(rating, 0), maxRating); const diff = clampedRating - starPosition; if (diff >= 1) return 100; if (diff <= 0) return 0; return Math.round(diff * 100); }; interface StarIconProps extends SVGProps { /** * The progress of the star icon. It should be a number between 0 and 100. * * @default 100 */ progress?: number; } export const StarIcon = ({ progress = 100, ...props }: StarIconProps) => { const id = useId(); return ( ); }; interface RatingStarsProps extends HTMLAttributes { /** * The rating to display. * * @default 5 */ rating?: number; /** * The number of stars to display. */ stars?: number; /** * The class name of the star icon. */ starClassName?: string; } export const RatingStars = ({ rating = 5, stars = 5, starClassName, ...props }: RatingStarsProps) => { return (
{Array.from({ length: stars }).map((_, index) => ( ))}
); };