# Rating

SvRating: an interactive review widget with a live label, plus a read-only half-star aggregate with a distribution breakdown.

Uses `@svgrid/grid` (MIT). Category: Buttons & Toggles. Live demo: https://svgrid.com/demos/311-rating/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvRating in Svelte 5 as a review widget: an interactive star input with a live text label, plus a read-only aggregate that shows half stars and a distribution breakdown per star count. The value and onChange keep it controlled, size sets the star scale, allowHalf enables half-star steps and readOnly turns it into a display.

SvRating - a production review widget: an interactive star input with a live summary, a read-only aggregate display, and half-star precision. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How do I show an average with half stars?

Pass a fractional value such as 4.3 with allowHalf and readOnly; the stars render partial fills at half-star precision without accepting input.

### How is the live label produced?

A small array maps each star count to a word; a $derived picks the entry for the current value and prints it next to the stars.

### Is the rating keyboard accessible?

Yes. Each star is a button with its own aria-label, the group carries a label, arrow keys and Tab move between stars and hover previews the value before a click commits it.

## Related documentation

- [SvRating](https://svgrid.com/docs/help/ui-components/sv-rating.md)

## Related articles

- [Migrating a Svelte 4 Table Component to Svelte 5](https://svgrid.com/blog/migrating-svelte-4-table-to-svelte-5/) - A concept-by-concept guide to porting a hand-rolled Svelte 4 data table to Svelte 5 runes - props, reactivity, stores, slots, events, and when to stop hand-rolling entirely.
- [Migrating from DataTables.net to a Svelte Data Grid](https://svgrid.com/blog/migrating-from-datatables-to-svelte/) - A practical migration guide from jQuery DataTables to SvGrid - how column definitions, server-side data, custom rendering, and selection map across, with working code for each step.
- [Migrating from ag-grid-react to a Svelte Stack](https://svgrid.com/blog/migrating-from-ag-grid-react-to-svelte/) - A practical field guide for porting ag-grid-react screens to SvGrid - column defs, cell renderers, server-side data, and the React-to-Svelte reactivity shift.

## Source code (311-rating.svelte)

```svelte
<script lang="ts">
  /**
   * SvRating - a production review widget: an interactive star input with a live
   * summary, a read-only aggregate display, and half-star precision. Copy-paste
   * ready.
   */
  import { SvRating } from '@svgrid/grid'

  let myRating = $state(0)
  const labels = ['', 'Terrible', 'Poor', 'Average', 'Good', 'Excellent']
  const breakdown = [
    { stars: 5, pct: 68 },
    { stars: 4, pct: 20 },
    { stars: 3, pct: 7 },
    { stars: 2, pct: 3 },
    { stars: 1, pct: 2 },
  ]
</script>

<div class="wrap">
  <header>
    <h2>Rating</h2>
    <p>A star input (WAI-ARIA slider) with hover preview, keyboard and half-star precision - reviews, feedback, quality scores.</p>
  </header>

  <section class="block">
    <h3>Leave a review</h3>
    <div class="rate">
      <SvRating value={myRating} onChange={(v) => (myRating = v)} size="lg" />
      <span class="lbl">{myRating ? labels[Math.ceil(myRating)] : 'Tap a star'}</span>
    </div>
  </section>

  <section class="block">
    <h3>Aggregate (4.5 average, read-only)</h3>
    <div class="agg">
      <div class="score"><span class="big">4.5</span><SvRating value={4.5} allowHalf readonly /></div>
      <div class="bars">
        {#each breakdown as b (b.stars)}
          <div class="bar"><span class="s">{b.stars}★</span><div class="track"><span style:width={`${b.pct}%`}></span></div><span class="p">{b.pct}%</span></div>
        {/each}
      </div>
    </div>
  </section>
</div>

<style>
  .wrap { padding: 20px; max-width: 560px; display: flex; flex-direction: column; gap: 22px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  .block h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--sg-muted, #64748b); }
  .rate { display: flex; align-items: center; gap: 12px; }
  .lbl { font-size: 14px; font-weight: 600; color: var(--sg-muted, #64748b); }
  .agg { display: flex; gap: 24px; align-items: center; flex-wrap: wrap; }
  .score { display: flex; flex-direction: column; align-items: center; gap: 4px; }
  .score .big { font-size: 34px; font-weight: 800; line-height: 1; }
  .bars { flex: 1; min-width: 200px; display: flex; flex-direction: column; gap: 4px; }
  .bar { display: flex; align-items: center; gap: 8px; font-size: 12px; }
  .bar .s { width: 26px; color: var(--sg-muted, #64748b); }
  .bar .p { width: 34px; text-align: right; color: var(--sg-muted, #94a3b8); }
  .track { flex: 1; height: 7px; border-radius: 999px; background: var(--sg-border, #e2e8f0); overflow: hidden; }
  .track span { display: block; height: 100%; background: var(--sg-rating-on, #f59e0b); }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/311-rating.svelte)
