Stars to give a score, or to show an average.
- 1.8 kB JS 3.0 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- 2 keys checked
- Right-to-left
- 9 tests
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 chatexport 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."), ),})When to use
Stars to give a score, or to show an average. Use Slider instead when the score isn't a few stars.
Usage
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
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”.
- Walnut desk organiser4.6(1,284)
- Linen apron, oat4.1(87)
- Cast iron grill pan, 28 cm3.4(12)
- Stoneware mug5.0(1)
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 fromexport 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
RatingView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | 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. |
valuerequired | number | null | — | The stars given, or null for none yet. The caller keeps it. |
onChangerequired | (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 | — | — |
namerequired | string | — | Submits the value under this form field name. |
style | Style | — | — |
RatingDisplayView function
Options
| Name | Type | Description |
|---|---|---|
valuerequired | 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 | Does |
|---|---|
| →Right arrow ←Left arrow | Give one star more or less |
| TabTab | Reach the stars once, on the score given |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/rating