Star Rating

Accessible star rating component for reviews, feedback, and filtering

Quick Start

2-Minute Setup

Add the CSS and JavaScript files, then create a star rating with a single data attribute:

Include Files • HTML
<link rel="stylesheet" href="styles/star-rating.css">
<script src="javascript/star-rating.js"></script>
Basic Usage • HTML
<!-- 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>

Live Demo

Interactive:
Read-only:

Features

Full Keyboard Support

Navigate with arrow keys, Home/End for min/max, Delete to clear. WCAG 2.1 AA compliant.

Screen Reader Ready

Uses role="slider" with aria-valuenow/text. Live region announces value changes.

Half-Star Precision

Support for 0.5 increments. Click left or right half of a star to set half values.

FilterBus Integration

Built-in support for cross-component filtering via data-filterbus-field.

Multiple Icons

Stars, hearts, or thumbs. Each with appropriate color schemes.

Fully Themeable

CSS variables for all colors. Custom colors via data attributes.

Data Attributes API

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.

JavaScript API

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.
Programmatic Usage • JS
// 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);

Events

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.

Keyboard Navigation

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)

Accessibility Notes

  • Uses role="slider" with proper ARIA attributes for screen readers
  • Announces value changes via aria-live="polite" region
  • Visible focus indicator meets WCAG 2.1 requirements
  • Read-only mode uses role="img" with descriptive label
  • Color is never the only means of conveying information (numeric display available)

CSS Variables

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

FilterBus Integration

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.

How It Works

  • Create a Star Rating with data-mode="filter" and data-filterbus-field="fieldName"
  • When user selects a rating, it publishes to FilterBus on the specified field
  • Other components subscribed to that field filter their content accordingly
  • Common use case: Filter products by minimum star rating

Example

Star Rating Filter with FilterBus • HTML + JS
<!-- 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>

FilterBus Attributes

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")

See FilterBus in Action

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
Back to Components View Examples