Command palette

@yasinsensoy/web

Web · Components

Rating

Rating and showing scores with stars (or your own icon).

7 exports

Live example

Preview

Click, hover, slide your finger on a phone or try ←/→ and the number keys.

Code

import { RatingField } from "@yasinsensoy/web";

<RatingField label="How did you like this product?" name="puan" step={0.5} showValue size="lg" required
  labels={["Bad", "Okay", "Good", "Very good", "Excellent"]} />

Events

  1. Events appear here when you interact with the preview.

Product card: read only, fractional average and review count

Hearts, out of 10

Satisfaction: a separate face for each score (highlight=single)

😞🙁😐🙂😍

Controlled: write the previewed score yourself

Puan: 3 · Preview: —

Import

import { Rating, snapRating, ratingFromPosition, ratingFill } from "@yasinsensoy/web";

Rating

componentClient component

Rating and showing scores with stars (or your own icon).

  • Rating: By clicking, previewing by hovering with the mouse, or sliding a finger over the icons on a phone. Vertical scrolling isn't blocked; no accidental score while the page scrolls.
  • Half and fractional scores: half an icon with step={0.5}. While read-only any value like 4.3 is drawn at its exact ratio.
  • Flexible icons: heart, thumb, your own SVG with icon/emptyIcon; face scales with icon={(i) => …} and highlight="single".
  • Meaning: with labels each score's name becomes the tooltip, text and screen reader text.
  • Keyboard: ←/→ (↑/↓) step by step, digits set the score directly, Home/End, Backspace clears. Directions flip automatically in right-to-left languages.
  • Form: joins FormData with name, required works, resets with the form. For label, description and error see RatingField.
Rating(props: RatingProps): JSX.Element

Examples

<Rating name="score" aria-label="Product rating" />

<Rating readOnly value={4.3} size="sm" showValue={(v) => `${v} · 128 reviews`} />

<Rating name="score" step={0.5} showValue
  labels={["Bad", "Okay", "Good", "Very good", "Excellent"]} />

<Rating icon="heart" tone="danger" max={10} aria-label="Likes" />

<Rating highlight="single" icon={(i) => ["😞", "🙁", "😐", "🙂", "😍"][i]} aria-label="Satisfaction" />

snapRating

function

Snaps a value to step increments and clamps it to 0 – max. Floating point error (0.1 + 0.2) is cleaned by rounding to the step's digits.

snapRating(value: number, step: number, max: number): number

Examples

`snapRating(3.3, 0.5, 5)` → `3.5`

Produces a value from a position inside an icon. fraction is the position 0 – 1 from the icon's start (its right side in right-to-left languages). The value always rounds up: with half steps the left half of an icon gives x.5 and the right half x+1.

ratingFromPosition(index: number, fraction: number, step: number): number

Examples

`ratingFromPosition(2, 0.3, 0.5)` → `2.5`

ratingFill

function

How much of icon number index is drawn filled (0 – 1).

ratingFill(value: number, index: number): number

Classes that can be passed to the inner parts of Rating.

NameTypeDescription
items?stringRow wrapping the icons (the focused element).
item?stringBox of each icon.
filled?stringFilled icon layer (its color can be changed here, e.g. text-rose-500).
empty?stringEmpty icon layer.
value?stringValue or label text next to the icons.

Icon: name, element, or a function returning an icon by the icon's index (from 0). The function is for giving each score its own icon (e.g. faces).

type RatingIcon = IconProp | ((index: number) => IconProp)

RatingProps

interface

Props of the Rating component.

NameTypeDefaultDescription
value?numberScore in controlled use; 0 if there is no score.
defaultValue?number0Initial score in uncontrolled use.
onValueChange?(value: number) => voidCalled when the score changes (0 when cleared).
onHoverChange?(value: number | null) => voidScore previewed while hovering with the mouse or dragging a finger; null when the preview ends. For showing "What does this score mean?" yourself.
max?number5Number of icons (highest score).
step?number1Score step: 1 whole, 0.5 half star, 0.1 tenth. With half steps the left half of an icon gives the half score. While read-only the value isn't snapped to the step; 3.7 is drawn with 70% of the star filled.
icon?RatingIconFilled icon. If omitted, a star is drawn. Can be a name ("heart"), an element or an (index) => icon function giving each score its own icon. Outline icons are painted filled in the filled layer automatically.
emptyIcon?RatingIconEmpty icon. If omitted, a faded version of the filled icon is used.
highlight?"range" | "single""range"Which icons are colored? - "range": all up to the score (stars). - "single": only the selected icon; the others stay faded. For scales where each score has its own icon (😞 … 😍). The step is always 1.
labels?readonly ReactNode[]Meaning of each whole score (labels[0] is 1 point): ["Bad", "Okay", "Good", "Very good", "Excellent"]. Shown as a tooltip on hovering the icon, next to it if showValue is on, and read to screen readers.
showValue?boolean | ((value: number, previewing: boolean) => ReactNode)falseWhether to write the score next to the icons. If true, writes the label if labels exist, otherwise the number (4.5); while hovering it shows the previewed score. If a function is given, the returned content is written (e.g. (v) => \${v} · 128 reviews\``).
clearable?booleantrueWhether pressing the selected score again clears it (0). On the keyboard Backspace/Delete/Home and the 0 key also clear. When off, at least step is given.
tone?Tone"warning"` (yellow star)Color of filled icons.
size?Size"md"Icon size.
readOnly?booleanDisplay only: draws a fractional score (4.3) at its exact ratio, isn't focusable, and is read to screen readers as "4.3 / 5". For product cards and reviews.
disabled?booleanDisabled.
name?stringForm field name; the score is submitted under this name (empty if no score).
form?stringId of the form the field belongs to (if outside the form).
required?booleanThe form can't be submitted without a score.
id?stringId of the focused element. RatingField passes it itself.
autoFocus?booleanFocuses when the page opens.
tooltip?TooltipPropTooltip shown when hovering the component. See TooltipProp.
className?stringClasses added to the container.
classNames?RatingClassNamesClasses added to the inner parts.
effect?EffectPropEffect or effects of each icon (e.g. "pulse", "bounce"). If omitted, UIProvider appearance.defaults.rating.effect. See BuiltinEffect.
aria-label?stringScreen reader name if there is no visible label (e.g. "Product rating").
aria-labelledby?stringId of the element naming the rating.
aria-describedby?stringId of the element describing the rating. RatingField passes it itself.
aria-invalid?boolean | "true" | "false"Show the field as invalid. RatingField passes it itself.