Skip to content

Inputs

Rating

Bit.BlazorUI

BitRating turns an opinion into a row of stars: hover to preview what a click would give, click to commit it. It is not limited to whole stars - Precision splits every item into as many selectable steps as you ask for, so halves, quarters and tenths are reachable with the pointer and, stepping by that same Precision, with the arrow keys. A bound value of any precision is always drawn exactly, which is what turns the same component into the read-only 4.3-out-of-5 summary that sits next to a review count. The whole row is a single tab stop that follows the WAI-ARIA radiogroup pattern and answers to the arrow, Home, End, Page and digit keys; the items can be cleared back to unrated, stacked into a column, carry their own tooltips, take a different icon at every position, or be replaced wholesale by a template, and the icons come from the built-in set or from any external icon library.

Usage

Every example is live. Open its code to see exactly what produced the component running underneath.

Basic

Left unbound like the first one below, the BitRating keeps its own state with no binding or event handler involved, and DefaultValue only picks where that state starts. Disabling it greys the items out and takes it out of the interaction entirely. ReadOnly is the other half of that: the value is still shown at full contrast, but nothing about it can be changed - which is the mode a rating spends most of its life in, summarizing what other people already rated. The difference runs deeper than the styling: a read-only rating stops being a group of choices and is announced as a single labelled image, while a disabled one is still a group, only an unavailable one. Notice the read-only one below carries a fractional value: partial fills are rendered whether or not the user could ever have picked them.

Basic:

DefaultValue:

Disabled:

ReadOnly:

Max

Max is both the highest value the rating accepts and the number of items it renders, so it is the scale of the whole control. Five is the familiar default, but a coarser three-point scale or a much finer one are just as valid; the items wrap when the row runs out of room, which is what keeps a 100-point rating usable inside a narrow container. A value above the Max is pulled back down to it, so the scale can be tightened at runtime without leaving an impossible value behind. And at the other extreme, a Max of 1 turns the rating into the compact single-star badge that sits next to a numeric score and a review count.

Max is 1 (a compact review summary):

Max is 3:

Max is 6:

Max is 100:

Vertical

Vertical stacks the items into a column instead of a row, and fills it from the bottom up so that "more" is up - the same direction the ArrowUp key already means. Everything else follows: a fractional value fills its item from the bottom edge rather than the leading one, the transparent slices a fine Precision lays over an item are stacked the same way, and the group announces itself as vertically oriented so a screen reader describes the arrangement the user is actually facing. It is the shape to reach for when the rating sits beside a vertical bar chart or in a narrow column where a row of five stars would not fit.

Vertical:

Vertical with a half precision:
3.5 of 5

Precision

Precision is the smallest change the user can make, expressed as a fraction of a single item. The default of 1 only allows whole items; 0.5 adds halves, 0.25 quarters, and 0.1 makes every tenth selectable. Below a whole item each item is covered by that many transparent slices, so the half a pointer lands on is the half that gets committed - and the arrow keys step by exactly the same amount, which keeps the fractions reachable without a pointer. A precision that does not divide an item evenly is rounded to the closest number of equal steps, so the items always end on a whole value, and an item is never split into more than a hundred steps. Remember it constrains only what the user can pick: a value bound from elsewhere is always drawn at its exact precision.

Half (Precision="0.5"):
2.5 of 5

Quarter (Precision="0.25"):
3.25 of 5

Exact (Precision="0.1"):
3.7 of 5

Clearing

A rating starts at the first item unless it is told that "not rated" is a legitimate answer. AllowZeroStars says exactly that: a value of 0 is kept instead of being pulled up to 1, so the rating can start empty - though the user still cannot get back to it. AllowClear is what adds that way back: clicking the item that is already selected, or pressing Delete or Backspace, returns the rating to 0. Since an unrated rating is the whole point of clearing, AllowClear opens up the 0 on its own, without AllowZeroStars.

Default (0 is pulled up to 1):

AllowZeroStars (starts empty, but cannot be emptied again):
0 of 5

AllowClear (click the selected item, or press Delete, to clear):

HighlightSelectedOnly

By default a rating is cumulative: picking the fourth item fills the first four, because four stars means "four out of five". HighlightSelectedOnly switches that to a scale of standalone choices where only the item matching the value is filled - the right reading when the items are faces, moods or grades rather than a quantity that accumulates. A fractional value still fills its own item by the fraction it covers, so the mode composes with Precision instead of rounding against it.

Cumulative (default):

HighlightSelectedOnly:

Hover preview

