Simple list
Drag the items to reorder them. The list reorders live as you drag, the other items slide out of the way, and
your own IList<string> is changed in place through @bind-Items - the order
under the list is read straight from it.
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
To pick up the item, press Space or Enter. While it is picked up, the arrow keys move it, Home and End move it to the start or the end of its list, Space or Enter drops it, and Escape puts it back.
Order: Item 1, Item 2, Item 3, Item 4, Item 5, Item 6
Try it
- Drag Item 1 down past Item 3. It moves in the list as soon as the pointer enters the next item, the others slide out of its way, and Order changes when you drop it.
-
Move the
Animationslider to1000to watch the slide in slow motion, or to0to turn it off. A move that starts while a slide is still running picks it up from where the item is, so nothing jumps. - Without a mouse: Tab into the list, pick the focused item up with Space, move it with ↑ ↓, and drop it with Space - or put it back with Escape. See keyboard and screen readers.
- On a touch screen, rest a finger on an item for a moment to pick it up; a quick swipe still scrolls the page. See touch and pen.
How it works
-
Every item is wrapped in a
<div>that carries the drag handlers;ItemTemplaterenders what goes inside it. -
The wrappers are rendered with
@key, so when the item moves in your list Blazor moves its existing DOM node instead of recreating it - which keeps the browser's drag alive while the list reorders under it. That is also why the items of a list must be unique: a list of strings must not hold the same string twice. -
The bound list must be mutable - a
List<T>, or anything else whoseIsReadOnlyisfalse. Declare the field asIList<TItem>, the parameter's own type, or@bind-Itemsdoes not compile. ItemsChangedis raised once per drag, after the drop, and only when the order actually changed - a drag that ends where it started raises nothing.- Before each change the list records where every item is, lets Blazor update the DOM, then slides each item from its old place to its new one (the FLIP technique). With the system's reduced-motion setting on, nothing slides.
Parameters on this page
| Parameter | Here | What it does |
|---|---|---|
@bind-Items | _items | The items to show and reorder. The list is changed in place, and ItemsChanged tells your field. |
TItem | string | The item type. Any type works; the items are passed to ItemTemplate and to every event, typed. |
ItemTemplate | @item | What each item shows, inside its wrapper. |
Id | simple-list | The list's DOM id, and the FromId / ToId its events report. Generated when not set. |
Class, ItemClass | demo-list, demo-item | The CSS classes of the list container and of every item wrapper. The look of the list is yours. |
GhostClass | demo-ghost | Replaces bit-bortable-ghost, the class of the item being dragged: a dashed card here, instead of the stylesheet's dimmed one. |
Animation | the slider | How long, in milliseconds, items take to slide into place; 150 by default, 0 turns it off. |