General
Sortable
Usage#
import { WDS_SORTABLE } from '@wds/angular/sortable';wds-sortable-item elements in wds-sortable, each with a unique value. In plain HTML leave value on the list unset: it then moves the elements itself and wds-change carries the identifiers. In a framework bind value (the list of items) and set it again from wds-change, which fires live while dragging; item identifiers come from getItemValue (by default id). Lists of cards across several columns are in Kanban.
Examples#
Default#
wds-sortable-item-handle (a grip icon when empty) limits dragging to itself, so the rest of the item can hold links and buttons. A copy of the item follows the pointer while the original keeps its place at half opacity.
import { Component } from '@angular/core';
import { WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent } from '@wds/angular/sortable';
import { WdsItemComponent } from '@wds/angular/item';
import { WdsBadgeComponent } from '@wds/angular/badge';
@Component({
selector: 'app-sortable-default-demo',
imports: [WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent, WdsItemComponent, WdsBadgeComponent],
template: `
<wds-sortable class="w-full max-w-md">
<wds-sortable-item value="schema" label="Freeze the database schema">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Freeze the database schema" description="Backend · 2 days">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="secondary">High</wds-badge>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="copy" label="Write the onboarding copy">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Write the onboarding copy" description="Content · 1 day">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="outline">Medium</wds-badge>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="audit" label="Run the accessibility audit">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Run the accessibility audit" description="Design · 3 days">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="secondary">High</wds-badge>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="notes" label="Draft the release notes">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Draft the release notes" description="Product · half a day">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="outline">Low</wds-badge>
</wds-item>
</wds-sortable-item>
</wds-sortable>
`,
})
export class SortableDefaultDemo {}Horizontal#
orientation="horizontal" lays the items out in a row; the left and right arrow keys move them. Without a handle the whole item is the handle.
import { Component } from '@angular/core';
import { WdsSortableComponent, WdsSortableItemComponent } from '@wds/angular/sortable';
import { WdsItemComponent } from '@wds/angular/item';
@Component({
selector: 'app-sortable-horizontal-demo',
imports: [WdsSortableComponent, WdsSortableItemComponent, WdsItemComponent],
template: `
<wds-sortable orientation="horizontal">
<wds-sortable-item value="lint" label="Lint">
<wds-item variant="outline" size="sm" heading="Lint"></wds-item>
</wds-sortable-item>
<wds-sortable-item value="test" label="Unit tests">
<wds-item variant="outline" size="sm" heading="Unit tests"></wds-item>
</wds-sortable-item>
<wds-sortable-item value="build" label="Build">
<wds-item variant="outline" size="sm" heading="Build"></wds-item>
</wds-sortable-item>
<wds-sortable-item value="deploy" label="Deploy">
<wds-item variant="outline" size="sm" heading="Deploy"></wds-item>
</wds-sortable-item>
</wds-sortable>
`,
})
export class SortableHorizontalDemo {}Grid#
orientation="grid" fits as many columns as there is room for (--wds-sortable-item-min-width, here 10rem). A dragged tile takes the place of the tile nearest the pointer; from the keyboard the up and down arrows move it by a row.
import { Component } from '@angular/core';
import { WdsSortableComponent, WdsSortableItemComponent } from '@wds/angular/sortable';
import { WdsCardComponent } from '@wds/angular/card';
@Component({
selector: 'app-sortable-grid-demo',
imports: [WdsSortableComponent, WdsSortableItemComponent, WdsCardComponent],
template: `
<wds-sortable orientation="grid" class="w-full max-w-2xl [--wds-sortable-gap:12px] [--wds-sortable-item-min-width:10rem]">
<wds-sortable-item value="revenue" label="Revenue">
<wds-card size="sm" class="h-full">
<div class="flex flex-col gap-1">
<span class="text-xs text-muted-foreground">Revenue</span>
<span class="text-2xl font-semibold tabular-nums">$48.2k</span>
<span class="text-xs text-muted-foreground">+12% this month</span>
</div>
</wds-card>
</wds-sortable-item>
<wds-sortable-item value="users" label="Active users">
<wds-card size="sm" class="h-full">
<div class="flex flex-col gap-1">
<span class="text-xs text-muted-foreground">Active users</span>
<span class="text-2xl font-semibold tabular-nums">2,184</span>
<span class="text-xs text-muted-foreground">+5% this week</span>
</div>
</wds-card>
</wds-sortable-item>
<wds-sortable-item value="orders" label="Orders">
<wds-card size="sm" class="h-full">
<div class="flex flex-col gap-1">
<span class="text-xs text-muted-foreground">Orders</span>
<span class="text-2xl font-semibold tabular-nums">631</span>
<span class="text-xs text-muted-foreground">−3% this week</span>
</div>
</wds-card>
</wds-sortable-item>
<wds-sortable-item value="refunds" label="Refund rate">
<wds-card size="sm" class="h-full">
<div class="flex flex-col gap-1">
<span class="text-xs text-muted-foreground">Refund rate</span>
<span class="text-2xl font-semibold tabular-nums">1.4%</span>
<span class="text-xs text-muted-foreground">Stable</span>
</div>
</wds-card>
</wds-sortable-item>
<wds-sortable-item value="tickets" label="Open tickets">
<wds-card size="sm" class="h-full">
<div class="flex flex-col gap-1">
<span class="text-xs text-muted-foreground">Open tickets</span>
<span class="text-2xl font-semibold tabular-nums">27</span>
<span class="text-xs text-muted-foreground">9 overdue</span>
</div>
</wds-card>
</wds-sortable-item>
<wds-sortable-item value="uptime" label="Uptime">
<wds-card size="sm" class="h-full">
<div class="flex flex-col gap-1">
<span class="text-xs text-muted-foreground">Uptime</span>
<span class="text-2xl font-semibold tabular-nums">99.98%</span>
<span class="text-xs text-muted-foreground">Last 30 days</span>
</div>
</wds-card>
</wds-sortable-item>
</wds-sortable>
`,
})
export class SortableGridDemo {}Placeholder overlay#
overlay="placeholder" drags a dashed box of the item’s size instead of its copy.
import { Component } from '@angular/core';
import { WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent } from '@wds/angular/sortable';
import { WdsItemComponent } from '@wds/angular/item';
import { WdsBadgeComponent } from '@wds/angular/badge';
@Component({
selector: 'app-sortable-placeholder-demo',
imports: [WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent, WdsItemComponent, WdsBadgeComponent],
template: `
<wds-sortable overlay="placeholder" class="w-full max-w-md">
<wds-sortable-item value="schema" label="Freeze the database schema">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Freeze the database schema" description="Backend · 2 days">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="secondary">High</wds-badge>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="copy" label="Write the onboarding copy">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Write the onboarding copy" description="Content · 1 day">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="outline">Medium</wds-badge>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="audit" label="Run the accessibility audit">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Run the accessibility audit" description="Design · 3 days">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="secondary">High</wds-badge>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="notes" label="Draft the release notes">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Draft the release notes" description="Product · half a day">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="outline">Low</wds-badge>
</wds-item>
</wds-sortable-item>
</wds-sortable>
`,
})
export class SortablePlaceholderDemo {}Disabled item#
disabled on an item keeps it from being dragged, but other items can still move past it. disabled on wds-sortable locks the whole list.
import { Component } from '@angular/core';
import { WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent } from '@wds/angular/sortable';
import { WdsItemComponent } from '@wds/angular/item';
import { WdsBadgeComponent } from '@wds/angular/badge';
@Component({
selector: 'app-sortable-disabled-demo',
imports: [WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent, WdsItemComponent, WdsBadgeComponent],
template: `
<wds-sortable class="w-full max-w-md">
<wds-sortable-item value="inbox" label="Inbox" disabled>
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Inbox" description="Always first">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
<wds-badge slot="actions" variant="secondary">Pinned</wds-badge>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="design" label="Design">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Design" description="14 open threads">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="hiring" label="Hiring">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Hiring" description="3 open threads">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
</wds-item>
</wds-sortable-item>
<wds-sortable-item value="finance" label="Finance">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" heading="Finance" description="8 open threads">
<wds-sortable-item-handle slot="media"></wds-sortable-item-handle>
</wds-item>
</wds-sortable-item>
</wds-sortable>
`,
})
export class SortableDisabledDemo {}Persist to a backend#
wds-commit fires once per finished drag (never during the live preview) with the new order and previousValue before it. Here the save fails about once in four tries and the list goes back to previousValue.
import { Component, signal } from '@angular/core';
import { WdsItemComponent } from '@wds/angular/item';
import { WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent } from '@wds/angular/sortable';
import { toast } from '@wds/angular/toast';
import type { WdsSortableCommitDetail } from '@wds/core/sortable';
interface Track {
id: string;
title: string;
artist: string;
length: string;
}
const TRACKS: Track[] = [
{ id: 't1', title: 'Harbor Lights', artist: 'The Low Tides', length: '3:42' },
{ id: 't2', title: 'Paper Planes at Dawn', artist: 'Mira Okafor', length: '4:05' },
{ id: 't3', title: 'Glasshouse', artist: 'North Avenue', length: '2:58' },
{ id: 't4', title: 'Slow Orbit', artist: 'Kestrel', length: '5:11' },
{ id: 't5', title: 'Late Train Home', artist: 'Juniper & Ash', length: '3:27' },
];
/** Pretends to save the order: fails about once in four tries. */
function saveOrder(): Promise<void> {
return new Promise((resolve, reject) => setTimeout(() => (Math.random() < 0.25 ? reject(new Error('Network error')) : resolve()), 700));
}
@Component({
selector: 'app-sortable-persisted-demo',
host: { class: 'block w-full max-w-md' },
imports: [WdsItemComponent, WdsSortableComponent, WdsSortableItemComponent, WdsSortableItemHandleComponent],
template: `
<wds-sortable [value]="tracks()" (wdsChange)="tracks.set($event.value)" (wdsCommit)="save($event)">
@for (track of tracks(); track track.id; let i = $index) {
<wds-sortable-item [value]="track.id" [label]="track.title">
<wds-item variant="outline" size="sm" class="[&::part(media)]:self-center" [heading]="i + 1 + '. ' + track.title" [description]="track.artist">
<wds-sortable-item-handle slot="media" />
<span slot="actions" class="text-xs tabular-nums text-muted-foreground">{{ track.length }}</span>
</wds-item>
</wds-sortable-item>
}
</wds-sortable>
`,
})
export class SortablePersistedDemo {
protected readonly tracks = signal(TRACKS);
/** Fires once per finished drag; the list already shows the move. */
protected save(commit: WdsSortableCommitDetail<Track>): void {
toast.promise(saveOrder(), {
loading: 'Saving the playlist…',
success: `Moved to position ${commit.overIndex + 1}`,
error: () => {
// Roll back to the order before the drag.
this.tracks.set(commit.previousValue);
return 'Could not save. The order was restored.';
},
});
}
}API References#
wds-sortable#
wds-sortable-item;
a wds-sortable-item-handle inside it limits dragging to the handle, otherwise the whole item is the handle.
With value set (a property: the list of items), the list does not move elements itself: it fires wds-change
with the new order, live while dragging, and you render it again. Without value it moves the elements and
wds-change carries the identifiers. wds-commit fires once per finished drag, with the order before the drag,
so a move can be saved and rolled back.
The mouse starts a drag after 10px of movement, touch after holding for 250ms (moving earlier scrolls the page).
From the keyboard Space or Enter picks the focused item up, the arrow keys move it, Space or Enter drops it and
Escape cancels; each step is announced to screen readers.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| orientation | WdsSortableOrientation | 'vertical' | vertical (a column), horizontal (a row) or grid (as many columns as fit). Also sets which arrow keys move an item. |
| overlay | WdsSortableOverlay | 'clone' | |
| restore-on-cancel.restoreOnCancel | boolean | false | |
| disabled | boolean | false |
Properties
| Property | Description |
|---|---|
| valueT[] | undefined | wds-change. Without it the list reads and moves the elements. |
| getItemValue(item: T) => string | value of its wds-sortable-item). Defaults to id, then value, then the item itself. |
Events
| Event | Description |
|---|---|
| wds-changeCustomEvent<WdsSortableChangeDetail> | restore-on-cancel. |
| wds-commitCustomEvent<WdsSortableCommitDetail> | |
| wds-drag-startCustomEvent<WdsSortableDragDetail> | |
| wds-drag-endCustomEvent<WdsSortableDragDetail> | |
| wds-drag-cancelCustomEvent<WdsSortableDragDetail> |
Slots
| Slot | Description |
|---|---|
| (default) | wds-sortable-item elements. |
CSS custom properties
| Property | Description |
|---|---|
| --wds-sortable-gap | |
| --wds-sortable-item-min-width |
wds-sortable-item#
wds-sortable. Without a wds-sortable-item-handle inside, the whole item is the handle:
it is a button in the tab order, dragged with the pointer or picked up with Space or Enter.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| value | string | '' | getItemValue returns for it. |
| label | string | '' | value). |
| disabled | boolean | false |
Slots
| Slot | Description |
|---|---|
| (default) |
wds-sortable-item-handle#
wds-sortable-item that drags it. With a handle inside, only the handle starts a drag and takes
keyboard focus; the rest of the item can hold links, inputs and buttons. Empty, it shows a grip icon; it can
also wrap other content.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| label | string | '' |
Slots
| Slot | Description |
|---|---|
| (default) |
Accessibility#
Screen reader#
wds-sortable-item-handle) is a button in the tab order, with aria-description explaining the keys. A picked-up item has aria-pressed="true".
label on the handle overrides it.
label so the announcements use their names instead of their identifiers. The copy that follows the pointer is aria-hidden.
Keyboard support#
| Key | Function |
|---|---|
restore-on-cancel the items go back to the order before it. |