While the pointer is over the items the rating previews the value a click would commit, painting it in the hover shade so it reads as a proposal rather than as the committed value; leaving the rating restores what is actually bound. OnHoverChange reports that previewed value as it changes, and null once the pointer leaves - which is how the familiar word-per-star caption below is built. At a fractional Precision the preview follows the slices too, so the caption can react to a half step. NoHoverPreview turns the whole behaviour off for the rare case where the preview competes with something else on the page.

Hover over the stars:

NoHoverPreview:

Icons

SelectedIconName and UnselectedIconName replace the pair of built-in star glyphs, which is what lets the same component read as a like, a heart or a checklist. The two icons are drawn on top of each other and the selected one is clipped to the fill percentage, so the pair works best when both glyphs share an outline - a hollow shape and its filled twin. For icons from an external library, see the External Icons section below.

Heart:

Checkbox:

Like:

Per-item icons

GetSelectedIcon and GetUnselectedIcon answer with the icon of a single position instead of one pair for the whole scale, which is what turns a rating into a scale that changes shape as it fills - thumbs down at the low end and thumbs up at the high end, or a different face at every step. Each callback receives the one-based position of the item and may return null to fall back to SelectedIcon / SelectedIconName, so only the positions that need their own glyph have to be answered. This is the lightweight half of ItemTemplate: the component keeps drawing the item, so the two-layer partial fill of a fractional value goes on working.

Thumbs (down for the first two, up for the rest):

A different face at every step (with HighlightSelectedOnly):

ItemTitles

ItemTitles gives each item its own native tooltip, in order, so hovering an item spells out what it means instead of leaving the user to count stars. The same words double as the accessible name of the item, which is a better answer for a screen reader than "4 of 5" - unless AriaLabelFormat overrides it. A list shorter than the rating simply leaves the remaining items without a tooltip, and they fall back to their position in the scale.

Hover over each star:

ItemTemplate

ItemTemplate replaces the pair of icons of every item with content of your own, while the BitRating keeps everything around it: the hit areas, the hover preview, the keyboard handling and the accessibility. The template receives a BitRatingItemContext describing the item - its Index, the Percentage it is filled by, whether it IsFull or merely IsSelected, and both the previewed DisplayValue and the committed Value - which is enough to drive anything from a numbered scale to the mood faces below. Note that a template renders whatever you give it as a whole, so a partial fill is yours to express if it matters; when all you need is a different glyph per position, GetSelectedIcon keeps the built-in partial fill instead.

Numbered scale:

Mood faces (with HighlightSelectedOnly):

Binding

A one-way Value makes the rating a pure display of whatever the page decides - the toggle below drives it and clicking the stars does nothing, because there is nowhere for the new value to go. @bind-Value completes the circuit in both directions, which is why the number field and the stars below stay in step whichever one is used. Notice the number field can push a fractional value into a rating whose Precision is 1: the constraint is on what the user can pick, never on what can be displayed.

One-way:
0 of 5

Two-way:

Events

OnChange fires after a new value lands, which is what an uncontrolled rating reports its result through. OnChanging runs first and can stop the change altogether: setting Cancel on the BitRatingChangeArgs keeps the current value, and since the callback is awaited it can take its time - long enough to ask the user a question. The args carry both the incoming Value and the OldValue being left behind, so the decision can depend on the direction of the change; the second rating below refuses to be lowered.

OnChange:

OnChanging (this one refuses to go down):

Validation

Inside an EditForm the BitRating is a form field like any other: it participates in the EditContext, reports its changes to it, and turns red while the value is invalid. Combined with AllowZeroStars, a Range annotation that starts at 1 is what turns "pick a rating" into a required question - the form starts unrated, and stays unsubmittable until something is picked. Required announces that expectation to assistive technologies ahead of the failure, rather than leaving the error message to be the first mention of it. The value itself travels in a hidden number input, so Name also makes the rating readable by a plain form post.

0 of 5

Accessibility

An interactive BitRating is a WAI-ARIA radiogroup of radio items and behaves like one: the whole row is a single tab stop that lands on the selected item, and from there the keyboard covers the entire scale. ArrowRight / ArrowUp raise the value by one Precision step and ArrowLeft / ArrowDown lower it; holding Shift - or using PageUp / PageDown, which need no modifier - moves a whole item at a time, so a rating split into tenths is still five presses wide rather than fifty. Home and End jump to the ends, the digit keys jump straight to that rating, and Delete or Backspace clear it where AllowClear permits. The horizontal arrows follow the reading direction, so they swap in RTL, and Escape is deliberately left alone so a rating inside a modal does not clear itself on the way to dismissing it.

