Rating

<sl-rating> | SlRating
Since 1.0 stable

Ratings give users a way to quickly view and provide feedback.

<sl-rating label="Rating"></sl-rating>

Examples

Labels

Ratings are commonly identified contextually, so labels aren’t displayed. However, you should always provide one for assistive devices using the label attribute.

<sl-rating label="Rate this component"></sl-rating>

Maximum Value

Ratings are 0–5 by default. To change the maximum possible value, use the max attribute.

<sl-rating label="Rating" max="3"></sl-rating>

Precision

Use the precision attribute to let users select fractional ratings.

<sl-rating label="Rating" precision="0.5" value="2.5"></sl-rating>

Symbol Sizes

Set the --symbol-size custom property to adjust the size.

<sl-rating label="Rating" style="--symbol-size: 2rem;"></sl-rating>

Readonly

Use the readonly attribute to display a rating that users can’t change.

<sl-rating label="Rating" readonly value="3"></sl-rating>

Disabled

Use the disable attribute to disable the rating.

<sl-rating label="Rating" disabled value="3"></sl-rating>

Detecting Hover

Use the sl-hover event to detect when the user hovers over (or touch and drag) the rating. This lets you hook into values as the user interacts with the rating, but before they select a value.

The event has a payload with phase and value properties. The phase property tells when hovering starts, moves to a new value, and ends. The value property tells what the rating’s value would be if the user were to commit to the hovered value.

<div class="detect-hover">
  <sl-rating label="Rating"></sl-rating>
  <span></span>
</div>

<script>
  const rating = document.querySelector('.detect-hover > sl-rating');
  const span = rating.nextElementSibling;
  const terms = ['No rating', 'Terrible', 'Bad', 'OK', 'Good', 'Excellent'];

  rating.addEventListener('sl-hover', event => {
    span.textContent = terms[event.detail.value];

    // Clear feedback when hovering stops
    if (event.detail.phase === 'end') {
      span.textContent = '';
    }
  });
</script>

<style>
  .detect-hover span {
    position: relative;
    top: -4px;
    left: 8px;
    border-radius: var(--sl-border-radius-small);
    background: var(--sl-color-neutral-900);
    color: var(--sl-color-neutral-0);
    text-align: center;
    padding: 4px 6px;
  }

  .detect-hover span:empty {
    display: none;
  }
</style>

Custom Icons

You can provide custom icons by passing a function to the getSymbol property.

<sl-rating label="Rating" class="rating-hearts" style="--symbol-color-active: #ff4136;"></sl-rating>

<script>
  const rating = document.querySelector('.rating-hearts');
  rating.getSymbol = () => '<sl-icon name="heart"></sl-icon>';
</script>

Value-based Icons

You can also use the getSymbol property to render different icons based on value.

<sl-rating label="Rating" class="rating-emojis"></sl-rating>

<script>
  const rating = document.querySelector('.rating-emojis');

  rating.getSymbol = value => {
    const icons = ['face-angry', 'face-frown', 'face-meh', 'face-smile', 'face-laugh'];
    return `<sl-icon name="${icons[value - 1]}"></sl-icon>`;
  };
</script>

Chip Symbol

Set symbol="chip" to render a numbered CSAT/Likert-style scale instead of stars. Chips are a single-select, whole-number scale — unlike stars, they don’t support fractional precision.

<sl-rating label="How useful was this?" symbol="chip" value="3"></sl-rating>

Chip Sizes

Use the size attribute to change the size of the chips. This attribute only affects symbol="chip" — star mode continues to use the --symbol-size custom property.





<sl-rating label="Rating" symbol="chip" size="small" value="2"></sl-rating>
<br /><br />
<sl-rating label="Rating" symbol="chip" size="medium" value="3"></sl-rating>
<br /><br />
<sl-rating label="Rating" symbol="chip" size="large" value="4"></sl-rating>

Chip with Tooltip

Add show-tooltip to reveal a tooltip to the right of the chips with a sentiment label for the current value. The label follows the value automatically as you hover or select a chip.

<sl-rating label="How useful was this?" symbol="chip" show-tooltip></sl-rating>

For a different scale, set tooltipLabels to an array of terms, one per whole-number value from 0 to max. It’s also used as each chip’s accessible name, so assistive technology always announces the sentiment even when the tooltip isn’t shown.

<sl-rating label="Agreement" symbol="chip" show-tooltip max="4" class="rating-agreement"></sl-rating>

<script>
  const rating = document.querySelector('.rating-agreement');
  const terms = ['No answer', 'Disagree', 'Neutral', 'Agree', 'Strongly agree'];

  rating.tooltipLabels = terms;
</script>

If a label can’t be expressed as a simple array — say it depends on other state — override getTooltipLabel instead; it defaults to reading from tooltipLabels.

Chip Disabled

Chips respect the same disabled attribute as stars, dimming to 50% opacity and removing every chip from the tab order.

<sl-rating label="Rating" symbol="chip" disabled value="3"></sl-rating>

Properties

Name Description Reflects Type Default
label A label that describes the rating to assistive devices. string ''
value The current rating. number 0
max The highest rating to show. number 5
precision The precision at which the rating will increase and decrease. For example, to allow half-star ratings, set this attribute to 0.5. number 1
readonly Makes the rating readonly. boolean false
disabled Disables the rating. boolean false
getSymbol 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 <sl-icon> elements. (value: number) => string -
symbol Determines how the rating is rendered: a continuous star scale, or a discrete numbered-chip (CSAT/Likert) scale. RatingSymbol 'star'
size The size of the chips. Only meaningful when symbol="chip"; star mode keeps using --symbol-size. RatingSize 'medium'
showTooltip
show-tooltip
Shows an sl-tooltip with a sentiment label to the right of the chips. Only meaningful when symbol="chip". boolean false
tooltipLabels
tooltip-labels
The sentiment label for each whole-number value (0-max), in order. Used as each chip’s accessible name, and, when show-tooltip is set, as the tooltip’s content. Defaults to the design system’s 0–5 CSAT scale — set this directly for a different max/scale, e.g. rating.tooltipLabels = ['No answer', 'Disagree', ...]. string[] [ 'Not yet rated', 'Not useful', 'Slightly useful', 'Useful', 'Very useful', 'Exactly what we need' ]
getTooltipLabel Returns the sentiment label for a given whole-number value (0-max). Defaults to reading from tooltipLabels; override this instead if the label can’t be expressed as a simple array (e.g. it depends on other state). (value: number) => string -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
sl-change Emitted when the rating’s value changes. -
sl-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. { phase: 'start' | 'move' | 'end', value: number }

Learn more about events.

Methods

Name Description Arguments
focus() Sets focus on the rating. options: FocusOptions
blur() Removes focus from the rating. -

Learn more about methods.

Custom Properties

Name Description Default
--symbol-color The inactive color for symbols.
--symbol-color-active The active color for symbols.
--symbol-size The size of symbols.
--symbol-spacing The spacing to use around symbols.

Learn more about customizing CSS custom properties.

Parts

Name Description
base The component’s base wrapper.
chip Each individual chip, only rendered when symbol="chip".
chip--selected Applied to the currently selected chip.

Learn more about customizing CSS parts.