GitHub

rating

Star rating component with two modes. Without name: a read-only display that supports half-star values. With name: an interactive radio group that submits the selected value in FormData — no JavaScript required.

Display (read-only)

Omit name to render a read-only display. Pass value as a number — halves are supported.

rating({ value: 5   })   // 5 stars
rating({ value: 3.5 })   // 3½ stars
rating({ value: 2   })   // 2 stars
rating({ value: 0   })   // empty

Sizes

'sm' · 'md' (default) · 'lg'

rating({ value: 4, size: 'sm' })
rating({ value: 4, size: 'md' })
rating({ value: 4, size: 'lg' })

Interactive

Add name to render radio inputs. Hovering a star previews the selection; clicking locks it. The selected value is submitted in FormData.

Your rating
rating({ name: 'score', label: 'Your rating', value: state.score, size: 'lg' })

In a card

Noise-Cancelling Headphones

4.5 · 1,284 reviews

Crystal-clear audio with 40-hour battery life and adaptive noise cancellation.

card({
  title:   'Noise-Cancelling Headphones',
  content:
    '<div style="display:flex;align-items:center;gap:.75rem">' +
      rating({ value: 4.5 }) +
      '<span>4.5 · 1,284 reviews</span>' +
    '</div>' +
    '<p>Crystal-clear audio...</p>',
})

Disabled

rating({ name: 'locked', value: 4, disabled: true })

In forms

The interactive rating submits the selected star count as a number string under name in FormData. Read it with Number(formData.get('score')). If nothing is selected, the field is absent from FormData.
PropTypeDefault
valuenumber0Current rating. Supports 0.5 steps in display mode.
maxnumber5Total number of stars
namestringField name — enables interactive radio mode
labelstringAccessible group label (interactive mode only)
sizesm | md | lgmd1rem / 1.5rem / 2rem
disabledbooleanfalseInteractive mode only
classstring