Directory
- Welcome
Rating Component
The Rating component provides an interactive star-based rating system with keyboard navigation, accessibility features, and form integration capabilities.
Key Features
-
Interactive Stars: Clickable star icons with hover effects
-
Keyboard Navigation: Full keyboard support with arrow keys
-
Form Integration: Works seamlessly with form submissions
-
Accessibility: ARIA support and screen reader compatibility
Basic Usage
Create a simple rating component:
<Rating />
Variants
Sizes
<Rating buttonProps={{ size: 'sm' }} />
With Label
<Rating label="Rate your experience" />
Required Rating
<Rating label="Product Rating" required name="product-rating"/>
Initial Rating Value
<Rating label="Pre-filled Rating" rating={3}/>
Props API
Prop | Type | Description |
---|---|---|
buttonProps | ComponentProps<typeof Button> | Props for star buttons |
label | string | Label text for the rating input |
labelProps | HTMLAttributesWithData<label> | Props for the label element |
name | string | Form field name |
rating | number | Initial rating value (1-5) |
required | boolean | Makes the rating required |
Rating Functions API
The rating component provides a set of functions to interact with the rating:
Function | Type | Description |
---|---|---|
set: rating | (rating: number) => void | Sets the rating value |
get: rating | () => number | Returns the current rating value |
reset | () => void | Resets the rating value to 0 |
Custom Event Listeners
Waiting for events to be emitted… (Click on the rating to see the events)
CustomEvent<{ id?: string; name?: string; rating: number;}>;
Form Integration
The Rating component works seamlessly with forms as it has a hidden input that is used to store the rating value:
<form> <Rating name="user-rating" required label="How would you rate this?" /> <button type="submit">Submit Rating</button></form>
Keyboard Navigation
The Rating component supports full keyboard interaction:
Key | Action |
---|---|
← / ↓ | Move to previous star |
→ / ↑ | Move to next star |
Space/Enter | Select current star rating |
Tab | Move focus to next interactive element |
Limited Time Launch Sale
Add interactive rating components to your website with our AstroJS starter template. Get started now!
GET 60% OFF!