Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

ember-drag-sort

Package Overview
Dependencies
Maintainers
7
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-drag-sort

A sortable list component with support for multiple and nested lists.

latest
Source
npmnpm
Version
5.0.0
Version published
Weekly downloads
8.8K
8.94%
Maintainers
7
Weekly downloads
 
Created
Source

ember-drag-sort

Ember Observer Score npm package version license MIT ember-versions 4.12+

Compatibility

  • Ember.js v4.12 or above
  • Ember CLI v4.12 or above
  • Node.js v20 or above

About

A drag'n'drop sortable list addon.

Features

  • Dragging between lists.
  • Drag handle.
  • Nested lists (tree-like structures).
  • Strict DDAU: does not mutate the lists while dragging. On drag end, calls an action for you to handle list mutation.
  • Disable sorting within a list while still allowing dragging in and out of the list (sort order is determined by host app instead of the user).

Demo

https://adopted-ember-addons.github.io/ember-drag-sort/

Versions, branches and jQuery

  • Version 1.x, gen-0 branch is based on jQuery. Supports Ember CLI 1.13+.
  • Version 2.x, gen-1 branch (current development head) has got rid of jQuery. Except for page objects which still import jQuery in test env. Supports Ember CLI 3.8+.
  • Version 3.x, gen-2 branch supports Ember CLI 3.12+.
  • Version 4.x, main branch supports Ember and Ember CLI 3.28+.

Browser support

Tested manually.

Works in desktop browsers:

  • Chrome
  • Firefox
  • Safari

Does not work on mobile browsers:

  • Safari/iOS: does not support drag'n'drop.
  • Firefox/Android: does not support drag'n'drop.
  • Chrome/Android: for some reason, does not fire the dragend event. Currently, the addon behaves incorrectly on Chrome/Android. If your webapp offers mobile experience, you must manually detect Chrome/Android and disable dragging.

Installation

Install the addon:

ember i ember-drag-sort

Usage

Basic usage

The drag-sort-list component accepts two mandatory arguments:

  • items -- the array of items to sort. Must be an Ember array (normally, all arrays in an Ember app are Ember arrays).
  • dragEndAction -- a closure action that should update elements when sort is complete.

The component accepts a block representing an individual item (the block is rendered multiple times, one per list item). It yields item and index.

<DragSortList @items={{this.items1}} @dragEndAction={{this.dragEnd}} as |item|>
  {{item.name}}
</DragSortList>

The drag end action

It is called on the source list component when the drag'n'drop operation is complete. It's called with a single argument -- an object with the following properties:

PropertyTypeDescription
groupStringGroup provided to the drag-sort-list component.
draggedItemThe list item being dragged.
sourceListArrayThe list where the sorting was initiated.
sourceIndexNumberThe initial index of the item in the source list.
targetListArrayThe list where sorting was finished. Will be the same as sourceList.
targetIndexNumberThe resulting index of the dragged item in the target list.

When sorting within one list, targetIndex assumes that the dragged item is not in the list.

For example, when your list is ['a', 'b', 'c'] and you put b after c, sourceIndex will be 1 and targetIndex will be 2. The initial index of c was 2, so you could suppose that target index after c is 3.

But targetIndex is calculated as if the dragged item b is not in the list: ['a', 'c']. Thus, next index after c will be 2.

This is because the array has three items with indexes 0, 1 and 2, so putting an item to position 3 would make no sense.

Here's the reference implementation of the dragEndAction action:

  @action
  dragEndAction ({sourceList, sourceIndex, targetList, targetIndex/* , sourceArgs, targetArgs */}) {
    if (sourceList === targetList && sourceIndex === targetIndex) return

    const item = sourceList[sourceIndex]

    sourceList.splice(sourceIndex, 1)
    targetList.splice(targetIndex, 0, item)
  }

The drag start action

This action is called when a drag is beginning, and can be used to customize the drag image, or otherwise modify the data transfer. It's called with a single argument -- an object with the following properties:

PropertyTypeDescription
eventEventThe dragstart event.
elementDOMElementThe DOM element being dragged.
draggedItemThe list item being dragged.

This can be used to put margins around the list items without those margins being included in the drag image:

