Rating
A star rating, interactive or read-only.
Example
<div class="stack"> <focsd-rating label="Rate this template" value="3"></focsd-rating> <focsd-rating label="Average rating" value="4" readonly></focsd-rating></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-rating.js';</script>
<focsd-rating label="Rate this template" value="3"></focsd-rating>import { Rating } from '@focsd-dev/react';
export function Example() { return <Rating onChange={(e) => console.log(e.target)} label="Rate this template" value="3"></Rating>;}<script setup>import '@focsd-dev/components/focsd-rating.js';</script>
<template> <focsd-rating @change="onChange" label="Rate this template" value="3"></focsd-rating></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-rating.js';
// template<focsd-rating (change)="onChange($event)" label="Rate this template" value="3"></focsd-rating><script> import '@focsd-dev/components/focsd-rating.js';</script>
<focsd-rating onchange={(e) => console.log(e.target)} label="Rate this template" value="3"></focsd-rating>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | number | 0 |
max | max | number | 5 |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired when the user commits a change to the value or state. |
CSS parts
Style the inside of the component with ::part(), for example focsd-rating::part(layout).
| Part | Description |
|---|---|
layout | The wrapper that places the label, the body and the messages. |
body | The stars, with the hint and message below them. |
filled | Added to each star at or below the value. |
label | The label element. |
star | One star. |
stars | The row of stars. |
hint | The author's hint. |
message | The shown error, warning or success message. |
message-icon | The icon in the message. |
announcer | The visually hidden live region. |
Accessibility
- Interactive: a
role="radiogroup"of star radios named "1 star", "2 stars"…, one tab stop. - Read-only:
role="img"named by the label and value ("Average rating: 4 out of 5"). - Form-associated: participates in
FormData, validation, reset and fieldsetdisabledlike a native control.
Keyboard
| Key | Action |
|---|---|
| Arrow keys | Change the rating (wraps around) |
| Home / End | One star / the maximum |
| Space | Choose the focused star |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.