Accessible star rating component for reviews, feedback, and filtering
Add the CSS and JavaScript files, then create a star rating with a single data attribute:
<link rel="stylesheet" href="styles/star-rating.css">
<script src="javascript/star-rating.js"></script>
<!-- Interactive Rating -->
<div data-rsl-star-rating data-star-value="3.5"></div>
<!-- Read-only Display -->
<div data-rsl-star-rating data-star-value="4.2" data-star-readonly="true"></div>
<!-- With Form Integration -->
<div data-rsl-star-rating
data-star-name="review_rating"
data-star-required="true"
data-star-label="Rate your experience"></div>
Navigate with arrow keys, Home/End for min/max, Delete to clear. WCAG 2.1 AA compliant.
Uses role="slider" with aria-valuenow/text. Live region announces value changes.
Support for 0.5 increments. Click left or right half of a star to set half values.
Built-in support for cross-component filtering via data-filterbus-field.
Stars, hearts, or thumbs. Each with appropriate color schemes.
CSS variables for all colors. Custom colors via data attributes.
| Attribute | Type | Default | Description |
|---|---|---|---|
data-rsl-star-rating |
Flag | — | Required. Initializes the star rating component. |
data-star-value |
Number | 0 | Initial rating value (0 to max). |
data-star-max |
Number | 5 | Maximum number of stars. |
data-star-half |
Boolean | true | Allow half-star increments. |
data-star-readonly |
Boolean | false | Display-only mode, no interaction. |
data-star-size |
String | medium | Size: small, medium, large, or xl. |
data-star-icon |
String | star | Icon type: star, heart, or thumb. |
data-star-color |
Color | #ffc107 | Custom filled star color. |
data-star-empty-color |
Color | #d0d0d0 | Custom empty star color. |
data-star-show-value |
Boolean | false | Show numeric value next to stars. |
data-star-clearable |
Boolean | false | Show clear button to reset rating. |
data-star-name |
String | — | Form field name for submission. |
data-star-required |
Boolean | false | Mark as required for form validation. |
data-star-label |
String | — | Accessible label text. |
data-filterbus-field |
String | — | FilterBus field name for cross-component filtering. |
| Method | Arguments | Returns | Description |
|---|---|---|---|
RSL.StarRating.init() |
— | void | Initialize all star rating components on the page. |
RSL.StarRating.create(el) |
HTMLElement | HTMLElement | Initialize a specific star rating element. |
RSL.StarRating.getValue(el) |
HTMLElement | Number | Get the current rating value. |
RSL.StarRating.setValue(el, value) |
HTMLElement, Number | void | Set the rating value programmatically. |
RSL.StarRating.clear(el) |
HTMLElement | void | Clear the rating (set to 0). |
RSL.StarRating.destroy(el) |
HTMLElement | void | Remove star rating and clean up. |
RSL.StarRating.createFilter(container, options) |
HTMLElement, Object | HTMLElement | Create a filter-mode star rating for FilterBus. |
// Get/Set values
const rating = document.querySelector('[data-rsl-star-rating]');
const value = RSL.StarRating.getValue(rating);
RSL.StarRating.setValue(rating, 4.5);
// Listen for changes
rating.addEventListener('rsl-star-change', function(e) {
console.log('New rating:', e.detail.value);
console.log('Previous rating:', e.detail.oldValue);
});
// Clear rating
RSL.StarRating.clear(rating);
| Event | Detail Properties | Description |
|---|---|---|
rsl-star-change |
{ value, oldValue, element } |
Fired when the rating value changes. |
rsl-star-clear |
{ oldValue, element } |
Fired when the rating is cleared. |
rsl-star-filter-change |
{ value, field } |
Fired in filter mode when selection changes. |
| Key | Action |
|---|---|
Arrow Right / Arrow Up |
Increase rating by one step (0.5 or 1) |
Arrow Left / Arrow Down |
Decrease rating by one step |
Home |
Set rating to minimum (0) |
End |
Set rating to maximum |
Delete / Backspace |
Clear rating (if clearable enabled) |
role="slider" with proper ARIA attributes for screen readersaria-live="polite" regionrole="img" with descriptive label| Variable | Default | Description |
|---|---|---|
--rsl-star-filled |
#ffc107 | Color for filled stars |
--rsl-star-empty |
#d0d0d0 | Color for empty stars |
--rsl-star-hover |
#ffca28 | Color on hover preview |
--rsl-star-focus |
#1976d2 | Focus outline color |
--rsl-star-size-sm |
16px | Small star size |
--rsl-star-size-md |
24px | Medium star size |
--rsl-star-size-lg |
32px | Large star size |
--rsl-star-size-xl |
48px | Extra large star size |
Star Rating integrates with RSL's FilterBus as a publisher component. In filter mode, it publishes rating selections that other components (Smart Table, Cards, Gallery, etc.) can subscribe to for filtering.
data-mode="filter" and data-filterbus-field="fieldName"<!-- Star Rating Filter (publishes to FilterBus) -->
<div id="rating-filter"></div>
<script>
// Create filter-mode star rating
RSL.StarRating.createFilter(document.getElementById('rating-filter'), {
field: 'minRating', // FilterBus field name
label: 'Minimum Rating'
});
// Other components subscribe to 'minRating' field
// e.g., Smart Table filters rows where rating >= selected value
</script>
<!-- Products table subscribes to rating filter -->
<table data-rsl-smart-table data-filter-subscribe="minRating">
<!-- Rows with data-rating attribute will be filtered -->
</table>
| Attribute | Type | Description |
|---|---|---|
data-mode="filter" |
String | Enables filter mode with rating options (All, 4+, 3+, 2+, 1+) |
data-filterbus-field |
String | FilterBus field name to publish to (e.g., "minRating", "rating") |
Watch how Star Rating Filter works with other components. This live demo shows Filter + Smart Table + KPI Cards + Charts all working together.
View Live Dashboard Demo