Star rating
A review rating with half stars: hover previews, a click locks it in with a burst, and a word tells you what the score means.
"use client";
import { useState, type CSSProperties, type KeyboardEvent } from "react";
import "./star-rating.css";
const WORDS: Record<number, string> = {
0.5: "Awful",
1: "Bad",
1.5: "Poor",
2: "Meh",
2.5: "Okay",
3: "Decent",
3.5: "Good",
4: "Great",
4.5: "Excellent",
5: "Perfect",
};
const STAR =
"M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5l-5.9 3.1 1.2-6.5L2.5 9.5l6.6-.9z";
/**
* StarRating
* - One slider (role="slider") for the whole row: arrows change by 0.5, Home/End jump.
* - Pointer position inside a star decides half or full.
* - Each star fills with a clip-path width from a CSS variable.
*/
export function StarRating({ theme = "light" }: { theme?: "light" | "dark" }) {
const [value, setValue] = useState(0);
const [hover, setHover] = useState<number | null>(null);
const [burst, setBurst] = useState(0);
const shown = hover ?? value;
const fromPointer = (e: {
currentTarget: HTMLDivElement;
clientX: number;
}) => {
const r = e.currentTarget.getBoundingClientRect();
const raw = ((e.clientX - r.left) / r.width) * 5;
return Math.min(5, Math.max(0.5, Math.ceil(raw * 2) / 2));
};
const set = (v: number) => {
setValue(v);
setBurst((b) => b + 1);
};
const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
const map: Record<string, number> = {
ArrowRight: 0.5,
ArrowUp: 0.5,
ArrowLeft: -0.5,
ArrowDown: -0.5,
};
if (e.key in map) set(Math.min(5, Math.max(0.5, value + map[e.key])));
else if (e.key === "Home") set(0.5);
else if (e.key === "End") set(5);
else return;
e.preventDefault();
};
return (
<div className="gu-stars" data-theme={theme}>
<p className="gu-stars__q" id="gu-stars-q">
How was your stay?
</p>
<div
className="gu-stars__row"
role="slider"
tabIndex={0}
aria-labelledby="gu-stars-q"
aria-valuemin={0}
aria-valuemax={5}
aria-valuenow={value}
aria-valuetext={
value ? `${value} out of 5, ${WORDS[value]}` : "Not rated"
}
onPointerMove={(e) =>
e.pointerType === "mouse" && setHover(fromPointer(e))
}
onPointerLeave={() => setHover(null)}
onClick={(e) => set(fromPointer(e))}
onKeyDown={onKey}
>
{[0, 1, 2, 3, 4].map((i) => {
const fill = Math.max(0, Math.min(1, shown - i));
return (
<span
key={i}
className="gu-stars__star"
style={{ "--fill": `${fill * 100}%`, "--i": i } as CSSProperties}
data-pop={value > i && burst ? burst : undefined}
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path className="gu-stars__empty" d={STAR} />
<path className="gu-stars__full" d={STAR} />
</svg>
</span>
);
})}
{burst > 0 && (
<span
key={burst}
className="gu-stars__burst"
style={{ left: `${(value / 5) * 100}%` } as CSSProperties}
aria-hidden="true"
/>
)}
</div>
<p className="gu-stars__word" aria-hidden="true">
{shown ? (
<>
<b>{shown.toFixed(1)}</b> {WORDS[shown]}
</>
) : (
"Tap a star"
)}
</p>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Star rating
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A five-star rating input with half stars. Hovering previews the score, clicking commits it with a staggered bounce and a small burst of dots, and a word explains the score ("4.5 Excellent").
## Layout and look
- Question line (21px, weight 900), a row of five 44–56px stars, and a muted line with the score in bold.
- Empty star fill: 16% Ink (dark: 18% Cream). Filled: orange #ff5a1f, clipped by width for half stars.
- Burst: six dots (orange and lime #d7ff3a) around the chosen point, scaling out and fading in 600ms.
## Behavior and motion
1. The row is one role="slider". Pointer X maps to 0.5 steps (ceil to the nearest half).
2. Mouse hover previews; leaving restores the committed value. Touch taps commit directly.
3. On commit, every filled star bounces (scale 1.25, -8° rotation) with a 50ms stagger.
4. Arrow keys ±0.5, Home 0.5, End 5.
## Touch
44px+ stars, touch-action manipulation.
## Accessibility
- aria-valuenow, min 0, max 5, aria-valuetext "4.5 out of 5, Excellent". Focus ring around the row.
- prefers-reduced-motion: no bounce or burst.
## Props
theme. Words in the WORDS map.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### StarRating.tsx
```tsx
"use client";
import { useState, type CSSProperties, type KeyboardEvent } from "react";
import "./star-rating.css";
const WORDS: Record<number, string> = {
0.5: "Awful",
1: "Bad",
1.5: "Poor",
2: "Meh",
2.5: "Okay",
3: "Decent",
3.5: "Good",
4: "Great",
4.5: "Excellent",
5: "Perfect",
};
const STAR =
"M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5l-5.9 3.1 1.2-6.5L2.5 9.5l6.6-.9z";
/**
* StarRating
* - One slider (role="slider") for the whole row: arrows change by 0.5, Home/End jump.
* - Pointer position inside a star decides half or full.
* - Each star fills with a clip-path width from a CSS variable.
*/
export function StarRating({ theme = "light" }: { theme?: "light" | "dark" }) {
const [value, setValue] = useState(0);
const [hover, setHover] = useState<number | null>(null);
const [burst, setBurst] = useState(0);
const shown = hover ?? value;
const fromPointer = (e: {
currentTarget: HTMLDivElement;
clientX: number;
}) => {
const r = e.currentTarget.getBoundingClientRect();
const raw = ((e.clientX - r.left) / r.width) * 5;
return Math.min(5, Math.max(0.5, Math.ceil(raw * 2) / 2));
};
const set = (v: number) => {
setValue(v);
setBurst((b) => b + 1);
};
const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
const map: Record<string, number> = {
ArrowRight: 0.5,
ArrowUp: 0.5,
ArrowLeft: -0.5,
ArrowDown: -0.5,
};
if (e.key in map) set(Math.min(5, Math.max(0.5, value + map[e.key])));
else if (e.key === "Home") set(0.5);
else if (e.key === "End") set(5);
else return;
e.preventDefault();
};
return (
<div className="gu-stars" data-theme={theme}>
<p className="gu-stars__q" id="gu-stars-q">
How was your stay?
</p>
<div
className="gu-stars__row"
role="slider"
tabIndex={0}
aria-labelledby="gu-stars-q"
aria-valuemin={0}
aria-valuemax={5}
aria-valuenow={value}
aria-valuetext={
value ? `${value} out of 5, ${WORDS[value]}` : "Not rated"
}
onPointerMove={(e) =>
e.pointerType === "mouse" && setHover(fromPointer(e))
}
onPointerLeave={() => setHover(null)}
onClick={(e) => set(fromPointer(e))}
onKeyDown={onKey}
>
{[0, 1, 2, 3, 4].map((i) => {
const fill = Math.max(0, Math.min(1, shown - i));
return (
<span
key={i}
className="gu-stars__star"
style={{ "--fill": `${fill * 100}%`, "--i": i } as CSSProperties}
data-pop={value > i && burst ? burst : undefined}
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path className="gu-stars__empty" d={STAR} />
<path className="gu-stars__full" d={STAR} />
</svg>
</span>
);
})}
{burst > 0 && (
<span
key={burst}
className="gu-stars__burst"
style={{ left: `${(value / 5) * 100}%` } as CSSProperties}
aria-hidden="true"
/>
)}
</div>
<p className="gu-stars__word" aria-hidden="true">
{shown ? (
<>
<b>{shown.toFixed(1)}</b> {WORDS[shown]}
</>
) : (
"Tap a star"
)}
</p>
</div>
);
}
```
### star-rating.css
```css
/* StarRating: change these colors to match your brand. */
.gu-stars {
--text: #0e0e10;
--muted: #57534b;
--empty: rgb(14 14 16 / 0.16);
--star: #ff5a1f;
--spark: #d7ff3a;
--focus: #2b35ff;
display: grid;
justify-items: center;
gap: 10px;
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-stars[data-theme="dark"] {
--text: #f3eee3;
--muted: #aea89c;
--empty: rgb(243 238 227 / 0.18);
--focus: #d7ff3a;
}
.gu-stars__q {
margin: 0;
font-size: 1.3rem;
font-weight: 900;
letter-spacing: -0.02em;
}
.gu-stars__row {
position: relative;
display: flex;
border-radius: 12px;
cursor: pointer;
touch-action: manipulation;
}
.gu-stars__row:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 6px;
}
.gu-stars__star {
display: block;
width: clamp(44px, 12vw, 56px);
padding: 3px;
}
.gu-stars__star svg {
display: block;
width: 100%;
overflow: visible;
transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.gu-stars__row:hover .gu-stars__star svg {
transform: scale(0.94);
}
.gu-stars__empty {
fill: var(--empty);
}
.gu-stars__full {
fill: var(--star);
clip-path: inset(0 calc(100% - var(--fill)) 0 0);
transition: clip-path 0.15s;
}
/* Rated stars bounce one after another */
.gu-stars__star[data-pop] svg {
animation: gu-stars-pop 0.45s calc(var(--i) * 50ms)
cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes gu-stars-pop {
40% {
transform: scale(1.25) rotate(-8deg);
}
}
.gu-stars__burst {
position: absolute;
top: 50%;
width: 8px;
height: 8px;
margin: -4px;
border-radius: 50%;
pointer-events: none;
box-shadow:
0 -28px 0 var(--spark),
24px -14px 0 var(--star),
24px 14px 0 var(--spark),
0 28px 0 var(--star),
-24px 14px 0 var(--spark),
-24px -14px 0 var(--star);
animation: gu-stars-burst 0.6s ease-out forwards;
}
@keyframes gu-stars-burst {
from {
transform: scale(0.3);
opacity: 1;
}
to {
transform: scale(1.5);
opacity: 0;
}
}
.gu-stars__word {
min-height: 1.5em;
margin: 0;
color: var(--muted);
font-weight: 600;
}
.gu-stars__word b {
color: var(--text);
font-variant-numeric: tabular-nums;
}
@media (prefers-reduced-motion: reduce) {
.gu-stars__star svg,
.gu-stars__full {
transition: none;
}
.gu-stars__star[data-pop] svg,
.gu-stars__burst {
animation: none;
display: none;
}
.gu-stars__star[data-pop] svg {
display: block;
}
}
```
You might also want
Next piece: Stat cards with sparklinesDate range picker
Components, advanced
Floating label form
Components, beginner
Multi-step form
Components, advanced