Rating
A star rating built on the WAI-ARIA radio pattern: a radiogroup of radio stars with roving focus, optional half values and pointer hover preview.
Value:
<NaviusRating @bind-Value="_rating" AllowHalf class="inline-flex gap-1">
@for (var i = 0; i < 5; i++)
{
<NaviusRatingItem class="group relative h-7 w-7">
<StarOutline class="text-muted-foreground/25" />
<span class="absolute inset-0 w-0 overflow-hidden
group-data-[state=full]:w-full group-data-[state=half]:w-1/2">
<StarFilled class="text-amber-400" />
</span>
</NaviusRatingItem>
}
</NaviusRating>
@code { private decimal? _rating = 3.5m; }Features
- A
role="radiogroup"ofrole="radio"stars with roving tabindex; the value logic lives on the root, so arrows move focus and value together. -
AllowHalfadds pointer half-zones and 0.5 keyboard steps; each star paints fromdata-state(full/half/empty), computed from the hover preview or the committed value. -
AllowClearlets re-selecting the current value (or arrowing below 1) clear to unrated;Labelsupplies each star's accessible name. - Controlled via
@bind-Value(adecimal?), uncontrolled viaDefaultValue; a hidden input mirrors the value into form submission whenNameis set.
Installation
Install the brain, or copy just this primitive in with the CLI.
navius add ratingAnatomy
Provide your own star items, or let the root auto-render Max of them when you give it no children.
@using Navius.Primitives.Components.Rating
@* Auto-rendered stars: *@
<NaviusRating @bind-Value="_rating" Max="5" />
@* Or your own item children: *@
<NaviusRating @bind-Value="_rating">
<NaviusRatingItem>@* star glyph *@</NaviusRatingItem>
</NaviusRating>API Reference
Root
NaviusRating. Owns the value, the keyboard model and the hover preview. Renders a div with role=radiogroup. Value is two-way bindable.
| Prop | Type | Default |
|---|---|---|
| Value | decimal? | - |
| ValueChanged | EventCallback<decimal?> | - |
| DefaultValue | decimal? | - |
| Max | int | 5 |
| AllowHalf | bool | false |
| AllowClear | bool | true |
| ReadOnly | bool | false |
| Disabled | bool | false |
| Required | bool | false |
| Invalid | bool | false |
| Name | string? | - |
| Label | Func<decimal, string>? | - |
| Dir | string? | - |
| ChildContent | RenderFragment? | - |
| …attributes | IDictionary<string, object>? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-navius-rating] | Present on the group |
| [data-disabled] | Present when disabled |
| [data-readonly] | Present when read-only |
Item
NaviusRatingItem. One star; its index is assigned by registration order. Renders a button with role=radio (plus two aria-hidden half-zones under AllowHalf).
| Prop | Type | Default |
|---|---|---|
| ChildContent | RenderFragment? | - |
| …attributes | IDictionary<string, object>? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-navius-rating-item] | Present on each star |
| [data-state] | "full" | "half" | "empty" (from hover preview or value) |
| [data-index] | The 1-based star index |
| [data-value] | The whole value this star selects |
| [data-checked] / [data-unchecked] | Whether this star holds the value |
| [data-highlighted] | Present while covered by a hover preview |
| [data-readonly] / [data-disabled] | Mirrors the group state |
Accessibility
Adheres to the radio group pattern with roving tabindex: only the checked star (or the first when unrated) is in the Tab order. The star holding a fractional value announces the real value (a 3.5 reads "3.5 stars"). The group is labelled "Rating" unless you supply an aria-label / aria-labelledby.
Keyboard interactions
| Key | Description |
|---|---|
| Arrow Up / Arrow Right | Raises the rating by one step (0.5 with AllowHalf; mirrored under rtl). |
| Arrow Down / Arrow Left | Lowers the rating by one step, clearing below 1 when AllowClear. |
| Home / End | Sets the rating to 1 / Max. |
| 1-9 | Jumps directly to that whole-star rating (capped at Max). |
| Backspace / Delete | Clears to unrated when AllowClear. |
| Space / Enter | Activates the focused star. |