Dnd
The Drag and Drop (dnd) component provides a comprehensive solution for implementing drag and drop functionality in your applications. Built on top of SortableJS, it offers a flexible and customizable way to create sortable lists and handle drag-and-drop interactions.
Architecture
Reference
List
The
dnd-listis the primary component of the Spark Drag and Drop Package, providing a wrapper around SortableJS for integration into toddle. By default, any element placed inside the default slot - be it a div, an li element, or a custom component - becomes sortable through drag and drop. It works immediately without any attribute modifications, yet it allows for extensive customization to suit various use cases, implementing nearly all options from SortableJS.Attributes
- animationnumber/default: 150
Defines the animation speed when moving items during sorting, in milliseconds. Set to 0 to disable animation.
- chosen-classstring/default: sortable-chosen
Specifies the CSS class added to an item when it is chosen for dragging.
- delaynumber/default: 100
Specifies the delay in milliseconds before dragging starts. This is particularly useful for enabling smooth scrolling on mobile devices.
- delay-on-touch-onlyboolean/default: true
When enabled, the delay will only be applied on touch devices.
- directionenum/default: vertical
Determines the flex direction of the list. Choose between vertical or horizontal orientation.
- disabledboolean/default: false
Disables all dragging actions for this list when
true. - drag-classstring/default: sortable-drag
Specifies the CSS class applied to an item while it is being dragged.
- easingstring/default: ease
Defines the easing function used for drag and drop animations.
- empty-insert-thresholdnumber/default: 20
Defines the minimum distance in pixels from an empty list required for a dragged element to be inserted.
- fallback-classstring/default: sortable-fallback
Specifies the CSS class applied to the cloned DOM Element when using
force-fallbackmode. - filter-classstring/default: -
Class on elements preventing drag initiation, like “no-drag”.
- flex-wrapenum/default: nowrap
Controls how the list items wrap within the container using CSS flex-wrap property.
- force-fallbackboolean/default: true
When enabled, forces the use of the fallback drag and drop implementation instead of HTML5 native DnD.
- gapstring/default: 0rem
Sets the spacing between list items using CSS gap property.
- ghost-classstring/default: sortable-ghost
Class added to the ghost item, which is the placeholder shown in the list during a drag.
- groupstring/default: random ID
Defines a group identifier that allows dragging items between lists sharing the same group value.
- handle-classboolean/default: false
Specifies a class for drag handle. If
true, an internal handle class is generated and provided automatically. Alternatively you can specify your own handle-class name. - heightstring/default: auto
Sets the height of the list container.
- idstring/default: -
Provides a unique identifier for the list component.
- invert-swapboolean/default: false
When enabled, uses an inverted swap zone calculation for determining item placement.
- inverted-swap-thresholdnumber/default: 1
Defines the threshold percentage (between 0 and 1) for the inverted swap zone calculation.
- prevent-on-filterboolean/default: true
When enabled, prevents default event behavior when a filtered element is attempted to be dragged.
- sortboolean/default: true
Enables sorting within the list.
- swap-thresholdnumber/default: 1
Defines the threshold percentage (between 0 and 1) for the swap zone calculation.
- tagenum/default: div
Specifies the HTML element tag to be used for the list container.
- touch-start-thresholdnumber/default: 4
Defines the minimum pointer movement in pixels required to cancel delayed sorting on touch displays.
- widthstring/default: 100%
Sets the width of the list container.
Events
- On add
Triggered when an element is dropped into the list from another list.
- On change
Triggered when a dragged element changes position within the list.
- On clone
Triggered when an element is cloned during a drag operation.
- On end
Triggered when a drag operation is completed.
- On filter
Triggered when attempting to drag an element that has a filter class applied.
- On move
Triggered during the movement of an item within or between lists.
- On remove
Triggered when an element is removed from the list.
- On sort
Triggered for any modification to the list (add, update, or remove).
- On start
Triggered when a drag operation begins.
- On update
Triggered when the order of items within the list changes.
Classes
- horizontal
Applied to the list container when the
directionattribute is set to horizontal.
Item
The
dnd-itemcomponent functions as a helper within the drag-and-drop ecosystem, seamlessly integrating with thednd-list. It inherits thednd-listcomponent's configuration and state via context and further extends this functionality to its child elements. Essentially, thednd-itemserves as a wrapper for your actual list items, conveying two key pieces of information to its children: whether the current item is being dragged (isChosen) and whether it serves as a placeholder for the drop target (isGhost). Additionally, it offers control over the dragged item's opacity through attributes.Attributes
- drag-opacitynumber/default: 1
Controls the opacity of the item while being dragged, with values ranging from 0 to 1.
- heightstring/default: auto
Sets the height of the draggable item.
- widthstring/default: 100%
Sets the width of the draggable item.
Handle
The
dnd-handlecomponent provides a designated drag handle for items within adnd-list. When used, dragging can only be initiated from this handle, providing more precise control over drag operations.Attributes
- heightstring/default: auto
Sets the height of the drag handle.
- widthstring/default: 100%
Sets the width of the drag handle.
Context
dndListContext
Provides access to the current configuration for classes within the list component.
dndItemContext
Provides access to the current state of the item, including whether it is being dragged (
isChosen) or serving as a placeholder (isGhost).