Since a group of stars carries no visible label, name it: AriaLabel names the rating as a whole and AriaLabelFormat names each item, with {0} as the item and {1} as the max - "Select 3 of 5 stars". Without it an item is named by its tooltip from ItemTitles, and failing that by its position, so no item is ever left nameless. GetAriaLabel is a callback given the current value and the max, used as the name whenever AriaLabel is not set; a read-only rating, which is announced as a single image rather than a group of choices, falls back to a plain "3.5 of 5" if neither is provided. ValueTextFormat is what rewords that fallback - and the live announcement an interactive rating makes for a fractional value, which no single radio can express - for another language or for a scale whose items are not stars. AutoFocus hands the rating the focus as the page renders.

Tab to it, then use the arrow, Page and digit keys:

GetAriaLabel (inspect the aria-label of the read-only rating):

Visibility

Visibility decides how the rating disappears: Hidden keeps its box and only stops painting it, so the layout around it does not move, while Collapsed removes the box entirely and lets the rest close the gap. The brackets below mark where each one sits.

Visible: [
]
Hidden: [ ]
Collapsed: [ ]

Score-based styling

The parameters of the rating are re-read on every render, so they can be computed from the very value the rating is bound to - which is all it takes to restyle the scale by the score it shows. The first rating below derives its Color from its own value, turning from red through orange to green as the score climbs the bands; the second derives its icons the same way, so a low score frowns and a high one smiles. This is the styling to reach for when "2 of 5" and "5 of 5" are different kinds of news rather than different amounts of the same one - and it needs no dedicated API, just callbacks and parameters that close over the bound value.

The color follows the score:

The icon follows it too:

Color

Color picks the theme color the filled part of the items is painted in, with Primary as the default. The unfilled part deliberately stays neutral whatever the color is, so it keeps reading as "not rated yet" instead of as a second, dimmer accent. The semantic colors - Success, Warning, Error and friends - tie the rating to what the score means, while the Background, Foreground and Border families keep it legible on non-default surfaces like the inverted panel below. And since Color is an ordinary parameter, it can be computed from the bound value itself - which is what the Score-based styling section above builds on.

3.5 of 5
Primary
3.5 of 5
Secondary
3.5 of 5
Tertiary
3.5 of 5
Info
3.5 of 5
Success
3.5 of 5
Warning
3.5 of 5
SevereWarning
3.5 of 5
Error

3.5 of 5
PrimaryBackground
3.5 of 5
SecondaryBackground
3.5 of 5
TertiaryBackground

3.5 of 5
PrimaryForeground
3.5 of 5
SecondaryForeground
3.5 of 5
TertiaryForeground
3.5 of 5
PrimaryBorder
3.5 of 5
SecondaryBorder
3.5 of 5
TertiaryBorder

External Icons

SelectedIcon and UnselectedIcon take a BitIconInfo instead of a built-in icon name, which is how icons from an external library are used. BitIconInfo.Fa and BitIconInfo.Bi build the class names for FontAwesome and Bootstrap Icons, BitIconInfo.Css takes whatever classes you hand it, and a plain string works too. They take precedence over SelectedIconName / UnselectedIconName when both are set, and the same type is what the per-item GetSelectedIcon callback returns. Remember to reference the stylesheet of the icon library itself.


FontAwesome (stars):
3.5 of 5

FontAwesome (hearts):
3.5 of 5

Bootstrap (stars):
3.5 of 5

Bootstrap (hearts):
3.5 of 5

Size

Size scales the items, from the Small that fits inline next to a line of text to the Large that carries a rating asked as the main question on a page. Medium is the default. A larger size also means larger hit areas, which matters most on touch and at fine precisions.

Small:

Medium:

Large:

Style & Class

Style and Class reach the root element of the rating, which is enough for the framing around it. Styles and Classes go a level deeper and address the parts by name - Root, Button, IconContainer, SelectedIcon and UnselectedIcon - so the filled and unfilled halves can be styled apart from each other without leaving the component.

Component's Style & Class:




Styles & Classes:

3.5 of 5

3.5 of 5

RTL

Setting Dir to Rtl mirrors the whole rating: the first item moves to the right, the partial fill of a fractional value grows from the right edge of its item, the transparent slices of a fine Precision are laid out from that same edge, and the horizontal arrow keys swap so that ArrowLeft still means "more". The fractional read-only rating below shows the fill anchored on the correct side.


3.5 of 5

API

Every parameter, public member, sub-class and enum this component exposes.

BitRating parameters

