Pick a score from a row of stars, whole or half, with a preview as the pointer moves over them.

Use a rating to collect a score, like how a stay went, or to show one, like an average. Each star is a radio, so it works like a radio group, with one value and a single tab stop.

import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Rating, RatingItem } from "@ui-sdk/angular/rating"

@Component({
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div uiRating #rating="uiRating" aria-label="Rating" [defaultValue]="3">
      @for (index of rating.items(); track index) {
        <span uiRatingItem [index]="index">
          <svg lucideStar></svg>
        </span>
      }
    </div>
  `,
})
class Example {}

export { Example }
Choose a star, then use the arrow keys to change the score.

Installation

npm install @ui-sdk/angular @ui-sdk/core

Composition

Use the following composition to build Rating:

uiRating
โ””โ”€โ”€ uiRatingItem

Render a uiRatingItem for each star: the root's items() lists them, from 1, and each item takes its index.

Usage

Import the parts and put them together:

import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Rating, RatingItem } from "@ui-sdk/angular/rating"

@Component({
  selector: "app-example",
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div uiRating #rating="uiRating" aria-label="Rating" [defaultValue]="3">
      @for (index of rating.items(); track index) {
        <span uiRatingItem [index]="index">
          <svg lucideStar></svg>
        </span>
      }
    </div>
  `,
})
class Example {}

export { Example }

Name the rating with aria-label, or put it in a Field whose label names it. Without defaultValue, nothing starts checked and Tab enters on the first star.

Stars

Put your icon in uiRatingItem. Each item says how much of its star the shown score covers: --rating-fill goes from 0 to 1, data-filled marks a whole star and data-partial the star a score ends partway through. The radios that pick the score sit over the icon, so it needs no events of its own.

[data-slot="rating-item"] {
  color: lightgray;
}

[data-slot="rating-item"][data-filled] {
  color: gold;
}

For half stars and averages, paint the icon up to calc(var(--rating-fill) * 100%), like with a gradient behind a star-shaped mask.

Read the item's state from a template reference, so an icon can fill once the score covers the star:

<span uiRatingItem [index]="index" #ratingItem="uiRatingItem">
  <svg
    lucideStar
    [fill]="ratingItem.state().fill === 1 ? 'currentColor' : 'none'"
  ></svg>
</span>

Half Stars

Set [step]="0.5" to split each star in two. Each half is its own radio, named like "2.5 stars", and a press on the first half of a star picks the half score.

import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Rating, RatingItem } from "@ui-sdk/angular/rating"

@Component({
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div
      uiRating
      #rating="uiRating"
      aria-label="Rating"
      [step]="0.5"
      [defaultValue]="3.5"
    >
      @for (index of rating.items(); track index) {
        <span uiRatingItem [index]="index">
          <svg lucideStar></svg>
        </span>
      }
    </div>
  `,
})
class Example {}

export { Example }
Choose the first half of a star. Its star fills halfway.

Preview

While a pointer moves over the stars, they show the score under it: --rating-fill follows the pointer, and data-highlighted marks the stars up to it. Leaving the stars shows the score again. Touch has no preview, since a tap picks the score at once.

Deselectable

Set deselectable to let people clear their score: a press on the checked star, or Space on it, sets the score back to null.

import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Rating, RatingItem } from "@ui-sdk/angular/rating"

@Component({
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div
      uiRating
      #rating="uiRating"
      aria-label="Rating"
      [defaultValue]="4"
      deselectable
    >
      @for (index of rating.items(); track index) {
        <span uiRatingItem [index]="index">
          <svg lucideStar></svg>
        </span>
      }
    </div>
  `,
})
class Example {}

export { Example }
Press the fourth star. The score clears.

Read-Only

Set readOnly to show a score that can't change, like an average. Any score shows, like 4.2, which fills a fifth of the last star. The rating becomes one image for screen readers, and its radios are hidden from them.

Name a read-only rating with getValueLabel, so its name includes the score. An aria-label would replace it.

import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Rating, RatingItem, type RatingOptions } from "@ui-sdk/angular/rating"

@Component({
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div
      uiRating
      #rating="uiRating"
      [value]="4.2"
      readOnly
      [getValueLabel]="getValueLabel"
    >
      @for (index of rating.items(); track index) {
        <span uiRatingItem [index]="index">
          <svg lucideStar></svg>
        </span>
      }
    </div>
  `,
})
class Example {
  readonly getValueLabel: RatingOptions["getValueLabel"] = (value, max) =>
    `Rated ${value} out of ${max}`
}

export { Example }
An average of 4.2, named Rated 4.2 out of 5.

Disabled

Set disabled to stop every star from being focused or changed. A disabled rating submits nothing.

import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Rating, RatingItem } from "@ui-sdk/angular/rating"

@Component({
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div
      uiRating
      #rating="uiRating"
      aria-label="Rating"
      [defaultValue]="2"
      disabled
    >
      @for (index of rating.items(); track index) {
        <span uiRatingItem [index]="index">
          <svg lucideStar></svg>
        </span>
      }
    </div>
  `,
})
class Example {}

export { Example }
The rating is disabled, so the stars can't be focused or pressed.

Controlled

Bind value with [(value)], and pass (beforeValueChange) for the reason. Call $event.cancel() in the callback to keep the current score. A controller from createRatingController() sets the score from anywhere, like the Clear Rating button below. Create one controller per mounted rating.

Console
import { Component, signal } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { useLog } from "./log"
import {
  createRatingController,
  Rating,
  RatingItem,
} from "@ui-sdk/angular/rating"

@Component({
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div>
      <div data-slot="controls">
        <button type="button" (click)="rating.setValue(null)">
          Clear Rating
        </button>
      </div>
      <div
        uiRating
        aria-label="Rating"
        [(value)]="value"
        (beforeValueChange)="
          log('value: ' + $event.value + ' (' + $event.reason + ')')
        "
        [controller]="rating"
        #stars="uiRating"
      >
        @for (index of stars.items(); track index) {
          <span uiRatingItem [index]="index">
            <svg lucideStar></svg>
          </span>
        }
      </div>
    </div>
  `,
})
class Example {
  readonly log = useLog()

  readonly rating = createRatingController()

  readonly value = signal<number | null>(4)
}

export { Example }
Choose a star, then press Clear Rating. The console logs each change and its reason.

See State and Events for controlled state, cancellation and debugging.

Form

Each radio renders a hidden native radio, so the rating takes part in forms. Set name to submit the score; nothing submits without a name or a score. required blocks submission until there's a score, and a reset returns to defaultValue.

Console
import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { useLog } from "./log"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import { Rating, RatingItem } from "@ui-sdk/angular/rating"

@Component({
  imports: [Field, FieldLabel, Rating, RatingItem, LucideStar],
  template: `
    <form data-slot="stack" (submit)="onSubmit($event)">
      <div uiField>
        <label uiFieldLabel>Rate your stay</label>
        <div uiRating #rating="uiRating" name="stay" required>
          @for (index of rating.items(); track index) {
            <span uiRatingItem [index]="index">
              <svg lucideStar></svg>
            </span>
          }
        </div>
      </div>
      <div data-slot="row">
        <button type="submit" data-variant="solid">Send Review</button>
        <button type="reset">Reset</button>
      </div>
    </form>
  `,
})
class Example {
  readonly log = useLog()

  onSubmit(event: SubmitEvent) {
    event.preventDefault()
    this.log(
      `Submitted stay=${new FormData(event.currentTarget as HTMLFormElement).get("stay")}`
    )
  }
}

export { Example }
Press Send Review without a score to see the browser's message. Choose a star and send it again.

Stars and Labels

Set max for the number of stars, and loop to let the arrow keys wrap from the last star to the first. Each radio is named by getValueLabel, which gets the score and max and says "1 star" or "3 stars" by default.

import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Rating, RatingItem } from "@ui-sdk/angular/rating"

@Component({
  selector: "app-example",
  imports: [Rating, RatingItem, LucideStar],
  template: `
    <div
      uiRating
      #rating="uiRating"
      aria-label="Difficulty"
      [max]="10"
      loop
      [getValueLabel]="getValueLabel"
    >
      @for (index of rating.items(); track index) {
        <span uiRatingItem [index]="index">
          <svg lucideStar></svg>
        </span>
      }
    </div>
  `,
})
class Example {
  readonly getValueLabel = (value: number, max: number) => `${value} of ${max}`
}

export { Example }

Styling

Parts are unstyled. Use data-filled, data-partial, data-checked and data-highlighted on each star, and --rating-fill for how full it is. data-disabled, data-readonly and data-required are on the rating and its stars.

Each part has a data-slot, such as rating-item, and its state attributes are listed under Data Attributes.

RatingItem

VariableWhat it holds
--rating-fillHow much of the star the shown score covers, from 0 to 1.

Data Attributes

Rating

[data-slot="rating"]

AttributeWhat it means
data-requiredPresent when the rating is required.
data-readonlyPresent when the rating only shows its score.
data-disabledPresent when the rating is disabled, by its own prop or a <fieldset>.

RatingItem

[data-slot="rating-item"]

AttributeWhat it means
data-filledPresent on stars the shown score covers whole.
data-partialPresent on the star the shown score covers part of, like the last of 2.5.
data-checkedPresent on the star the score ends in.
data-disabledPresent when the rating is disabled, by its own prop or a <fieldset>.
data-readonlyPresent when the rating only shows its score.
data-highlightedPresent on the stars up to the score a pointer previews.

API Reference

Each option below is an input on the directive, and each callback an output. A change callback is two outputs: (beforeValueChange), with the details and cancel(), then (valueChange) with the new value alone. Bind value with [(value)].

Rating

value?number | null
The score. Pass it to control the rating. null means no score yet.
defaultValue?number | null
The score at first, when uncontrolled. A form reset returns to it.
(beforeValueChange)
Runs when a user or a form reset wants to change the score. details.reason says why, and details.cancel() stops it.
max?number
How many stars there are.
step?number
The smallest change, as a fraction of a star: 0.5 makes half stars, each half its own radio.
deselectable?boolean
Lets a press on the checked star clear the score.
disabled?boolean
Disables every star: they can't be focused or changed, and the score isn't submitted.
readOnly?boolean
Shows the score as an image, named by getValueLabel, that can't be changed. Any score shows, like an average of 4.2.
required?boolean
Blocks form submission until there's a score, with the browser's own message.
name?string
The name the score submits under. Without one, the rating submits nothing.
form?string
The id of a form to belong to, when the rating isn't inside that form.
loop?boolean
Whether arrow keys wrap from the last star to the first.
getValueLabel?((value: number, max: number) => string)
Names a score, for each star's radio and for a read-only rating.
controller?{ getValue: () => number | null; setValue: (value: number | null, details?: RatingChangeOptions) => boolean; focus: () => void; }
Sets the score from anywhere. Create one with createRatingController().
debug?boolean
Logs every state change with its reason to the console, in development builds.

State

What each part's state() signal holds.

valuenumber | null
The score, or null before anyone rates.
highlightednumber | null
The score a pointer previews over the stars, or null while none does.
disabledboolean
Whether the rating is disabled, by itself or by a <fieldset>.
readOnlyboolean
Whether the rating only shows its score.
requiredboolean
Whether a form won't submit until there's a score.
indexnumber
Which star this is, from 1.
fillnumber
How much of this star the shown score covers, from 0 to 1. Also --rating-fill.
checkedboolean
Whether the score ends in this star.
highlightedboolean
Whether a pointer previews a score that reaches this star.
disabledboolean
Whether the rating is disabled, by itself or by a <fieldset>.
readOnlyboolean
Whether the rating only shows its score.

Reasons

$event.reason says why the score changed.

item-pressMouseEvent | KeyboardEvent
A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
list-navigationKeyboardEvent
Arrow, Home or End keys moved the selection, typing picked an item, or Ctrl or โŒ˜ with A selected every item of a listbox.
resetnone
A form reset.
restorenone
The browser restored state, like after going back, or a saved value was read back from storage.
imperativenone
A controller or api call.

Controller

Create one with createRatingController() and pass it as controller.

Every write method takes an optional { reason, event }, runs the change callback like a press, and returns false when the change is canceled.

getValue()() => number | null
The score, or null before anyone rates.
setValue(value, details?)(value: number | null, details?: RatingChangeOptions | undefined) => boolean
Sets the score, as a user would. Disabled and read-only ratings ignore it, and it returns false for a score no star has.
focus()() => void
Moves focus to the checked star, or the first one.

Events

A before output emits { value, reason, event, cancel() } before the change, and its plain output follows with the value alone. A bound value only updates when (beforeValueChange) wasn't canceled.

BindingOnReceives
[(value)]uiRatingThe score, or null
(beforeValueChange)uiRating{ value, reason, cancel() }: the score or null, and the reason

Accessibility

A rating is a radio group, so it follows the WAI-ARIA radio group pattern and behaves like native radios that share a name. Each star holds a button with the radio role per score it can pick, and a hidden native radio that takes part in forms. Icons inside the stars should be hidden from screen readers with aria-hidden, since each radio is already named. A read-only rating has the img role instead, named by getValueLabel.

Keyboard

KeyDoes
TabMoves into the rating, onto the checked star or the first one
Arrow keysMove to the next or previous score and check it, stopping at the ends unless loop is set
SpaceChecks the focused star, or clears it when it's checked and deselectable
EnterSubmits its form

Tested Behavior

Every line below is an automated test.

Selection
  • The default score is checked, and the stars up to it are full.
  • Clicking a star checks it and says why.
  • A half star fills half of its star.
  • Clicking the checked star clears a deselectable rating, and only that one.
  • A change can be cancelled.
  • A controller sets the score.
Preview
  • A pointer over a star previews its score, and leaving shows the score again.
Keyboard
  • Tab lands on the checked star, and the next Tab leaves.
  • Arrow keys change the score by a step, and stop at the ends.
  • Space on the checked star clears a deselectable rating.
Read-only
  • A read-only rating is one image, named by its label, that shows any score.
  • A read-only rating holds nothing focusable, as an image must.
Forms
  • Each score submits under its rating's name.
  • A form reset returns to the default score.
  • A disabled rating submits nothing, and its stars can't be focused or checked.

Runs in React, Vue and web components.