Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sortable-dnd

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sortable-dnd

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

  • 0.5.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.4K
decreased by-11.85%
Maintainers
1
Weekly downloads
 
Created
Source

sortable-dnd

npm npm Software License

A JS Library for Drag and Drop, supports Sortable-Draggable and Virtual-List

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
  multiple: false, // Enable multiple drag
  selectHandle: '', // Handle selector within list items which used to select element in `multiple: true`

  animation: 150, // Animation speed moving items when sorting
  chosenClass: '', // Class name for the dragging 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

  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
  fallbackOnBody: false, // Appends the ghost element into the document's body
  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, must return an HTMLElement 
  },
  // callback functions
  onDrag: ({ from, event }) => {
    // Triggered when drag is started
  },
  onMove: ({ from, event }) => {
    // Triggered when the dragged element is moving
  },
  onDrop: ({ from, to, changed, event }) => {
    // Triggered when drag is completed
  },
  onAdd: ({ from, to, event }) => {
    // Triggered when the element is dropped into the list from another
  },
  onRemove: ({ from, to, event }) => {
    // Triggered when the element is removed from the list into another
  },
  onChange: ({ from, to, event }) => {
    // Triggered when the dragged element changes position in the list
  },
  onSelect: (params) => {
    // Triggered when an element is selected by clicking the mouse
  },
  onDeselect: (params) => {
    // Triggered when an element is unselected by clicking the mouse
  },
})

Group

// string
new Sortable(el, {
  ...,
  group: 'name'
})

// object
new Sortable(el, {
  ...,
  group: { 
    name: 'group', // group name
    put: true | false | ['foo', 'bar'], // whether elements can be added from other lists, or an array of group names from which elements can be taken.
    pull: true | false | 'clone', // whether elements can be moved out of this list.
    revertClone: true | false, // revert cloned element to initial position after moving to a another list.
  }
})

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: Options); // Create new instance

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

Sortable.dragged; // The element being dragged

Sortable.ghost; // The ghost element

Sortable.active; // Active Sortable instance

Utils

const { on, off, css, index, closest, getOffset, 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);

// Get element's offet in given parentNode
getOffset(element: HTMLElement, parentEl: HTMLElement);

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

Keywords

FAQs

Package last updated on 08 Dec 2023

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc