Overview
A rating is a row of stars. With <span> stars it shows a score. With <button> stars the user picks a score. A menu variant puts a score summary in a trigger and the vote in a panel.
Pick another component when:
- the user answers yes or no: Switch
- the bar shows progress, not opinion: Progress
- the page asks for feedback at its end: User Feedback
Markup
Variants
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Display (default) | — | — | A read-only score, such as the average of many reviews |
| Structure | Interactive | canon #rating-interactive |
— | The user picks a score |
| Structure | Form (id: form) | canon #rating-form |
— | An interactive rating in a form: a group with a question and a hidden input that sends the score |
| Structure | Menu | canon #rating-menu |
— | A score summary in a trigger, with the vote in a panel. Style, Value and Loading change the summary rating |
| Size | XS | .nds-xs |
.nds-rating:not(.nds-xs):not(.nds-dropmenu .nds-rating) |
16px stars. Inline next to a title or a list item |
| Size | SM | .nds-sm |
.nds-rating:not(.nds-dropmenu .nds-rating) |
24px stars. Inline, a little larger |
| Size | MD (default) | — | — | 32px stars. Most layouts |
| Size | LG | .nds-lg |
.nds-rating:not(.nds-dropmenu .nds-rating) |
48px stars, 40px on phones. A featured review or a hero |
| Style | Brand | .nds-brand |
.nds-rating |
Stars in the brand primary color, not the secondary color |
| Value | Empty (not: form) | [data-rating="0"] |
.nds-rating |
No star is filled. The start of a new vote |
| Value | Whole (hint: 4 of 5 stars, no half star) (not: form) | [data-rating="4"] |
.nds-rating |
A whole number fills that many stars |
| Value | Half star (default) (not: form) | — | — | The stars show the score to the nearest half: 3.5 fills half of the fourth star |
| Required | Required (hint: The user must pick a score before the form submits) | [data-required] |
.nds-form-group |
The form does not submit until the user picks a score |
| State (any) | Loading | .nds-loading |
.nds-rating |
The stars pulse as placeholders while the score loads |
| State (any) | Disabled | [data-state~="disabled"] |
.nds-rating:has(> button):not(.nds-dropmenu .nds-rating) |
The user cannot pick a score now. Interactive only |
| State (any) | Disabled | [disabled] |
button.nds-rating-star:not(.nds-dropmenu .nds-rating-star) |
Write both parts of this choice |
Behavior
Display
Stars built from <span> elements show data-rating and nothing else. They do not react to the pointer or the keyboard. Set data-rating to the score, from 0 to the number of stars.
Interactive
Stars built from <button> elements let the user pick a score. Hover shows the score a click would give. A click, Enter or Space sets the score. The script writes it back to data-rating and fires nds:rating:change. The script also writes an aria-label on each star in the page’s language, such as 3 stars, so the buttons need no label in the markup.
Form
The Form structure puts an interactive rating in a .nds-form-group, with a <legend> that asks the question. The stars are buttons, not inputs, so the score posts through a hidden input with data-rating-value. The script writes the score into it on each pick, and 0 when no star is picked. After a form reset, the script puts back the score the page loaded with. User Feedback uses the same input.
Required
data-required on the .nds-form-group makes the score required. Forms validation treats data-rating="0" as empty: it shows “Please choose a rating” and focuses the first star.
Menu
The Menu structure puts a display rating and a vote count in a Dropmenu trigger. The panel holds an interactive rating and a Vote button. Put nds-rating-menu on the .nds-dropmenu-menu. The page owns the vote: listen for nds:rating:change, and send the score from your own code.
Built-in Features
-
Auto-initialization
The script starts every
.nds-ratingon the page, and every one added or removed later. -
Screen Reader Score
A screen reader reads a display rating as one image with its score, such as "3.5 of 5", in the page's language. An interactive rating is a group of radios: each star has its own name, such as "2 stars", and the picked star reads as checked.
-
Mode Detection
The tag of the stars sets the mode.
<button>stars are interactive,<span>stars are display only. -
Half Stars
The stars show the score to the nearest half: 4.2 shows 4 stars, 4.3 to 4.7 show four and a half, and 4.8 shows 5. A screen reader still reads the exact score.
-
Keyboard Navigation
The rating is one tab stop. The arrow keys pick the next or previous score; left and right follow the text direction. Home and End pick the lowest and highest score. Enter and Space pick the focused star. Escape leaves the star.
-
Phone Size
The large stars shrink from 48px to 40px on phones.
-
Loading Placeholder
.nds-loadingturns the stars into pulsing placeholders. -
Programmatic Control
Read, set, disable and enable a rating from code. Each change that picks a new score fires
nds:rating:change.
Best Practices
- Use five stars. Users expect five.
- Use
nds-xsornds-smnext to a title or a list item. Use the default size in most layouts. Usends-lgfor a featured review. - In a List, leave the size class off. The list makes the stars SM, a small list (
nds-sm) XS, and a large list (nds-lg) MD. - Set
data-ratingto the score on a display rating. Set it to0on an interactive rating that collects a new score. - Do not use a rating for a yes or no choice. Use a Switch.
- Do not use a rating to show progress. Use Progress.
- Use
nds-brandwhere the secondary color does not fit the page identity. - Add a visible label near an interactive rating, such as “Rate this service”. In a form, the group’s
<legend>is the label. - Put the rating in a Dropmenu when a compact trigger collects the vote.
- To show a review count beside the stars, write your own text next to the rating.
.nds-card-ratingis a Card part: use it only in a card. - Do not write
nds-md. The default size has no class.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
.nds-rating-star |
a child of .nds-rating |
One star. A <button> makes the rating interactive |
.nds-rating-menu |
.nds-dropmenu-menu |
Centers the label and the stars of the vote item in a Dropmenu, and sets the menu padding |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-rating |
.nds-rating |
The score, from 0 to the number of stars. The script writes the new score here on every pick |
data-state~="interactive" |
.nds-rating |
The script sets it at load when the first star is a <button> and the rating is not disabled. It turns on hover, press and pointer styles |
data-state~="disabled" |
.nds-rating |
Write it, with disabled on each star, for a rating that starts disabled. The script also sets it in setDisabled(true) and removes it in setDisabled(false) |
data-state~="selected" |
.nds-rating-star |
The script sets it on the first N stars, where N is the whole part of the score, and removes it when the score drops |
data-state~="half" |
.nds-rating-star |
The script sets it on the star after the whole stars when the score rounds to a half |
data-state~="preview" |
.nds-rating-star |
The script sets it on the stars up to the hovered star, and removes it when the pointer leaves |
data-value |
.nds-rating-star |
The script writes the 1-based position of the star |
data-required |
.nds-form-group that holds a rating |
The rating must be above 0 before the form submits |
data-rating-value |
a hidden <input> in the .nds-form-group, or in .nds-rating |
Holds the score for the form. The script writes it on each pick, and 0 when no star is picked |
CSS Custom Properties
Set these on .nds-rating. --star-size also reaches it from a parent, and it wins over the size classes. nds-brand sets the four color properties.
| Property | Default | Controls |
|---|---|---|
--star-size |
32px |
Width and height of each star |
--star-color |
var(--rating-star-default-default) |
Color of an empty star |
--star-selected |
var(--rating-star-default-selected) |
Color of a filled star |
--star-pressed |
var(--rating-star-default-pressed) |
Color of a star while pressed |
--star-hovered |
var(--rating-star-default-hovered) |
Color of a star on hover, interactive only |
Tokens
Set a token at :root, or on a wrapper to reach every rating inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the rating group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--rating-star-default-default | var(--colors-neutral-300) | |
--rating-star-default-selected | var(--colors-secondary-600) | |
--rating-star-default-pressed | var(--colors-secondary-700) | |
--rating-star-default-hovered | var(--colors-secondary-500) | |
--rating-star-brand-default | var(--colors-neutral-300) | |
--rating-star-brand-selected | var(--colors-primary-600) | |
--rating-star-brand-pressed | var(--colors-primary-700) | |
--rating-star-brand-hovered | var(--colors-primary-500) |
JavaScript
The NDS.Rating script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
The instance is on the element as el.ndsRating. It is there once the component starts: code that runs at page load waits for NDS.init.ready.
| Method | Effect |
|---|---|
NDS.Rating.init() |
Start every .nds-rating that has no instance |
NDS.Rating.create(el) |
Start one rating and return its instance. It returns the existing instance when there is one |
NDS.Rating.destroy(el) |
The same as el.ndsRating.destroy() |
NDS.Rating.validate(el, options) |
Check the rating group and return { valid, message, rating }. Pass { showMessage: false } to check without a message |
NDS.Rating.enable(el) |
Start the rating if needed, then take it out of disabled |
NDS.Rating.getValue(el) |
Return the score, 0 for no stars, or '' when the rating is not found. el is the rating, its group, its hidden input, its id or a selector |
NDS.Rating.setValue(el, value) |
Set the score and return true. It returns false, and changes nothing, when value is not from 0 to the star count, or the rating is not found. The event fires only when the score changes |
NDS.Rating.clear(el) |
Set the score to 0, with no stars. Return true, or false when the rating is not found |
el.ndsRating.setValue(v) |
The same as NDS.Rating.setValue, on a rating that has started |
el.ndsRating.getValue() |
Return the score |
el.ndsRating.clear() |
Set the score to 0 |
el.ndsRating.enable(), el.ndsRating.disable() |
Enable or disable the rating |
el.ndsRating.isDisabled() |
Return whether the rating is disabled |
el.ndsRating.destroy() |
Remove the listeners and el.ndsRating. NDS.Rating.create(el) then starts a new one |
| Event | Fired on | Detail |
|---|---|---|
nds:rating:change |
.nds-rating (bubbles) |
{ rating, value, element }. rating and value are the same score |
The full API is in the banner of _js/nds-rating.js.