star-rating-basic
stackzero/star-rating-basicFreeComponent
stackzero publishes no description for this item.
Install it
npx shadcn@latest add https://ui.stackzero.co/r/star-rating-basic.jsonSource
1"use client";23import { cn } from "@/lib/utils";4import { Star } from "lucide-react";5import * as React from "react";67interface StarRatingBasicProps {8 value: number;9 onChange?: (value: number) => void;10 className?: string;11 iconSize?: number;12 maxStars?: number;13 readOnly?: boolean;14 color?: string;15}1617const StarIcon = React.memo(18 ({19 iconSize,20 index,21 isInteractive,22 onClick,23 onMouseEnter,24 style,25 }: {26 index: number;27 style: React.CSSProperties;28 iconSize: number;29 onClick: () => void;30 onMouseEnter: () => void;31 isInteractive: boolean;32 }) => (33 <Star34 key={index}35 size={iconSize}36 fill={style.fill}37 color={style.color}38 onClick={onClick}39 onMouseEnter={onMouseEnter}40 className={cn(41 "transition-colors duration-200",42 isInteractive && "cursor-pointer hover:scale-110"43 )}44 style={style}45 />46 )47);48StarIcon.displayName = "StarIcon";4950const StarRating_Basic = ({51 className,52 color = "#e4c616",53 iconSize = 24,54 maxStars = 5,55 onChange,56 readOnly = false,57 value,58}: StarRatingBasicProps) => {59 const [hoverRating, setHoverRating] = React.useState<number | null>(null);6061 const handleStarClick = React.useCallback(62 (index: number) => {63 if (readOnly || !onChange) return;64 const newRating = index + 1;65 onChange(newRating);66 },67 [readOnly, onChange]68 );6970 const handleStarHover = React.useCallback(71 (index: number) => {72 if (!readOnly) {73 setHoverRating(index + 1);74 }75 },76 [readOnly]77 );7879 const handleMouseLeave = React.useCallback(() => {80 if (!readOnly) {81 setHoverRating(null);82 }83 }, [readOnly]);8485 const getStarStyle = React.useCallback(86 (index: number) => {87 const ratingToUse =88 !readOnly && hoverRating !== null ? hoverRating : value;89 return {90 color: ratingToUse > index ? color : "gray",91 fill: ratingToUse > index ? color : "transparent",92 } as React.CSSProperties;93 },94 [readOnly, hoverRating, value, color]95 );9697 const stars = React.useMemo(() => {98 return Array.from({ length: maxStars }).map((_, index) => {99 const style = getStarStyle(index);100 return (101 <StarIcon102 key={index}103 index={index}104 style={style}105 iconSize={iconSize}106 onClick={() => handleStarClick(index)}107 onMouseEnter={() => handleStarHover(index)}108 isInteractive={!readOnly}109// … truncated
What it pulls in
npm packages
Files it writes
More from stackzero
All 112 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| face-rating-basicface-rating-basic | stackzero | Components | Free | 1 dep | |
| face-rating-gradientface-rating-gradient | stackzero | Components | Free | 2 deps | |
| image-carousel-basicimage-carousel-basic | stackzero | Components | Free | 5 deps | |
| image-viewer-basicimage-viewer-basic | stackzero | Components | Free | 3 deps | |
| image-viewer-basic-ex-01image-viewer-basic-ex-01 | stackzero | Components | Free | 1 dep | |
| image-viewer-basic-ex-02image-viewer-basic-ex-02 | stackzero | Components | Free | 1 dep | |
| image-viewer-motionimage-viewer-motion | stackzero | Components | Free | 4 deps | |
| image-viewer-motion-ex-01image-viewer-motion-ex-01 | stackzero | Components | Free | 1 dep |
