Primitives
Rating
Interactive star rating with hover preview and a springy active star.
Motionhover scale + tap on spring.snappy
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { Rating } from "kineto/rating";Preview
tsx
"use client";
import * as React from "react";
import { Star } from "lucide-react";
import { motion } from "motion/react";
import { cn } from "@/lib/cn";
import { spring } from "@/lib/motion-presets";
const sizeClasses = { sm: "size-4", md: "size-5", lg: "size-7" } as const;
export interface RatingProps
extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
value?: number;
defaultValue?: number;
onValueChange?: (value: number) => void;
max?: number;
readOnly?: boolean;
size?: keyof typeof sizeClasses;
}
const Rating = React.forwardRef<HTMLDivElement, RatingProps>(
(
{
className,
value,
defaultValue = 0,
onValueChange,
max = 5,
readOnly = false,
size = "md",
...props
},
ref
) => {
const [internal, setInternal] = React.useState(defaultValue);
const [hover, setHover] = React.useState<number | null>(null);
const current = value ?? internal;
const display = hover ?? current;
const set = (next: number) => {
if (readOnly) return;
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
return (
<div
ref={ref}
role="radiogroup"
className={cn("inline-flex items-center gap-1", className)}
{...props}
>
{Array.from({ length: max }).map((_, i) => {
const idx = i + 1;
const filled = idx <= display;
return (
<motion.button
key={i}
type="button"
disabled={readOnly}
aria-label={`${idx} star${idx > 1 ? "s" : ""}`}
whileTap={readOnly ? undefined : { scale: 0.85 }}
animate={{ scale: !readOnly && hover === idx ? 1.15 : 1 }}
transition={spring.snappy}
onMouseEnter={() => !readOnly && setHover(idx)}
onMouseLeave={() => !readOnly && setHover(null)}
onFocus={() => !readOnly && setHover(idx)}
onBlur={() => !readOnly && setHover(null)}
onClick={() => set(idx)}
className={cn(
"rounded transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
readOnly ? "cursor-default" : "cursor-pointer",
filled ? "text-warning" : "text-muted-foreground/35"
)}
>
<Star className={cn(sizeClasses[size], filled && "fill-current")} />
</motion.button>
);
})}
</div>
);
}
);
Rating.displayName = "Rating";
export { Rating };src/components/ui/rating.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue | number | — | Controlled or uncontrolled rating. |
| max | number | 5 | Number of stars. |
| readOnly | boolean | — | Display-only mode. |