<cs-rating>
StableFormsSince 0.1

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 cs-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.



Submit

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.



Submit

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.



Submit Reset

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/rating/rating.js';
import './cornerstone/components/rating/rating.js';
import CsRating from '@cruglobal/cornerstone-components/react/rating/index.js';

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
defaultValuedefault-valueThe default value of the form control. Used to reset the rating to its initial value.number0
disableddisabledDisables the rating.booleanfalse
formBy 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.HTMLFormElement | null
getSymbolgetSymbolA 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 <cs-icon> elements.(value: number, isSelected: boolean) => string
labellabelA label that describes the rating to assistive devices.string''
maxmaxThe highest rating to show.number5
namenameThe name of the rating, submitted as a name/value pair with form data.string | nullnullYes
precisionprecisionThe precision at which the rating will increase and decrease. For example, to allow half-star ratings, set this attribute to 0.5.number1
readonlyreadonlyMakes the rating readonly.booleanfalseYes
requiredrequiredMakes the rating a required field.booleanfalseYes
sizesizeThe component's size.'xs' | 's' | 'm' | 'l' | 'xl''m'Yes
validationTargetOverride this to change where constraint validation popups are anchored.undefined | HTMLElement
validatorsValidators 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.Validator[][]
valuevalueThe current rating.number0

Methods

Learn more about methods.

NameDescriptionArguments
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.

NameDescription
changeEmitted when the rating's value changes.
cs-hoverEmitted 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.
cs-invalidEmitted when the form control has been checked for validity and its constraints aren't satisfied.

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--symbol-colorThe inactive color for symbols.
--symbol-color-activeThe active color for symbols.
--symbol-spacingThe spacing to use around symbols.

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
ratingThe component's outer wrapper.::part(rating)

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, are included in this list.