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).
Behaviour
- Value: a number from
0(no rating, the default) tocount(5), in steps of 1, or of 0.5 withallowHalf. Reasons:item-press,keyboard,none(a form reset). - The stars: the consumer renders one Item per star,
index1 tocount, usually from the Root's children function (({ items, value }) => ...; Vue: the default scoped slot; Angular:items()andvalue()throughexportAs). 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, so2.5checks star 3),highlighted(drawn filled, whole or half: up to the displayed value rounded up) andhalf(drawn half filled: the displayed value isindex - 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 andcount; no key reaches0. 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 choosesindex - 0.5); withallowClear, a press on the current value clears it to0. - Read-only and disabled:
readOnlykeeps the stars focusable (aria-readonlyon the Root) and nothing changes the score;disabledmakes 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 not0), the value as text andrequired. A form reset returns an uncontrolled Root todefaultValue. 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),validategets 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
| part | element | data-primitives-part | role |
|---|---|---|---|
| Root | div | rating-root | the radiogroup; holds the value |
| Item | button | rating-item | one star: a radio |
| HiddenInput | input | rating-hidden-input | what 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
| prop | type | default | description |
|---|---|---|---|
value | number | the score, controlled; 0 for none | |
defaultValue | number | 0 | the initial score, uncontrolled |
onValueChange | (value: number, details: { reason }) => void | every change (item-press, keyboard, none) | |
count | number | 5 | the number of stars |
allowHalf | boolean | false | half steps: the arrows step by 0.5, a press on a star's inline-start half chooses index - 0.5 |
allowClear | boolean | false | a press that chooses the current value again clears it to 0 |
readOnly | boolean | false | shown and focusable; nothing changes it |
disabled | boolean | false | every star is disabled, out of the tab order |
required | boolean | false | the form's validation fails while the value is 0 |
name | string | the HiddenInput's name (a Field's wins) | |
loopFocus | boolean | true | the 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
| prop | type | default | description |
|---|---|---|---|
index | number | required: 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
| part | attributes |
|---|---|
| Root | role="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 |
| Item | role="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) |
| HiddenInput | name (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.
| key | action |
|---|---|
| Tab | enters at the checked star, else the first one; Tab again leaves it |
| ArrowRight, ArrowDown | the value one step up (from 0, the first step), wrapping from count (loopFocus) (ArrowLeft in RTL) |
| ArrowLeft, ArrowUp | the value one step down, wrapping from the first step to count (loopFocus) (ArrowRight in RTL) |
| Home | the first step (1, or 0.5) |
| End | count |
| Space | checks the focused star's whole value, when it is not checked |
| Enter | nothing (a native radio) |
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/rating | ChangeDetails, ChangeReason, Rating, RatingItemProps, RatingItemState, RatingRootProps, RatingRootRenderState, RatingRootState, RenderProp |
@fragiola/primitives-vue/rating | ChangeDetails, ChangeReason, Rating, RatingItemProps, RatingItemSlots, RatingItemState, RatingRootEmits, RatingRootProps, RatingRootRenderState, RatingRootSlots, RatingRootState, RenderSlotProps |
@fragiola/primitives-angular/rating | ChangeDetails, ChangeReason, RatingHiddenInput, RatingItem, RatingItemState, RatingRoot, RatingRootState, ValueChangeEvent |