npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sortable-dnd

v0.6.14

Published

JS library for drag-and-drop lists, supports sortable and draggable

Downloads

14,737

Readme

sortable-dnd

npm npm Software License

A JS Library for Drag and Drop, supports Sortable and Draggable

Live Demo

Usage

Install

npm install sortable-dnd --save

HTML

<ul id="group">
  <li class="item">
    <i id="handle" class="handle">drag me</i>
    <p>1</p>
  </li>
  <li class="item">
    <i id="handle" class="handle">drag me</i>
    <p>2</p>
  </li>
  <li class="item">
    <i id="handle" class="handle">drag me</i>
    <p>3</p>
  </li>
</ul>

JavaScript

import Sortable from 'sortable-dnd';

let sortable = new Sortable(document.getElementById('group'), {
  // draggable: 'li', // use tagName
  // draggable: '#item', // use id
  draggable: '.item', // use class
  // handle: 'I', // use tagName
  // handle: '#handle', // use id
  // handle: (e) => e.target.tagName === 'I' ? true : false, // use function
  handle: '.handle', // use class
});

Options

new Sortable(element, {
  draggable: '', // Specifies which items inside the element should be draggable
  handle: '', // Drag handle selector within list items
  group: '', // see @Group
  lockAxis: '', // Axis on which dragging will be locked
  multiple: false, // Enable multiple drag
  selectHandle: '', // Handle selector within list items which used to select element in `multiple: true`

  easing: '', // Easing for animation
  animation: 150, // Animation speed moving items when sorting
  chosenClass: '', // Class name for the chosen item
  selectedClass: '', // The class of the element when it is selected, it is usually used when multiple drag
  ghostStyle: {}, // The style of the mask element when dragging
  ghostClass: '', // The class of the mask element when dragging

  sortable: true, // Whether the current list can be sorted by dragging.
  disabled: false, // Disables the sortable if set to true
  autoScroll: true, // Automatic scrolling when moving to the edge of the container
  scrollThreshold: 55, // Threshold to trigger autoscroll
  scrollSpeed: { x: 10, y: 10 }, // Vertical&Horizontal scrolling speed (px)
  delay: 0, // Time in milliseconds to define when the sorting should start
  delayOnTouchOnly: false, // Only delay if user is using touch
  touchStartThreshold: 1, // How many *pixels* the point should move before cancelling a delayed drag event.
  emptyInsertThreshold: -1, // Distance mouse must be from empty sortable to insert drag element into it.
  fallbackOnBody: false, // Appends the ghost element into the document's body
  store: null, // store data
  direction: '', // Direction of Sortable, will be detected automatically if not given.
  swapOnDrop: true, // When the value is false, the dragged element will return to the starting position of the drag

  customGhost: (nodes) => {
    // Customize the ghost element in drag
    // you must return an HTMLElement
  },

  // Element is chosen
  onChoose: (event) => {
    // see @SortableEvent
  },

  // Element is unchosen
  onUnchoose: (event) => {
    // see @SortableEvent
  },

  // Element dragging started
  onDrag: (event) => {
    // see @SortableEvent
  },

  // Move an item in the list or between lists
  onMove: (event) => {
    // see @SortableEvent
  },

  // Element dragging is completed
  onDrop: (event) => {
    // see @SortableEvent
  },

  // Element is dropped into the current list from another
  onAdd: (event) => {
    // see @SortableEvent
  },

  // Element is removed from the current list into another
  onRemove: (event) => {
    // see @SortableEvent
  },

  // Dragging element changes position in the current list
  onChange: (event) => {
    // see @SortableEvent
  },

  // Element is selected
  onSelect: (event) => {
    // see @SelectEvent
  },

  // Element is unselected
  onDeselect: (event) => {
    // see @SelectEvent
  },
});

Group

// string
group: 'name',

// object
group: {
  name: 'group', // group name

  // whether elements can be added from other lists,
  // or an array of group names from which elements can be taken.
  put: true | false | ['group1', 'group2'],

  // whether elements can be moved out of this list.
  pull: true | false | 'clone',

  // revert drag element to initial position after moving to a another list.
  revertDrag: true | false,
}

SortableEvent

event.from; // previous list
event.to; // list, in which moved element.
event.node; // dragged element
event.nodes; // dragged elements
event.clone; // cloned element, all dnd operations are based on cloned element and do not alter the source dom(node).
event.clones; // cloned elements, there is a value only in the `pull: clone` after moving to a another list.
event.target; // drop element
event.oldIndex; // old index within parent. `-1`: element added from another list to the current list
event.newIndex; // new index within parent. `-1`: element has been removed from the current list
event.event; // TouchEvent | MouseEvent
event.pullMode; // Pull mode if dragging into another sortable.
event.relative; // Position of the drop element relative to the drag element after swap is complete.
event.revertDrag; // revert draged element to initial position after moving to a another list in `pull: 'clone'` & `revertDrag: true`.
event.backToOrigin; // dragged element go back to the original list in `pull: 'clone'`.

SelectEvent

event.event; // TouchEvent | MouseEvent
event.index; // index within parent
event.node; // dragged element
event.from;  // list container

Methods

let sortable = new Sortable(el);

// Manually clear all the state of the component, using this method the component will not be draggable
sortable.destroy();

// Get or set the option value, depending on whether the `value` is passed in
sortable.option(key, value?);

// Selects the provided multi-drag item
sortable.select(element);

// Deselects the provided multi-drag item
sortable.deselect(element);

// Get the selected elements in the list, the return value is available in the case of `multiple`
sortable.getSelectedElements();

Static Methods & Properties

import Sortable from 'sortable-dnd';

Sortable.create(el: HTMLElement, options: SortableOptions); // Create new instance

Sortable.get(el: HTMLElement); // Get the Sortable instance of an element

Sortable.dragged; // The element being dragged

Sortable.clone; // The clone element

Sortable.ghost; // The ghost element

Sortable.active; // Active Sortable instance

Utils

import Sortable from 'sortable-dnd';
const { on, off, css, index, closest, getRect, toggleClass } = Sortable.utils;

// attach an event handler function
on(el: HTMLElement, event: String, fn: Function);

// remove an event handler
off(el: HTMLElement, event: String, fn: Function);

// set one CSS properties
css(el: HTMLElement, prop: String, value: String);

// Returns the index of an element within its parent for a selected set of elements
index(el: HTMLElement, selector: String);

// For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
closest(el: HTMLElement, selector: String, context: HTMLElement, includeContext: Boolean);

// Returns the "bounding client rect" of given element
getRect(element: HTMLElement, relativeToContainingBlock: boolean, container: HTMLElement);

// Add or remove one classes from each element
toggleClass(el: HTMLElement, name: String, state: Boolean);