rating

TosiRating / <tosi-rating> provides a drop-in replacement for an <input> that renders a rating using icons.

const { tosiRating } = tosijsui
preview.append(
  tosiRating({ value: 3.4 }),
  tosiRating({ min: 0, value: 3.4, step: 0.5, hollow: true }),
  tosiRating({ value: 3.4, ratingFill: 'deepskyblue', ratingStroke: 'deepskyblue' }),
  tosiRating({ value: 3.1, max: 10, ratingFill: 'hotpink', ratingStroke: 'hotpink', icon: 'heart', iconSize: 32 }),
  tosiRating({ class: 'color', value: 3.1, max: 5, icon: 'tosiPlatform', iconSize: 32 }),
)
const rating = preview.querySelector('tosi-rating')
test('rating renders', () => {
  expect(rating).toBeTruthy()
  expect(rating.tagName.toLowerCase()).toBe('tosi-rating')
})
test('rating has correct value', () => {
  expect(rating.value).toBe(3.4)
})
test('rating has correct max', () => {
  expect(rating.max).toBe(5)
})
.preview {
  display: flex;
  flex-direction: column;
}

.preview .color::part(empty) {
  filter: grayscale(1);
  opacity: 0.25;
}

Attributes

Form Integration

<tosi-rating> is form-associated, meaning it works directly in native <form> elements:

<form>
  <tosi-rating name="rating" required></tosi-rating>
  <button type="submit">Submit</button>
</form>

Keyboard

<tosi-rating> should be fully keyboard navigable (and, I hope, accessible).

The up key increases the rating, down descreases it. This is the same as the behavior of <input type="number">, Shoelace's rating widget, and (in my opinion) common sense, but not like MUI's rating widget.