A row of 5 stars. Each star is outlined until a rating is applied, click a star to set the value, hovering previews the fill without changing the value.
<k-rating></k-rating>
Set the value attribute to fill in stars up front. Stars 1 through value render filled, the rest stay outlined.
<k-rating value="3"></k-rating>
Read the current value from the value property, or listen for the change event to react as the user rates. Set value directly to apply or clear a rating programmatically — this also fires change.
<k-rating id="movieRating"></k-rating>
<p>You rated: <span id="output">0</span> / 5</p>
<button id="setFive">Set 5</button>
<button id="clearRating">Clear</button>
<script>
const $rating = document.getElementById('movieRating');
const $output = document.getElementById('output');
$rating.addEventListener('change', (event) => {
$output.textContent = event.detail.value;
});
document.getElementById('setFive').addEventListener('click', () => {
$rating.value = 5;
});
document.getElementById('clearRating').addEventListener('click', () => {
$rating.value = 0;
});
</script>
You rated: 0 / 5
Rating is form-associated. Set a name attribute and its value submits along with the rest of the form.
<form id="reviewForm">
<label>Rate this product:</label>
<k-rating name="rating" value="3"></k-rating>
<div class="mt">
<button type="submit" class="mr">Submit</button>
<button type="reset">Reset</button>
</div>
</form>
<p>Submitted: <span id="result"></span></p>
<script>
document.getElementById('reviewForm').addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(e.target);
document.getElementById('result').textContent = formData.get('rating');
});
</script>
Submitted:
The disabled attribute prevents the user from changing the value while still displaying it. The stars appear at 50% opacity and clicks have no effect.
<k-rating disabled value="4"></k-rating>
Override the --star_size, --star_color, --star_color__filled, and --star_gap custom properties to restyle the widget.
<k-rating
value="4"
style="
--star_size: 2.5rem;
--star_color: var(--c_border);
--star_color__filled: var(--c_danger);
--star_gap: 0.5rem;
"
></k-rating>
new Rating();
The rating component is a custom element and does not require any external dependencies beyond the Kempo-UI library. It renders 5 <k-icon> elements internally using the bundled star and star_filled icons.
| Property | Type | Default | Description |
|---|---|---|---|
value |
Number | 0 |
The applied rating, from 0 (no stars filled) to 5. Stars 1 through value render filled, the rest render outlined. Syncs to the value attribute. |
name |
String | '' |
The name attribute for form submission. When used in a form, this is the key under which the value is submitted. |
disabled |
Boolean | false |
When true, the stars cannot be changed by user interaction. The current value is still displayed at 50% opacity. Syncs to the disabled attribute. |
| Event | Detail | Description |
|---|---|---|
change |
{ value: Number } |
Fired whenever the value changes, whether from a click or set programmatically. The detail contains the new value. |