Interactive
Rating input
A star rating with half stars, drawn as a WAI-ARIA slider over a number input that keeps the value. Hover previews, click sets, clicking the same value clears, and arrows, Home, End and digits work. Needs monsoon.js.
Needs monsoon.js. The styles work on their own, and the behavior comes from the optional interactive layer: add <script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script> or import 'monsooncss/js'. See the interactive layer.
Example
<div class="grid gap-3">
<div class="rating-input" data-m-rating-input>
<input type="number" class="input max-w-24" name="score" min="0" max="5" step="0.5" value="3.5" aria-label="Your rating">
</div>
<div class="rating-input" data-m-rating-input>
<input type="number" class="input max-w-24" min="0" max="10" step="1" value="7" aria-label="Score out of 10">
</div>
</div>JavaScript API
Attribute
data-m-rating-input
Options
| Option | Description |
|---|---|
max on the input sets the number of stars, step the precision (0.5 for halves) |
Methods
| Method | Description |
|---|---|
Monsoon.ratingInput(el).value | |
Monsoon.ratingInput(el).value = 4.5 |
Events
| Event | Description |
|---|---|
monsoon:change { value } | |
input and change on the native input |
Classes
4 classes. Click a name to copy it. Utilities on the same element always win over component defaults, so any of them can be adjusted with plain utility classes.
| Class | Type | What it does |
|---|---|---|
| Base | Base class of the rating input. On a <div> in the example. Sets inline-flex display, alignment and gap, and --tone and --_size.CSS |
|
| Modifier | Modifier, combined with the rating-input class. Not used in the example above. Sets inline-flex display, gap, corner radius, pointer cursor, border and not-allowed cursor, and --tone.CSS |
|
| Modifier | Modifier, combined with the rating-input class. Not used in the example above. Sets width, height, background, transition and transform, and --tone.CSS |
|
| Modifier | Modifier, combined with the rating-input class. Not used in the example above. Sets width, text size, font weight and text color.CSS |
Usage
The styles ship inside the same stylesheet as the utilities, and the behavior comes from the optional monsoon.js script, which enhances any element with a matching data-m attribute, including markup added later.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/monsooncss">
<script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script>
<!-- Rating input: start from the base class, then add modifiers -->
<div class="grid gap-3">
<div class="rating-input" data-m-rating-input>
<input type="number" class="input max-w-24" name="score" min="0" max="5" step="0.5" value="3.5" aria-label="Your rating">
</div>
<div class="rating-input" data-m-rating-input>
<input type="number" class="input max-w-24" min="0" max="10" step="1" value="7" aria-label="Score out of 10">
</div>
</div>Theme it with --seed on any ancestor, for example style="--seed: #0d9488", and force a scheme with class="dark" or class="light" on a wrapper.
Accessibility
- Give every control a visible
labellinked withfor, or anaria-labelwhen the purpose is obvious from context. Link help and error text witharia-describedby; invalid styles use:user-invalid, so they appear only after the user interacts. - Every interactive part shows a visible focus ring with
:focus-visible, and tone colors pair with-foregroundtokens that keep text at 4.5:1 or better in light and dark mode.