What is react-sortable-hoc?
The react-sortable-hoc package is a set of higher-order components to turn any list into an animated, touch-friendly, sortable list. It is highly customizable and provides a simple API to create sortable lists with React.
What are react-sortable-hoc's main functionalities?
Sortable List
This feature allows you to create a sortable list where items can be dragged and dropped to reorder them. The SortableContainer and SortableElement higher-order components are used to wrap the list and list items, respectively.
import React from 'react';
import { SortableContainer, SortableElement } from 'react-sortable-hoc';
import arrayMove from 'array-move';
const SortableItem = SortableElement(({ value }) => <li>{value}</li>);
const SortableList = SortableContainer(({ items }) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
);
});
class SortableComponent extends React.Component {
state = {
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
};
onSortEnd = ({ oldIndex, newIndex }) => {
this.setState(({ items }) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
};
render() {
return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />;
}
}
export default SortableComponent;
Drag Handle
This feature allows you to add a drag handle to each item in the list, making it possible to drag items only by the handle. The SortableHandle higher-order component is used to create the handle.
import React from 'react';
import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
import arrayMove from 'array-move';
const DragHandle = SortableHandle(() => <span>::</span>);
const SortableItem = SortableElement(({ value }) => (
<li>
<DragHandle />
{value}
</li>
));
const SortableList = SortableContainer(({ items }) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
);
});
class SortableComponent extends React.Component {
state = {
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
};
onSortEnd = ({ oldIndex, newIndex }) => {
this.setState(({ items }) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
};
render() {
return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} useDragHandle />;
}
}
export default SortableComponent;
Grid Layout
This feature allows you to create a sortable grid layout where items can be dragged and dropped to reorder them in both horizontal and vertical directions. The axis prop is set to 'xy' to enable this functionality.
import React from 'react';
import { SortableContainer, SortableElement } from 'react-sortable-hoc';
import arrayMove from 'array-move';
const SortableItem = SortableElement(({ value }) => <div className="grid-item">{value}</div>);
const SortableGrid = SortableContainer(({ items }) => {
return (
<div className="grid">
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</div>
);
});
class SortableComponent extends React.Component {
state = {
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
};
onSortEnd = ({ oldIndex, newIndex }) => {
this.setState(({ items }) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
};
render() {
return <SortableGrid items={this.state.items} onSortEnd={this.onSortEnd} axis="xy" />;
}
}
export default SortableComponent;
Other packages similar to react-sortable-hoc
react-beautiful-dnd
react-beautiful-dnd is a drag-and-drop library for React that focuses on beautiful and accessible drag-and-drop interactions. It provides a higher level of customization and accessibility compared to react-sortable-hoc, but it can be more complex to set up.
react-dnd
react-dnd is a flexible HTML5 drag-and-drop library for React. It provides a powerful API for creating complex drag-and-drop interfaces. It is more versatile than react-sortable-hoc but requires more boilerplate code and setup.
react-draggable
react-draggable is a simple component for making elements draggable. It is less feature-rich compared to react-sortable-hoc but is easier to use for basic drag-and-drop functionality.
React Sortable (HOC)
Features
- Suuuper smooth animations – Chasing the 60FPS dream 🌈
- Higher Order Components – Integrates with your existing components
- Drag handle, locked axis, events, and more!
- 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:
import {SortableContainer, SortableElement} from 'react-sortable-hoc';
var Sortable = require('react-sortable-hoc');
var SortableContainer = Sortable.SortableContainer;
var SortableElement = Sortable.SortableElement;
Alternatively, an UMD build is also available:
<script src="react-sortable-hoc/dist/umd/react-sortable-hoc.js"></script>
Usage
Basic Example
import React, {Component} from 'react';
import {render} from 'react-dom';
import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-hoc';
const SortableItem = SortableElement(({value}) => <li>{value}</li>);
const SortableList = SortableContainer(({items}) => {
return (
<ul>
{items.map((value, index) =>
<SortableItem key={`item-${index}`} index={index} value={value} />
)}
</ul>
);
});
class SortableComponent extends Component {
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 (
<SortableList items={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
Property | Type | Default | Description |
---|
axis | String | y | The axis you want to sort on, either 'x' or 'y' |
lockAxis | String | | If you'd like, you can lock movement to an axis while sorting. This is not something that is possible with HTML5 Drag & Drop |
helperClass | String | | You can provide a class you'd like to add to the sortable helper to add some styles to it |
transitionDuration | Number | 300 | The duration of the transition when elements shift positions. Set this to 0 if you'd like to disable transitions |
pressDelay | Number | 0 | If 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 |
onSortStart | Function | | Callback that get's invoked when sorting begins. function({node, index, collection}, event) |
onSortMove | Function | | Callback that get's invoked during sorting as the cursor moves. function(event) |
onSortEnd | Function | | Callback that get's invoked when sortin ends. function({oldIndex, newIndex, collection}, e) |
useDragHandle | Boolean | false | If you're using the SortableHandle HOC, set this to true |
useWindowAsScrollContainer | Boolean | false | If you want, you can set the window as the scrolling container |
hideSortableGhost | Boolean | true | Whether 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. |
lockToContainerEdges | Boolean | false | You can lock movement of the sortable element to it's parent SortableContainer |
SortableElement HOC
Property | Type | Default | Required? | Description |
---|
index | Number | | ✓ | This is the element's sortableIndex within it's collection. This prop is required. |
collection | Number or String | 0 | | The collection the element is part of. This is useful if you have multiple groups of sortable elements within the same SortableContainer . Example |
disabled | Boolean | false | | Whether the element should be sortable or not |
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.