Name Type Default value Description
AllowClear bool false Lets the current value be cleared, by clicking the item that is already selected or by pressing Delete or Backspace. Clearing sets the value to 0, so it also makes 0 a reachable value the same way AllowZeroStars does.
AllowZeroStars bool false Allow the initial rating value be 0. Note that a value of 0 still won't be selectable by mouse or keyboard unless AllowClear is also set.
AriaLabelFormat string? null Optional label format for each individual rating star (not the rating control as a whole) that will be read by screen readers. Placeholder {0} is the current rating and placeholder {1} is the max. Without it an item is named by its ItemTitles tooltip, and failing that by its position in the scale.
AutoFocus bool false If true, the rating automatically receives focus when the page renders.
Classes BitRatingClassStyles? null Custom CSS classes for different parts of the BitRating.
Color BitColor? null The general color of the rating, applied to the filled part of the items. The unfilled part stays neutral so it reads as "not rated yet" whichever color is picked.
GetAriaLabel Func<double, double, string>? null Optional callback to set the aria-label for rating control in readOnly mode. Also used as a fallback aria-label if the AriaLabel parameter is not provided. The first argument is the current value and the second one is the max.
GetSelectedIcon Func<int, BitIconInfo?>? null Chooses the selected (filled) icon of each rating item separately, from the one-based position of the item. Returning null falls back to SelectedIcon / SelectedIconName.
GetUnselectedIcon Func<int, BitIconInfo?>? null Chooses the unselected (empty) icon of each rating item separately, from the one-based position of the item. Returning null falls back to UnselectedIcon / UnselectedIconName.
HighlightSelectedOnly bool false Highlights only the item matching the current value instead of every item up to it, turning the rating into a scale of standalone choices rather than a cumulative one. A fractional value still fills its own item by the fraction it covers.
ItemTemplate RenderFragment<BitRatingItemContext>? null Replaces the default pair of icons of every rating item with custom content.
ItemTitles IList<string>? null The native tooltips of the rating items, in order, shown when hovering over each one, and used as the accessible name of the item unless AriaLabelFormat overrides it. Items beyond the end of the list simply get no tooltip.
Max int 5 Maximum rating, which is also the number of rendered items. Values below 1 are treated as 1.
NoHoverPreview bool false Turns off the preview that follows the pointer over the items and shows the value that a click would commit.
OnChanging EventCallback<BitRatingChangeArgs> Callback invoked before the value of the rating changes, letting the change be cancelled by setting Cancel on the provided args.
OnHoverChange EventCallback<double?> Callback for when the previewed value changes, which is the value a click would commit. It receives null when the pointer leaves the rating and the preview ends.
Precision double 1 The smallest change of the value the user can make, as a fraction of a single item. The default of 1 only allows whole items, 0.5 adds halves, 0.1 makes every tenth selectable. It constrains what the user can pick, not what can be displayed.
SelectedIcon BitIconInfo? null Icon for selected rating elements using external icon libraries (e.g. FontAwesome, Bootstrap Icons). Takes precedence over SelectedIconName when both are set.
SelectedIconName string? FavoriteStarFill Custom icon name for selected rating elements (Fluent UI). For external icon libraries, use SelectedIcon instead.
Size BitSize? null Size of rating elements.
Styles BitRatingClassStyles? null Custom CSS styles for different parts of the BitRating.
UnselectedIcon BitIconInfo? null Icon for unselected rating elements using external icon libraries (e.g. FontAwesome, Bootstrap Icons). Takes precedence over UnselectedIconName when both are set.
UnselectedIconName string? FavoriteStar Custom icon name for unselected rating elements (Fluent UI). For external icon libraries, use UnselectedIcon instead.
ValueTextFormat string? null The format of the spoken form of the current value, where placeholder {0} is the value and placeholder {1} is the max. It is what the live region of an interactive rating announces for a value no radio can carry, and what a read-only rating falls back to when it is given no other label. The default is "{0} of {1}".
Vertical bool false Stacks the rating items in a column instead of a row, filling from the bottom up so that "more" is up, the way the ArrowUp key means more.

BitInputBase parameters

Name Type Default value Description
DefaultValue TValue? null The default value of the input to be used in uncontrolled mode (i.e. when the Value is not bound), typically used alongside the OnChange callback.
DisplayName string? null Gets or sets the display name for this field.
InputHtmlAttributes IReadOnlyDictionary<string, object>? null Gets or sets a collection of additional attributes that will be applied to the created element.
Name string? null Gets or sets the name of the element. Allows access by name from the associated form.
NoValidate bool false Disables the validation of the input.
OnChange EventCallback<TValue?> Callback for when the input value changes.
ReadOnly bool false Makes the input read-only.
Required bool false Makes the input required.
Value TValue? null Gets or sets the value of the input. This should be used with two-way binding.

BitInputBase public members

