VDT.Core.Blazor.DragAndDropList
1.1.0
dotnet add package VDT.Core.Blazor.DragAndDropList --version 1.1.0
NuGet\Install-Package VDT.Core.Blazor.DragAndDropList -Version 1.1.0
<PackageReference Include="VDT.Core.Blazor.DragAndDropList" Version="1.1.0" />
paket add VDT.Core.Blazor.DragAndDropList --version 1.1.0
#r "nuget: VDT.Core.Blazor.DragAndDropList, 1.1.0"
// Install VDT.Core.Blazor.DragAndDropList as a Cake Addin #addin nuget:?package=VDT.Core.Blazor.DragAndDropList&version=1.1.0 // Install VDT.Core.Blazor.DragAndDropList as a Cake Tool #tool nuget:?package=VDT.Core.Blazor.DragAndDropList&version=1.1.0
VDT.Core.Blazor.DragAndDropList
Blazor component that allows users to reorder items in a list by dragging and dropping.
Features
- Drag and drop items in a list to change the order
- Touch and mouse support
- Fully customizable item layout template
Usage
To start using this component, follow the below steps.
- Assign your list of items to the
Items
property - Inside the item template, there needs to be an element that has an
@onmousedown
and@ontouchstart
event callback tocontext.StartDragging
, passing theMouseEventArgs
andTouchEventArgs
, to start the dragging action - When the dragging item is dropped, the component
OnDropItem
event is triggered which provides an object of typeDropItemEventArgs<TItem>
; subscribe to this event to handle the reordering of your list
Please note that reordering must be done in client code since the component should not change its input property Items
. To handle reordering you can either
perform manual switching using the Item
, OriginalItemIndex
, NewItemIndex
and IndexDelta
properties of the event arguments parameter, or use the
IList<TItem>.Reorder
extension method. If a reordering action needs to be reverted, you can use the IList<TItem>.RevertOrder
extension method.
If you need to know when a drag and drop action is initiated, you can also subscribe to the OnDragItem
event, which provides an object of type
DragItemEventArgs<TItem>
with Item
and OriginalItemIndex
properties.
Example
<DragAndDropList TItem="Item" Items="Items" OnDropItem="ItemDropped">
<ItemTemplate>
<div class="mt-3 p-3 bg-light border rounded d-flex justify-content-between align-items-center">
<div class="overflow-hidden">
<h5 class="text-truncate">@context.Item.Text</h5>
<div class="text-muted text-truncate">@context.Item.Id</div>
</div>
<div>
<button class="btn btn-primary" @onmousedown="context.StartDragging" @ontouchstart="context.StartDragging">
<span>↕</span><span class="ps-2 d-none d-lg-inline">Move</span>
</button>
</div>
</div>
</ItemTemplate>
</DragAndDropList>
@code {
private record Item(Guid Id, string Text);
private List<Item> Items { get; set; } = Enumerable.Range(1, 8).Select(i => new Item(Guid.NewGuid(), $"Item {i}")).ToList();
private void ItemDropped(DropItemEventArgs<Item> args) {
Items.Reorder(args);
}
}
Style
The only styles that get automatically applied to the list and its items are those that are needed to render the dragging and switching effects for the list items. Further styles can be applied either directly on the item layout template, or using below CSS classes.
drag-and-drop-list
for the list containerdrag-and-drop-list-item
for each list itemdrag-and-drop-list-item-active
for the list item that is currently being dragged
Example
/* Display a shadow around the currently active element's content */
.drag-and-drop-list-item-active > div > div {
box-shadow: 0 0 0.75rem #dee2e6;
}
/* Create smooth switching animations */
.drag-and-drop-list-item:not(.drag-and-drop-list-item-active) {
top: 0;
transition: top 0.4s ease-in-out;
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
- VDT.Core.Blazor.GlobalEventHandler (>= 3.2.0)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.0)
- VDT.Core.Blazor.GlobalEventHandler (>= 3.2.0)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
- VDT.Core.Blazor.GlobalEventHandler (>= 3.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Fix calculation for index delta