Fragiola

API: components

Rating

A row of stars that shows and chooses a score, in whole or half steps, with a pointer preview, the APG rating radio group, as a form control.

A row of stars (or any marks) that shows and chooses a score from 1 to count, in whole or half steps: a product rating, a satisfaction score. The marks are the consumer's (an icon, a glyph); the primitive gives each one its state (checked, highlighted, half), the keys, the pointer preview and the form value. Base UI has no Rating: it follows the WAI-ARIA APG's rating radio group example of the radio group pattern: the Root is a radiogroup and each star a radio the consumer names ("1 star", "2 stars", ...). A choice over an arbitrary range is a Slider; a score nobody chooses, a Meter (a read-only Rating shows one too, but stays focusable).

Product rating
Gallery

Behaviour

  • Value: a number from 0 (no rating, the default) to count (5), in steps of 1, or of 0.5 with allowHalf. Reasons: item-press, keyboard, none (a form reset).
  • The stars: the consumer renders one Item per star, index 1 to count, usually from the Root's children function (({ items, value }) => ...; Vue: the default scoped slot; Angular: items() and value() through exportAs). An Item renders the consumer's mark, or a function of its state, so a mark can be drawn whole, half or empty.
  • The state of a star: checked (it holds the value: the value rounded up, so 2.5 checks star 3), highlighted (drawn filled, whole or half: up to the displayed value rounded up) and half (drawn half filled: the displayed value is index - 0.5). The displayed value is the hovered one during a preview, else the value.
  • Focus: one tab stop, the checked star, else the first one. Every arrow steps the value and moves the focus to the star that holds it (the APG radio group's arrows, over the value's steps), wrapping with loopFocus; Home and End go to the first step and count; no key reaches 0. Left and Right are swapped in RTL.
  • Pointer: a mouse or a pen over a star previews its value (touch has no preview); leaving the Root shows the value again. A press chooses the value under the pointer (with allowHalf, the star's inline-start half chooses index - 0.5); with allowClear, a press on the current value clears it to 0.
  • Read-only and disabled: readOnly keeps the stars focusable (aria-readonly on the Root) and nothing changes the score; disabled makes every star a disabled button, out of the tab order.
  • Names: every star is named by the consumer (aria-label, or a label in its content): the primitive has no text. A radio has no value text, so name the star that holds a half value by the value ("2.5 stars" on star 3).
  • Forms: the HiddenInput, a part the consumer renders inside the Root, is visually hidden and carries the name (only while the value is not 0), the value as text and required. A form reset returns an uncontrolled Root to defaultValue. Without it the Root has no form value and no native validation.
  • In a Field: the Root is the field's control (named, described, aria-invalid, the field's states), validate gets the value, and the HiddenInput is the validated element.
  • The mechanisms: the value math is the toolkit's /range (ratingKeyTarget, ratingValueAt, ratingItemState), beside the slider's.

Anatomy

partelementdata-primitives-partrole
Rootdivrating-rootthe radiogroup; holds the value
Itembuttonrating-itemone star: a radio
HiddenInputinputrating-hidden-inputwhat the form submits and validates (optional)

Usage

import { Rating } from "@fragiola/primitives-react/rating";

<span id="rating">Rating</span>
<Rating.Root aria-labelledby="rating" name="rating" allowHalf>
    {({ items, value }) => (
        <>
            {items.map((index) => (
                <Rating.Item key={index} index={index} aria-label={`${index} out of 5`}>
                    {(state) => (state.half ? "⯪" : state.highlighted ? "★" : "☆")}
                </Rating.Item>
            ))}
            <Rating.HiddenInput />
        </>
    )}
</Rating.Root>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:value, @value-change), and Angular as inputs and outputs ([(value)], (valueChangeDetails) with { value, details }).

Root

proptypedefaultdescription
valuenumberthe score, controlled; 0 for none
defaultValuenumber0the initial score, uncontrolled
onValueChange(value: number, details: { reason }) => voidevery change (item-press, keyboard, none)
countnumber5the number of stars
allowHalfbooleanfalsehalf steps: the arrows step by 0.5, a press on a star's inline-start half chooses index - 0.5
allowClearbooleanfalsea press that chooses the current value again clears it to 0
readOnlybooleanfalseshown and focusable; nothing changes it
disabledbooleanfalseevery star is disabled, out of the tab order
requiredbooleanfalsethe form's validation fails while the value is 0
namestringthe HiddenInput's name (a Field's wins)
loopFocusbooleantruethe arrows wrap from count to the first step and back

The children may be a function of { items, value }: the stars' indexes ([1, 2, ..., count]) and the value.

Item

proptypedefaultdescription
indexnumberrequired: the star's number, 1 to count; its whole value

The children may be a function of its state: index, checked, highlighted, half, disabled, readOnly (also its render and class functions').

HiddenInput

No props of its own.

State and part attributes

partattributes
Rootrole="radiogroup", aria-readonly="true" and data-readonly when read-only, aria-disabled="true" and data-disabled when disabled, aria-required="true" and data-required when required; in a Field, aria-labelledby, aria-describedby, aria-invalid and the field's states
Itemrole="radio", type="button", aria-checked ("true" on the checked star, "false" on the others), tabindex (0 on the tab stop, -1 on the others), disabled and data-disabled when disabled, data-readonly, data-checked / data-unchecked (exactly one), data-highlighted (drawn filled, whole or half), data-half (drawn half filled)
HiddenInputname (while the value is not 0), value (the value as text, "" for 0), required, disabled, aria-hidden="true", tabindex="-1", visually hidden

data-highlighted is a star's fill here (the displayed value), not a navigation highlight.

Keyboard

The step is 1, or 0.5 with allowHalf. A read-only or disabled Rating ignores every key but Tab.

keyaction
Tabenters at the checked star, else the first one; Tab again leaves it
ArrowRight, ArrowDownthe value one step up (from 0, the first step), wrapping from count (loopFocus) (ArrowLeft in RTL)
ArrowLeft, ArrowUpthe value one step down, wrapping from the first step to count (loopFocus) (ArrowRight in RTL)
Homethe first step (1, or 0.5)
Endcount
Spacechecks the focused star's whole value, when it is not checked
Enternothing (a native radio)

Exports

entryexports
@fragiola/primitives-react/ratingChangeDetails, ChangeReason, Rating, RatingItemProps, RatingItemState, RatingRootProps, RatingRootRenderState, RatingRootState, RenderProp
@fragiola/primitives-vue/ratingChangeDetails, ChangeReason, Rating, RatingItemProps, RatingItemSlots, RatingItemState, RatingRootEmits, RatingRootProps, RatingRootRenderState, RatingRootSlots, RatingRootState, RenderSlotProps
@fragiola/primitives-angular/ratingChangeDetails, ChangeReason, RatingHiddenInput, RatingItem, RatingItemState, RatingRoot, RatingRootState, ValueChangeEvent