Interactive
Sortable list
Reorder with a pointer or the keyboard: Space picks an item up, the arrows move it, Space drops it and Escape puts it back. Siblings slide out of the way and the list scrolls near its edges. 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
<ul class="sortable max-w-sm" data-m-sortable aria-label="Playlist">
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Overture</span><span class="text-xs text-muted-foreground">2:14</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Night drive</span><span class="text-xs text-muted-foreground">3:41</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Monsoon season</span><span class="text-xs text-muted-foreground">4:05</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Glass harbor</span><span class="text-xs text-muted-foreground">2:58</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Coda</span><span class="text-xs text-muted-foreground">1:47</span></li>
</ul>JavaScript API
Attribute
data-m-sortable
Options
| Option | Description |
|---|---|
data-m-sortable-group="board" (lists that trade items) | |
data-m-sortable-axis="x" (a horizontal list) | |
a .sortable-handle inside items limits the pointer drag to it |
Methods
| Method | Description |
|---|---|
Monsoon.sortable(el).items | |
Monsoon.sortable(el).move(item, index, toList) |
Events
| Event | Description |
|---|---|
monsoon:sort { item, from, to, oldIndex, newIndex } (preventDefault undoes it) |
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 sortable list. On an <ul> in the example. Sets grid display, alignment, gap, margin, padding and auto cursor.CSS |
|
| Part | Part of the sortable list. On a <li> in the example. Sets flex display, alignment, gap, padding, text size and text color.CSS |
|
| Part | Part of the sortable list. On a <span> in the example. Sets alignment, width, height, margin, background and grab cursor.CSS |
|
| Modifier | Modifier, combined with the sortable class. Not used in the example above. Sets fixed position, offsets, stacking order, margin, overflow and shadow.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>
<!-- Sortable list: start from the base class, then add modifiers -->
<ul class="sortable max-w-sm" data-m-sortable aria-label="Playlist">
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Overture</span><span class="text-xs text-muted-foreground">2:14</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Night drive</span><span class="text-xs text-muted-foreground">3:41</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Monsoon season</span><span class="text-xs text-muted-foreground">4:05</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Glass harbor</span><span class="text-xs text-muted-foreground">2:58</span></li>
<li class="sortable-item"><span class="sortable-handle" aria-hidden="true"></span><span class="flex-1">Coda</span><span class="text-xs text-muted-foreground">1:47</span></li>
</ul>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
- 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.