Rating
Ratings display a numeric score as a row of selectable symbols, typically stars. Use them to capture quick feedback or show an average rating for a product or piece of content.
This component works with standard <form> elements. See form controls for form submission and client-side validation.
Examples
Label
Ratings are usually identified by context, so the label isn't displayed. Always provide one with the label attribute so assistive devices can announce the control.
Disabled
Use the disabled attribute to disable the rating.
Readonly
Use the readonly attribute to display a rating that users can't change. Unlike disabled, a readonly rating still submits its value with the form.
Size
Use the size attribute to change the rating's size.
For finer control, set the font-size property directly.
Max Value
Ratings go from 0 to 5 by default. Use the max attribute to change the highest possible value.
Precision
Use the precision attribute to let users select fractional ratings, such as half stars.
Custom Icons
Pass a function to the getSymbol property to render a custom symbol in place of the default star.
Value-Based Icons
The getSymbol function receives the symbol's value and whether it's currently selected, so you can render different icons across the scale.
Detecting Hover
Use the wa-hover event to react as the user hovers over (or touches and drags across) the rating, before they commit to a value.
The event's detail carries phase and value. The phase property reports when hovering starts, moves to a new value, and ends. The value property is what the rating would become if the user committed to the hovered symbol.
Required
Use the required attribute to make the rating mandatory. The form won't submit until the user selects a value.
Custom Validity
Use the setCustomValidity() method to set a custom validation message. This prevents the form from submitting and makes the browser display your message. Pass an empty string to clear the error.
Form Submission
Ratings work in forms just like native form controls. The rating's name and value are included in the form data on submit.
API
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.
Import this component directly from the CDN:
import 'https://ka-f.webawesome.com/webawesome@3.10.0/components/rating/rating.js';
After installing Web Awesome via npm, import this component:
import '@awesome.me/webawesome/dist/components/rating/rating.js';
If you're self-hosting Web Awesome, import this component from your server:
import './webawesome/dist/components/rating/rating.js';
To import this component for React 18 or below, use the following code:
import WaRating from '@awesome.me/webawesome/dist/react/rating/index.js';
Attributes & Properties
Learn more about attributes and properties.
| Name | Description | Reflects |
|---|---|---|
defaultValuedefault-value |
The default value of the form control. Used to reset the rating to its initial value.
Type
number
Default
0
|
|
disableddisabled |
Disables the rating.
Type
boolean
Default
false
|
|
form |
By default, form controls are associated with the nearest containing
<form> element. This attribute allows you
to place the form control outside of a form and associate it with the form that has this id. The form must be in
the same document or shadow root for this to work.
Type
HTMLFormElement | null
|
|
getSymbolgetSymbol |
A function that customizes the symbol to be rendered. The first and only argument is the rating's current value.
The function should return a string containing trusted HTML of the symbol to render at the specified value. Works
well with
<wa-icon> elements.
Type
(value: number, isSelected: boolean) => string
|
|
labellabel |
A label that describes the rating to assistive devices.
Type
string
Default
''
|
|
maxmax |
The highest rating to show.
Type
number
Default
5
|
|
namename |
The name of the rating, submitted as a name/value pair with form data.
Type
string | null
Default
null
|
|
precisionprecision |
The precision at which the rating will increase and decrease. For example, to allow half-star ratings, set this
attribute to
0.5.
Type
number
Default
1
|
|
readonlyreadonly |
Makes the rating readonly.
Type
boolean
Default
false
|
|
requiredrequired |
Makes the rating a required field.
Type
boolean
Default
false
|
|
sizesize |
The component's size.
Type
'xs' | 's' | 'm' | 'l' | 'xl' | 'small' | 'medium' | 'large'
Default
'm'
|
|
validationTarget |
Override this to change where constraint validation popups are anchored.
Type
undefined | HTMLElement
|
|
validators |
Validators are static because they have
observedAttributes, essentially attributes to "watch"
for changes. Whenever these attributes change, we want to be notified and update the validator.
Type
Validator[]
Default
[]
|
|
valuevalue |
The current rating.
Type
number
Default
0
|
Methods
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
formStateRestoreCallback() |
Called when the browser is trying to restore element’s state to state in which case reason is "restore", or when the browser is trying to fulfill autofill on behalf of user in which case reason is "autocomplete". In the case of "restore", state is a string, File, or FormData object previously set as the second argument to setFormValue. |
state: string | File | FormData | null,
reason: 'autocomplete' | 'restore'
|
resetValidity() |
Reset validity is a way of removing manual custom errors and native validation. | |
setCustomValidity() |
Do not use this when creating a "Validator". This is intended for end users of components. We track manually defined custom errors so we don't clear them on accident in our validators. |
message: string
|
Events
Learn more about events.
| Name | Description |
|---|---|
change |
Emitted when the rating's value changes. |
wa-hover |
Emitted when the user hovers over a value. The phase property indicates when hovering starts, moves to a new value, or ends. The value property tells what the rating's value would be if the user were to commit to the hovered value. |
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
CSS Custom Properties
Learn more about CSS custom properties.
| Name | Description |
|---|---|
--symbol-color |
The inactive color for symbols.
|
--symbol-color-active |
The active color for symbols.
|
--symbol-spacing |
The spacing to use around symbols.
|
CSS Parts
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
base |
The component's base wrapper. |
::part(base)
|
Dependencies
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.