0.6.0 • Published 7 months ago

sortable-dnd-lock-axis-fork v0.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

npm npm Software License

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

Live Demo

Usage

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'

var DND = new Sortable(
  document.getElementById('group'),
  {
    chosenClass: 'chosen',
    draggable: (e) => e.target.tagName === 'LI' ? true : false, // use function
    // draggable: 'li' // use tagName 
    // draggable: '.item' // use class
    // draggable: '#item' // use id
    // draggable: (e) => e.target // use function to set drag Element
    handle: (e) => e.target.tagName === 'I' ? true : false, // use function
    // handle: 'I', // use tagName
    // handle: '.handle', // use class
    // handle: '#handle', // use id
    onDrag: ({ from, event }) => {
      // code
    },
    onMove: ({ from, event }) => {
      // code
    },
    onDrop: ({ from, to, changed, event }) => {
      // code
    },
    onAdd: ({ from, to, event }) => {
      // code
    },
    onRemove: ({ from, to, event }) => {
      // code
    },
    onChange: ({ from, to, event }) => {
      // code
    },
    onSelect: (params) => {
      // code
    },
    onDeselect: (params) => {
      // code
    }
  }
)

Methods

MethodDescription
destroy()Manually clear all the state of the component, using this method the component will not be draggable
option(key, value?)Get or set the option value, depending on whether the value is passed in

Options

Common used

OptionTypeDefaultDescription
draggableString/Function-Specifies which items inside the element should be draggable
handleString/Funnction-Drag handle selector within list items
groupString/Object-string: 'name' or object: { name: 'group', put: true/false, pull: true/false }
multipleBooleanfalseEnable multiple drag
animationNumber150Animation speed moving items when sorting
onDragFunction-The callback function when the drag is started
onMoveFunction-The callback function when the dragged element is moving
onDropFunction-The callback function when the drag is completed
onAddFunction-The callback function when element is dropped into the list from another list
onRemoveFunction-The callback function when element is removed from the list into another list
onChangeFunction-The callback function when the dragged element changes position in the list
onSelectFunction-The callback function when element is selected
onDeselectFunction-The callback function when element is unselected

Others

OptionTypeDefaultDescription
disabledBooleanfalseDisables the sortable if set to true
chosenClassString''Class name for the dragging item
selectedClassString''The class of the element when it is selected, it is usually used when multiple drag
ghostStyleObject{}The style of the mask element when dragging
ghostClassString''The class of the mask element when dragging
autoScrollBooleantrueAutomatic scrolling when moving to the edge of the container
scrollThresholdNumber55Threshold to trigger autoscroll
delayNumber0Time in milliseconds to define when the sorting should start
delayOnTouchOnlyBooleanfalseOnly delay if user is using touch
fallbackOnBodyBooleanfalseAppends the cloned DOM Element into the Document's Body
stopPropagationBooleanfalseThe stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases
swapOnDropBooleantrueWhen the value is false, the dragged element will return to the starting position of the drag