.the-item {
  margin: 20px;
}
<DragSortList
  items={{this.items1}}
  dragStartAction={{action "dragStart"}}
  dragEndAction={{action "dragEnd"}}
  as |item|
>
  <div class="the-item">
    {{item.name}}
  </div>
</DragSortList>
actions: {
  dragStart({ event, element }) {
    let target = element.querySelector('.the-item');
    let { x, y } = element.getBoundingClientRect();
    // Set drag image, positioning it to align with `.the-item`'s position
    event.dataTransfer.setDragImage(target, event.clientX - x, event.clientY - y);
  }
}

The determine foreign position action

You may want to let the user drag items in and out of a list, without letting him rearrange items within a list. In that case the order of items is determined by the app.

Here's a use case. Your CMS allows the admin to put widgets in page areas. The admin panel has a number of lists representing page header, sidebar, footer, etc. The admin is allowed to rearrange widgets to his liking. And there's a list of unused widgets, the admin can drag items to and from that list, but unused items should always be sorted alphabetically.

To achieve that, pass a closure action determineForeignPositionAction into the list of unused items. This will prevent the user from sorting items in that list.

When the user drags a foreign items into such a list, the action will be called to determine the position of the item. Essentially, by running that action the ember-drag-sort addon asks the host app to suggest desired position of the dragged item.

The action is only called for foreign items. When the user drags an item out of the unsortable list but then drags the item back, it will appear on its original position.

The determineForeignPositionAction is called with with a single argument -- an object with the following properties:

PropertyTypeDescription
draggedItemThe item being dragged.
itemsArrayThe list where the item should be positioned.

This action must return an integer -- the desired position of the item.

The simplest implementation is to always put the item into the end of the list:

@action
determineForeignPosition ({/* draggedItem,  */items}) {
  return items.length
}

To sort items alphabetically, you can use lodash:

@action
determineForeignPosition ({draggedItem, items}) {
  return _.sortedIndex(items.toArray(), draggedItem)
}

Or do it by hand:

@action
determineForeignPosition ({draggedItem, items}) {
    const itemsCopy = items.slice(); // make sure not to mutate the list
    itemsCopy.push(draggedItem);
    itemsCopy.sort((a, b) => a.name.localeCompare(b.name));
    return itemsCopy.indexOf(draggedItem);
}

determineForeignPositionAction must not actually sort the list. It's only purpose is to suggest desired item position, which is necessary to display the placeholder.

Marking a list as a source only bucket

Sometimes you may have a need to define a bucket of items that is "source only". This means that you can only grab items from it to add to other buckets. The source bucket can never be reordered or modified by dragging items out of it. It is only a source to drag to other lists.

This list would be marked as source only:

<DragSortList
  @items={{this.items1}}
  @dragEndAction={{this.dragEnd}}
  @sourceOnly={{true}}
  as |item|
>
  {{item.name}}
</DragSortList>

You could then have one or more other lists which you could drag the items from the source list into.

Passing additional arguments

When using drag-sort-list in a template, you can pass additional arguments to it. These arguments will be passed into the dragEndAction.

Here's a case where this is useful. Say, you have parent and child models, with a many-to-many relationship between the two. When you reorder a list of children, the new order must be persisted by calling .save() on the parent. But the dragEndAction does not have access to the parent by default!

To resolve this problem, pass the parent into the drag-sort-list component via the additionalArgs argument:

{{#each parents as |parent|}}
  <DragSortList
    @items          = {{this.parent.children}}
    @additionalArgs = {{hash parent=parent foo="bar"}}
    @dragEndAction  = {{this.dragEnd}}
    as |child|
  }}
    {{child.name}}
  </DragSortList>
{{/each}}

Now you can access the parent of both source and target lists in the dragEndAction. The value of additionalArgs will be exposed as sourceArgs and targetArgs:

@action
dragEndAction({ sourceList, sourceIndex, sourceArgs, targetList, targetIndex, targetArgs }) {
  if (sourceModel === targetModel && sourceIndex === targetIndex) return;

  const item = sourceList[sourceIndex];
  sourceList.splice(sourceIndex, 1);
  targetList.splice(targetIndex, 0, item);

  // Access the parent via `sourceArgs` and `targetArgs`
  sourceArgs.parent.save();
  targetArgs.parent.save();

  console.log(sourceArgs.foo); // => "bar"
  console.log(targetArgs.foo); // => "bar"
}

drag-sort-list arguments reference

ArgumentTypeDefault valueDescription
itemsEmber ArrayAn array of items to display and offer sorting.
dragEndActionClosure actionThis callback will be called on source list when sorting is complete. See above for details.
dragStartActionClosure actionThis callback will be called on source list when dragging is starting. See above for details.
determineForeignPositionActionClosure action or undefinedundefinedWhen provided, used to determine the position of the placeholder when dragging a foreign item into the list. When not provided, the user is able to determine the order. See above for details.
groupundefinedUsed to restrict dragging between multiple lists to only some of those lists. Typically a string.
draggingEnabledBooleantrueDisables sorting. Useful when dragEndAction is an async operation.
childClassString""HTML class applied to list item components.
childTagNameString"div"tagName applied to list item components.
handleString, typically "[draggable]", or nullnullSelector of the drag handle element. When provided, items can only be dragged by handle. :warning: The handle element must have draggable="true" attribute.
isHorizontalBooleanfalseDisplays the list horizontally. :warning: Horizontal lists don't work well when nested.
isRtlBooleanfalseRTL - Right to left. Might be useful for certain languages. :warning: Has no effect on vertical lists.
additionalArgsundefinedA catch-all for additional arguments you may want to access in the dragEndAction. Can be used for things like passing the parent of the list in for saving hasMany relationships.

HTML classes

drag-sort-list component has HTML class dragSortList. It also assumes the following classes dynamically:

HTML classApplied when...
-isEmptyThe given list is empty.
-draggingEnabledDragging is enabled via the draggingEnabled attribute.
-isDraggingDragging is in progress and the given list is either a source list or belongs to the same group as the source list.
-isDraggingOverDragging is in progress and the placeholder is within the given list. This class is removed from a list when an item is dragged into a different list.
-isExpandedDragging is in progress and the given list is either empty or contains only the dragged item. Used to give some height to the list, so that the item can be dragged back into it.

The individual item component has HTML class dragSortItem. It also assumes the following classes dynamically:

HTML classApplied when...
-isDraggedThe given item is the one being dragged. Used to hide the item from the list.
-isDraggingOverDragged item is positioned either above/before or below/after the given item.
-placeholderBeforeDragged item is positioned above/before the given item.
-placeholderAfterDragged item is positioned below/after the given item.

CSS concerns

When dragging, the dragged item is hidden via the -isDragged HTML class that applies display: none.

The placeholder (drop target) is shown via the -placeholderBefore or -placeholderAfter HTML classes. These classes apply padding to the given list item, and the placeholder is an absolutely positioned :before pseudo-element. A similar pseudo-element is applied to an -isExpanded list (see above).

For sorting to work correctly, you must not apply padding to the list HTML element. If you need some padding on the list, apply it to its parent element.

You must not apply any padding or margin to list item elements either. If you need padding between list items, apply it to HTML elements that you pass into list items.

Avoid collapsing margins between list items and between list item and list. Collapsing margins may cause a jumping glitch.

Events

There's an Ember service called dragSort. You can listen to the following events on it, using the Evented API.

Each event is called with as single argument, which is an object with properties. For the description of properties, see dragEndAction documentation above.

Event nameDescriptionArgument properties
startSorting has started.group, draggedItem, sourceList, sourceIndex
sortDragged item has been moved within a list. The list is referenced as targetList.group, draggedItem, sourceList, sourceIndex, targetList, oldTargetIndex, newTargetIndex
moveItem has been dragged into a different list.group, draggedItem, sourceList, sourceIndex, oldTargetList, newTargetList, targetIndex
endSorting has ended.group, draggedItem, sourceList, sourceIndex, targetList, targetIndex

Test helpers

trigger

trigger is a low-level test helper that can be imported like this:

import trigger from "ember-drag-sort/utils/trigger";

It accepts three arguments:

ArgumentTypeDescription
elementString, DOM element or jQuery collectionSelector or element to trigger an operation on.
eventNameStringFor list: dragenter; for list item: dragstart, dragover or dragend.
aboveBooleanOnly for dragover. Whether to put placeholder above (true) or below (false) target item.

The order of operations is the following:

  • dragstart on the element to drag.
  • dragenter on target list that the dragged element should be moved into (optional).
  • dragover on target element, the one that the dragged element should be dropped next to. Provide third argument to indicate above or below.
  • dragover on the dragged element.

After performing the operations, you must wait for async behavior.

See this addon's integration test for example.

sort

sort is a high-level test helper that moves an item to a new position within the same list.

It can be imported like this:

import { sort } from "ember-drag-sort/utils/trigger";

It accepts the following arguments:

ArgumentTypeRequired?Description
sourceListString, DOM element or jQuery collectionyesSelector or element of the drag-sort-list component.
sourceIndexIntegeryesZero-based index of the item to pick up.
targetIndexIntegeryesZero-based index of the item to drop picked item on top of, calculated while the picked item is still on its original position.
aboveBooleanyesWhether to drop picked item above (true) or below (false) target item.
handleSelectorStringnoProvide if handles are used in the list

After executing sort in a test, perform a wait using wait, andThen or await.

Example:

import {sort} from 'ember-drag-sort/utils/trigger'

test('sorting a list', async function (assert) {
  await visit('/')

  const list = document.querySelector('.dragSortList')

  await sort(list, 0, 1, false)

  const expectedTitles = ['Bar', 'Foo', 'Baz', 'Quux']

  assert.strictEqual(list.childElementCount, 4)

  expectedTitles.forEach((expectedTitle, k) => {
    m = `List #0 item #${k} content title`
    expect(list.children[k].textContent.trim(), m).equal(expectedTitle)
  })
}))

move

move is a high-level test helper that moves an item from one list into another.

It can be imported like this:

import { sort } from "ember-drag-sort/utils/trigger";

It accepts the following arguments:

ArgumentTypeRequired?Description
sourceListString, DOM element or jQuery collectionyesSelector or element of the source drag-sort-list component.
sourceIndexIntegeryesZero-based index of the item to pick up.
targetListString, DOM element or jQuery collectionyesSelector or element of the target drag-sort-list component.
targetIndexIntegernoZero-based index of the item to drop picked item on top of, calculated while the picked item is still on its original position. When omitted, adds item to the end of the target list. Must be omitted when moving into an empty list.
aboveBooleanyes if targetList is providedWhether to drop picked item above (true) or below (false) target item.
handleSelectorStringnoProvide if handles are used in the list

After executing sort, perform a wait using wait, andThen or await.

Example:

const [list0, list1] = document.querySelectorAll(".dragSortList");

await move(list0, 0, list1, 1, false);

This will pick the first item from list0 and drop it below the second item of list1.

See this addon's acceptance test for example.

Development

Use Volta

Use Volta to automatically pick correct Node and Yarn versions.

Do not use npm, use yarn

This project uses Yarn to lock dependencies. You can install yarn with npm i -g yarn.

Installation for development

  • git clone <repository-url> this repository
  • cd ember-drag-sort
  • yarn install :warning:

For more information on using ember-cli, visit https://ember-cli.com/.

Running

Branch names

Main branches are named as gen-1, gen-2, etc. Default branch on GitHub is where active development happens.

This naming scheme is due to the fact that this project uses SemVer. As a result, major version number will rise very quickly, without any correlation with actual major changes in the app.

The number in the branch name, "generation", is supposed to be incremented in these cases:

  • A huge improvement or change happens in the addon.
  • There's a change in the addon's API or architecture which introduces a necessity to maintain more than one branch at a time.
  • The codebase is started from scratch.

Pull requests are welcome from feature branches. Make sure to discus proposed changes with addon maintainers to avoid wasted effort.

Updating the table of contents

Maintaining the TOC by hand is extremely tedious. Use this tiny webapp to generate the TOC automatically. Enable the first two checkboxes there.

Demo deployment

This command will deploy the app to https://adopted-ember-addons/ember-drag-sort.github.io/ember-drag-sort/ :

ember deploy prod

Credits

Built by @lolmaus and contributors.

Notable contributors: @frysch, @RobbieTheWagner.

Conceieved in Firecracker.

Reimplemented in Deveo/Perforce.

Originally developed and maintained by kaliber5.

kaliber5

License

This project is licensed under the MIT License.

Keywords

ember-addon

FAQs

Package last updated on 22 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts