
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
react-sortablejs
Advanced tools
react-sortablejs
React bindings to SortableJS
Please note that this is not considered ready for production, as there are still a number of bugs being sent through.
sortablejs
and @types/sortablejs
are peer dependencies. The latter only used if intellisense/typescript is desired.
npm install --save react-sortablejs sortablejs
npm install --save-dev @types/sortablejs
# OR
yarn add react-sortablejs sortablejs
yarn add -D @types/sortablejs
Here is the TLDR of what sortable is:
- Shopping List: # list of items / sortable. This represents `react-sortablejs`
- eggs # list item. These are all the items in the list and is what you move around.
- bread # list item
- milk # list item
import React, { FC, useState } from "react";
import { ReactSortable } from "react-sortablejs";
interface ItemType {
id: number;
name: string;
}
export const BasicFunction: FC = (props) => {
const [state, setState] = useState<ItemType[]>([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable list={state} setList={setState}>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
import React, { Component } from "react";
import { ReactSortable } from "react-sortablejs";
interface BasicClassState {
list: { id: string; name: string }[];
}
export class BasicClass extends Component<{}, BasicClassState> {
state: BasicClassState = {
list: [{ id: "1", name: "shrek" }],
};
render() {
return (
<ReactSortable
list={this.state.list}
setList={(newState) => this.setState({ list: newState })}
>
{this.state.list.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
}
}
Sortable has some pretty cool plugins such as MultiDrag and Swap.
By Default:
You must mount the plugin with sortable ONCE ONLY.
import React from "react";
import { ReactSortable, Sortable, MultiDrag, Swap } from "react-sortablejs";
// mount whatever plugins you'd like to. These are the only current options.
Sortable.mount(new MultiDrag(), new Swap());
const App = () => {
const [state, setState] = useState([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable
multiDrag // enables mutidrag
// OR
swap // enables swap
>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
For a comprehensive list of options, please visit https://github.com/SortableJS/Sortable#options.
Those options are applied as follows.
Sortable.create(element, {
group: " groupName",
animation: 200,
delayOnTouchStart: true,
delay: 2,
});
// --------------------------
// Will now be..
// --------------------------
import React from "react";
import { ReactSortable } from "react-sortablejs";
const App = () => {
const [state, setState] = useState([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable
// here they are!
group="groupName"
animation={200}
delayOnTouchStart={true}
delay={2}
>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
Thes are all defaults DOM attributes. Nothing special here.
The same as state
in const [ state, setState] = useState([{ id: 1}, {id: 2}])
state
must be an array of items, with each item being an object that has the following shape:
/** The unique id associated with your item. It's recommended this is the same as the key prop for your list item. */
id: string | number;
/** When true, the item is selected using MultiDrag */
selected?: boolean;
/** When true, the item is deemed "chosen", which basically just a mousedown event. */
chosen?: boolean;
/** When true, it will not be possible to pick this item up in the list. */
filtered?: boolean;
[property: string]: any;
The same as setState
in const [ state, setState] = useState([{ id: 1}, {id: 2}])
If you're using {group: { name: 'groupName', pull: 'clone'}}
, this means your in 'clone' mode. You should provide a function for this.
Check out the source code of the clone example for more information. I'll write it here soon.
ReactSortable is a div
element by default. This can be changed to be any HTML element (for example ul
, ol
)
or can be a React component.
This value, be the component or the HTML element should be passed down under props.tag
.
Let's explore both here.
Here we will use a ul
. You can use any HTML.
Just add the string and ReactSortable will use a li
instead of a div
.
import React, { FC, useState } from "react";
import { ReactSortable } from "react-sortablejs";
export const BasicFunction: FC = (props) => {
const [state, setState] = useState([{ id: "1", name: "shrek" }]);
return (
<ReactSortable tag="ul" list={state} setList={setState}>
{state.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ReactSortable>
);
};
When using a custom component in the tag
prop, the only component it allows is a forwardRef
component.
Currently we only support components who use the React.forwardRef
API.
If it doesn't have one, you can add one using React.forwardRef()
.
todo: Some third party UI components may have nested elements to create the look they're after. This could be an issue and not sure how to fix.
import React, { FC, useState, forwardRef } from "react";
import { ReactSortable } from "react-sortablejs";
// This is just like a normal component, but now has a ref.
const CustomComponent = forwardRef<HTMLDivElement, any>((props, ref) => {
return <div ref={ref}>{props.children}</div>;
});
export const BasicFunction: FC = (props) => {
const [state, setState] = useState([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable tag={CustomComponent} list={state} setList={setState}>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
Sortable affects the DOM, adding, and removing nodes/css when it needs to in order to achieve the smooth transitions we all know an love. This component reverses many of it's actions of the DOM so React can handle this when the state changes.
key !== index
DO NOT use the index as a key for your list items. Sorting will not work.
In all the examples above, I used an object with an ID. You should do the same!
I may even enforce this into the design to eliminate errors.
Basically the child updates the state twice. I'm working on this.
Our usage indicates that as long as we only move items between lists that don't use the same setState
function.
I hope to provide an example soon.
We don't have anything that works 100%, but here I'd like to spit ball some potential avenues to look down.
onMove
to handle state changes instead of onAdd
,onRemove
, etc.react-beautiful-dnd is a drag-and-drop library for React that focuses on providing a beautiful and accessible drag-and-drop experience. It offers more advanced features like automatic scrolling and customizable drag handles. Compared to react-sortablejs, it provides a more polished user experience but may have a steeper learning curve.
react-dnd is a flexible drag-and-drop library for React that is built on top of the HTML5 drag-and-drop API. It provides a more low-level API compared to react-sortablejs, allowing for greater customization and control over the drag-and-drop interactions. It is suitable for more complex use cases where fine-grained control is required.
react-draggable is a library for making elements draggable in React. It is more focused on providing basic drag-and-drop functionality for individual elements rather than lists or grids. It is simpler and more lightweight compared to react-sortablejs, making it a good choice for basic drag-and-drop needs.
FAQs
React bindings to [SortableJS](https://github.com/SortableJS/Sortable)
We found that react-sortablejs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
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.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.