Name Type Default value Description
InputElement ElementReference The ElementReference of the input element.
FocusAsync() () => ValueTask Gives focus to the input element.
FocusAsync(bool preventScroll) (bool preventScroll) => ValueTask Gives focus to the input element.

BitComponentBase parameters

Name Type Default value Description
AriaLabel string? null Gets or sets the accessible label for the component, used by assistive technologies.
Class string? null Gets or sets the CSS class name(s) to apply to the rendered element.
Dir BitDir? null Gets or sets the text directionality for the component's content.
ForceAnimation bool false Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested.
HtmlAttributes Dictionary<string, object> new Dictionary<string, object>() Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters.
Id string? null Gets or sets the unique identifier for the component's root element.
IsEnabled bool true Gets or sets a value indicating whether the component is enabled and can respond to user interaction.
Style string? null Gets or sets the CSS style string to apply to the rendered element.
TabIndex string? null Gets or sets the tab order index for the component when navigating with the keyboard.
Visibility BitVisibility BitVisibility.Visible Gets or sets the visibility state (visible, hidden, or collapsed) of the component.

BitComponentBase public members

Name Type Default value Description
UniqueId Guid Guid.NewGuid() Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed.
RootElement ElementReference Gets the reference to the root HTML element associated with this component.

BitRatingClassStyles properties

The CSS classes and styles of the individual parts of the BitRating.

Name Type Default value Description
Root string? null Custom CSS classes/styles for the root element of the rating.
Button string? null Custom CSS classes/styles for the rating's button.
IconContainer string? null Custom CSS classes/styles for the rating icon container.
SelectedIcon string? null Custom CSS classes/styles for the rating selected icon.
UnselectedIcon string? null Custom CSS classes/styles for the rating unselected icon.

BitRatingChangeArgs properties

The arguments of the OnChanging callback, which runs before the value of the rating changes.

Name Type Default value Description
Value double The rating value the component is about to move to.
OldValue double The rating value the component is moving away from.
Cancel bool false Set to true to cancel the change and keep the current value of the rating.

BitRatingItemContext properties

The context passed to the ItemTemplate, describing the rating item being rendered.

Name Type Default value Description
Index int The one-based position of the item in the rating.
Max int The number of items the rating renders.
Percentage double How much of the item is filled, from 0 to 100. A partially filled item is the fractional part of the value.
DisplayValue double The value the item is rendered from, which is the hovered value while a hover preview is active, and the committed value otherwise.
Value double The committed value of the rating, regardless of any hover preview.
IsSelected bool Whether the item is filled at all, meaning its Percentage is greater than zero.
IsFull bool Whether the item is completely filled, meaning its Percentage is 100.

BitIconInfo properties

Represents icon information for rendering icons. Supports built-in Fluent UI icons and external icon libraries (FontAwesome, Bootstrap Icons, etc.). Use BitIconInfo.Css("fa-solid fa-star"), BitIconInfo.Fa("solid star"), or BitIconInfo.Bi("star-fill") for external icons.

Name Type Default value Description
Name string? null Gets or sets the name of the icon.
BaseClass string? null Gets or sets the base CSS class for the icon. For external icon libraries like FontAwesome, you might set this to "fa" or leave empty.
Prefix string? null Gets or sets the CSS class prefix used before the icon name. For external icon libraries, you might set this to "fa-" or leave empty.

BitSize enum

Name Value Description
Small 0 Display rating icon using small size.
Medium 1 Display rating icon using medium size.
Large 2 Display rating icon using large size.

BitColor enum

Name Value Description
Primary 0 Primary general color.
Secondary 1 Secondary general color.
Tertiary 2 Tertiary general color.
Info 3 Info general color.
Success 4 Success general color.
Warning 5 Warning general color.
SevereWarning 6 SevereWarning general color.
Error 7 Error general color.
PrimaryBackground 8 Primary background color.
SecondaryBackground 9 Secondary background color.
TertiaryBackground 10 Tertiary background color.
PrimaryForeground 11 Primary foreground color.
SecondaryForeground 12 Secondary foreground color.
TertiaryForeground 13 Tertiary foreground color.
PrimaryBorder 14 Primary border color.
SecondaryBorder 15 Secondary border color.
TertiaryBorder 16 Tertiary border color.

BitVisibility enum

Name Value Description
Visible 0 The content of the component is visible.
Hidden 1 The content of the component is hidden, but the space it takes on the page remains (visibility:hidden).
Collapsed 2 The component is hidden (display:none).

BitDir enum

Name Value Description
Ltr 0 Ltr (left to right) is to be used for languages that are written from the left to the right (like English).
Rtl 1 Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic).
Auto 2 Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.