Five-star rating input with hover, touch, and keyboard support

Build a reusable Star Rating component that lets users select a rating between 1 and 5 stars. Use the provided SVG for rendering each star. The component must support mouse, touch, and keyboard interactions for accessibility, and visually indicate the current rating (and hover state). A star rating component in React behaves like a radio group, so the ARIA radio group pattern is a good guide for keyboard support.
role="radiogroup" for the container, role="radio" and aria-checked for each star).
No input props required. All state is managed internally.
// Renders a 5-star rating UI. Users can set their rating (0-5) via mouse, keyboard, or touch.
const StarSVG = ({ filled }) => (
<svg width="32" height="32" viewBox="0 0 20 20" fill={filled ? "#fbbf24" : "none"} stroke="#fbbf24" strokeWidth="1.5">
<polygon points="10,2 12.59,7.95 19,8.66 14,13.14 15.18,19.02 10,16 4.82,19.02 6,13.14 1,8.66 7.41,7.95" />
</svg>
);
The vanilla JavaScript starter provides the same markup as a starSVG(filled) function that returns an HTML string.
- The UI shows five stars and a text "3/5" when the user clicks the third star.
- Hovering on a star highlights all stars up to that star (without selecting).
- Arrow keys move the hover/focus state; Enter/Space selects; Escape clears.
- Clicking the same star twice clears the selection (sets to zero).
- Use role="radiogroup" on the container. - Each star uses role="radio" and aria-checked. - Manage tabIndex for keyboard navigation. - Show current rating visually and as aria-label.
- Users should be able to select a rating, clear it, and visually see their selection.
- The component must be accessible for screen readers and keyboard users.
- Visual feedback must be clear for both selected and hovered stars.
The tests click your stars, press keys, and read the text on the screen, like a real user would. You don’t need test ids or specific class names. They only check that:
role="radio", in order, inside an element with role="radiogroup". Clicking a star selects it.aria-checked="true"; stars after it are not checked.N/5 (for example 0/5, 3/5). Extra spacing doesn’t matter.keydown of Enter on a star selects it, and Escape clears the rating. Handle these keys in a keydown listener on the star (or a parent).