Skip to content

Repository files navigation

React Sortable (HOC)

A set of higher-order components to turn any list into an animated, touch-friendly, sortable list.

npm versionlicenseXO code styleGitter

Features

  • Higher Order Components – Integrates with your existing components
  • Drag handle, auto-scrolling, locked axis, events, and more!
  • Suuuper smooth animations – Chasing the 60FPS dream 🌈
  • Works with React Virtualized, React-Infinite, etc.
  • Horizontal or vertical lists ↔ ↕
  • Touch support 👌

Installation

Using npm:

$ npm install react-sortable-hoc --save

Then, using a module bundler that supports either CommonJS or ES2015 modules, such as webpack:

// Using an ES6 transpiler like Babelimport{SortableContainer,SortableElement}from'react-sortable-hoc';// Not using an ES6 transpilervarSortable=require('react-sortable-hoc');varSortableContainer=Sortable.SortableContainer;varSortableElement=Sortable.SortableElement;

Alternatively, an UMD build is also available:

<scriptsrc="react-sortable-hoc/dist/umd/react-sortable-hoc.js"></script>

Usage

Basic Example

importReact,{Component}from'react';import{render}from'react-dom';import{SortableContainer,SortableElement,arrayMove}from'react-sortable-hoc';constSortableItem=SortableElement(({value})=><li>{value}</li>);constSortableList=SortableContainer(({items})=>{return(<ul>{items.map((value,index)=><SortableItemkey={`item-${index}`}index={index}value={value}/>)}</ul>);});classSortableComponentextendsComponent{state={items: ['Item 1','Item 2','Item 3','Item 4','Item 5','Item 6']}onSortEnd=({oldIndex, newIndex})=>{this.setState({items: arrayMove(this.state.items,oldIndex,newIndex)});};render(){return(<SortableListitems={this.state.items}onSortEnd={this.onSortEnd}/>)}}render(<SortableComponent/>,document.getElementById('root'));

That's it! React Sortable does not come with any styles by default, since it's meant to enhance your existing components.

More code examples are available here.

Prop Types

SortableContainer HOC

PropertyTypeDefaultDescription
axisStringyThe axis you want to sort on, either 'x' or 'y'
lockAxisStringIf you'd like, you can lock movement to an axis while sorting. This is not something that is possible with HTML5 Drag & Drop
helperClassStringYou can provide a class you'd like to add to the sortable helper to add some styles to it
transitionDurationNumber300The duration of the transition when elements shift positions. Set this to 0 if you'd like to disable transitions
pressDelayNumber0If you'd like elements to only become sortable after being pressed for a certain time, change this property. A good sensible default value for mobile is 200
onSortStartFunctionCallback that get's invoked when sorting begins. function({node, index, collection}, event)
onSortMoveFunctionCallback that get's invoked during sorting as the cursor moves. function(event)
onSortEndFunctionCallback that get's invoked when sortin ends. function({oldIndex, newIndex, collection}, e)
useDragHandleBooleanfalseIf you're using the SortableHandle HOC, set this to true
useWindowAsScrollContainerBooleanfalseIf you want, you can set the window as the scrolling container
hideSortableGhostBooleantrueWhether to auto-hide the ghost element. By default, as a convenience, React Sortable List will automatically hide the element that is currently being sorted. Set this to false if you would like to apply your own styling.
lockToContainerEdgesBooleanfalseYou can lock movement of the sortable element to it's parent SortableContainer

SortableElement HOC

PropertyTypeDefaultRequired?Description
indexNumberThis is the element's sortableIndex within it's collection. This prop is required.
collectionNumber or String0The collection the element is part of. This is useful if you have multiple groups of sortable elements within the same SortableContainer. Example
disabledBooleanfalseWhether the element should be sortable or not
Why shoud I use this?

There are already a number of great Drag & Drop libraries out there (for instance, react-dnd is fantastic). If those libraries fit your needs, you should definitely give them a try first. However, most of those libraries rely on the HTML5 Drag & Drop API, which has some severe limitations. For instance, things rapidly become tricky if you need to support touch devices, if you need to lock dragging to an axis, or want to animate the nodes as they're being sorted. React Sortable HOC aims to provide a simple set of higher-order components to fill those gaps. If you're looking for a dead-simple, mobile-friendly way to add sortable functionality to your lists, then you're in the right place.

Dependencies

React Sortable List has very few dependencies. It depends on invariant and a couple lodash functions. It has the following peerDependencies: react, react-dom

Contributions

Yes please! Feature requests / pull requests are welcome.

Made with ❤︎ in the heart of Montreal.

About

A set of higher-order components to turn any list into an animated, touch-friendly, sortable list.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages