Socket
Socket
Sign inDemoInstall

react-dnd-list

Package Overview
Dependencies
6
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-dnd-list

Light and customizable drag and drop list for React


Version published
Weekly downloads
142
decreased by-12.88%
Maintainers
1
Install size
436 kB
Created
Weekly downloads
 

Readme

Source

React Drag and Drop List

Light and customizable drag and drop list for React, with no additional dependencies.

Demo

Check out the demo page here. See the code in the demo directory of this repo.

Getting started

Install:

npm install --save react-dnd-list

Import:

import DnDList from 'react-dnd-list'

Create your item component:

const Item = props => {
  const dnd = props.dnd

  return (
    <li
      style={{ ...dnd.item.styles, ...dnd.handler.styles }}
      className={dnd.item.classes}
      ref={dnd.item.ref}
      {...dnd.handler.listeners}
    >
      {props.item}
    </li>
  )
}

dnd props are essential for the drag and drop functionality. Assign dnd.item.styles, dnd.item.classes, and dnd.item.ref to the outer DOM component. Do not override transform and transition styles of that component!

dnd.handler.listeners is an object containing two keys: onMouseDown and onTouchStart, with appropriate functions as values. Together with dnd.handler.styles, assign it to a component that will initiate drag on click or touch.

The item prop is just a value from your array of items (look below).

Warning! Do not use margins on item components – use padding instead. Support for margins will be added in near future.

Create your list component:

const MyList = () => {
  const [list, setList] = useState(['1', '2', '3'])

  return (
    <ul>
      <DnDList
        items={list}
        itemComponent={Item}
        setList={setList}
      />
    </ul>
  )
}

You need to provide DnDList with an array of items, its update function, and the item component (the one we created earlier).

That's it, you now have a functional drag and drop list!

Customization

Item component

You have access to a few more props inside your item component:

NameTypesDescription
itemInDragbooleantrue if the item is being dragged.
listInDragbooleantrue if any of the items in your list is being dragged.
indexnumberItem's index in the list.
lastbooleantrue if the item is last in the list.

List component

There are several props you can pass to DnDList to customize it:

NameTypeDefaultDescription
horizontalbooleanfalseLet's you swap items horizontally if set to true.
transitionStylesobjectLet's you override default CSS transition styles, for example { transtitionDuration: '.5s' }.
disableTransitionsbooleanfalseDisables swap transitions if set to true.

Function props:

NameArgsReturnsDescription
setSwapThresholdsize (number)numberLet's you set a swap threshold for every item in the list individually, based on their size (height or width, depending on the type of the list). For example size => size * 0.5 will result in elements being swapped after the dragged element has traversed more than 50% of their size.
setOverflowThresholdsize (number)numberFunction similar to setSwapThreshold. Let's you set how far the dragged item can be moved over the list container bounds, based on its (the dragged item) size.

By default swap threshold is set to 1, and overflow threshold to 0.

Keywords

FAQs

Last updated on 20 Apr 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc