Skip to the page

Forms and inputs · mid

Star rating

A star rating is a radio group: the value is state, the hover preview is separate state that never overwrites the value, clicking sets the value, arrow keys change it, and every star has an accessible name like '3 of 5 stars'.

The notes

What you must know

  • Semantics: native radio inputs in a fieldset/legend styled as stars, or role='radiogroup' with role='radio' and aria-checked; one tab stop for the group.
  • State: value (committed) and hoverValue (preview); display = hoverValue ?? value; mouse leave clears hoverValue only.
  • Click sets value; optional 'click the same star again to clear'.
  • Keyboard: arrows change the value by one step, Home/End to min/max.
  • Half stars: decide from the pointer position inside the star (offsetX < width / 2 → half), or render 10 half-width radios.
  • API: value/onChange (controlled) or defaultValue, max (default 5), allowHalf, readOnly, size.
  • Visuals: SVG stars with currentColor; a read-only display uses role='img' with aria-label='Rated 3.5 out of 5'.

The red pen

Common traps: mistake → fix

  • Mistake: Store the hover value in the main value

    Fix: moving the mouse changes the rating.

  • Mistake: Images of stars with no accessible name

    Fix: screen readers say nothing useful.

  • Mistake: Five separate buttons with five tab stops

    Fix: use one radio group.

Easy to hard

Questions interviewers ask, easy to hard

  1. Build a 5-star rating with hover preview.
  2. Add half stars.
  3. Make it keyboard and screen-reader friendly.
  4. Make it a reusable controlled component.

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview