# Rating

> Stars to give a score, or to show an average.

Page: https://uicontext.com/docs/components/rating

Group: Primitives.

## When to use

Stars to give a score, or to show an average. Use [Slider](https://uicontext.com/docs/components/slider.md) instead when the score isn't a few stars.

## Usage

```ts
import { Rating } from "./ui/uicontext/components/Rating.ts"

const Message = UiSchema.TaggedEnum({ Rated: { stars: Schema.NullOr(Schema.Number) } })

export const Review = (h: Html.H<typeof Message.Type>, stars: number | null) =>
	Rating(h, { label: "Your rating", name: "rating", showLabel: true, value: stars, onChange: (next) => Message.Rated({ stars: next }) })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### After a support chat

The example the page opens with: Rating in “After a support chat”.

```ts title="src/demo.ts" source="www/src/examples/rating/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Rating } from "./ui/uicontext/components/Rating.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	card: { display: "grid", gap: space.md, inlineSize: "min(100%, 24rem)" },
	question: { margin: 0, fontSize: font.base, fontWeight: 600, color: color.fg },
	thanks: { margin: 0, minBlockSize: "1.4em", fontSize: font.sm, color: color.muted },
})

const Message = UiSchema.TaggedEnum({ Rated: { stars: Schema.NullOr(Schema.Number) } })

// the question at the end of a support chat
export const SupportRating = Html.component({
	name: "SupportRating",
	model: Schema.Struct({ stars: Schema.NullOr(Schema.Number) }),
	message: Message,
}).make({
	init: () => ({ stars: null }),
	update: { Rated: ($, { stars }) => $.evolve({ stars }) },
	view: (h, { stars }) =>
		h.div.attrs(sx(styles.card)).children(
			h.p.attrs({ "aria-hidden": "true", ...sx(styles.question) }).children("How did Priya do?"),
			Rating(h, {
				label: "How did Priya do?",
				name: "support-rating",
				value: stars,
				words: ["Poor", "Fair", "Good", "Great", "Excellent"],
				clearable: true,
				onChange: (next) => Message.Rated({ stars: next }),
			}),
			h.p.attrs({ role: "status", ...sx(styles.thanks) }).children(stars === null ? "" : stars <= 2 ? "Sorry about that. Tell us what went wrong?" : "Thanks for letting us know."),
		),
})
```

### Showing a score

`RatingDisplay` draws an average, fills part of a star for the fraction, and reads as “4.6 out of 5, from 1,284 ratings”.

```ts title="src/display.ts" source="www/src/examples/rating/display.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { RatingDisplay } from "./ui/uicontext/components/Rating.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	list: { display: "grid", gap: space.lg, margin: 0, padding: 0, listStyle: "none", inlineSize: "min(100%, 24rem)" },
	item: { display: "grid", gap: space.xs },
	name: { fontSize: font.base, fontWeight: 500, color: color.fg },
})

const products = [
	{ name: "Walnut desk organiser", score: 4.6, count: 1284 },
	{ name: "Linen apron, oat", score: 4.1, count: 87 },
	{ name: "Cast iron grill pan, 28 cm", score: 3.4, count: 12 },
	{ name: "Stoneware mug", score: 5, count: 1 },
]

// scores on product cards: part of a star for the fraction, and how many it's from
export const ProductScores = <M>(h: Html.H<M>) =>
	h.ul.attrs(sx(styles.list)).children(
		products.map((p) =>
			h.li.key(p.name).attrs(sx(styles.item)).children(h.span.attrs(sx(styles.name)).children(p.name), RatingDisplay(h, { value: p.score, count: p.count }))),
	)
```

## API reference

### Rating (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` | — | The question, as the group's legend: "How was your stay?" |
| `showLabel` | `boolean` | — | Show the label above the stars; it's still read out when hidden. |
| `value` (required) | `number \| null` | — | The stars given, or null for none yet. The caller keeps it. |
| `onChange` (required) | `(value: number \| null) => Message` | — | — |
| `max` | `number` | `5` | How many stars; 5 by default. |
| `words` | `string[]` | — | Words for each score, shown beside the stars and read with them: ["Poor", …, "Excellent"]. |
| `clearable` | `boolean` | — | Pressing the picked star again clears the rating. |
| `disabled` | `boolean` | — | — |
| `name` (required) | `string` | — | Submits the value under this form field name. |
| `style` | `Style` | — | — |

### RatingDisplay (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `value` (required) | `number` | The average, such as 4.6. |
| `max` | `number` | — |
| `count` | `number` | How many ratings it's from; shown as "(1,284)". |
| `locale` | `string` | The reader's locale, for the numbers. |
| `style` | `Style` | — |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Right arrow, Left arrow | Give one star more or less |
| Tab | Reach the stars once, on the score given |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/rating
```

With npm: `npx shadcn@latest add checkfu/uicontext/rating`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/rating`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/rating`.

It writes these files:

- `src/ui/uicontext/components/Rating.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Rating.ts](https://github.com/checkfu/uicontext/blob/main/src/Rating.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
