Gathering detailed insights and metrics for react-sortablejs
Gathering detailed insights and metrics for react-sortablejs
Gathering detailed insights and metrics for react-sortablejs
Gathering detailed insights and metrics for react-sortablejs
sortablejs
JavaScript library for reorderable drag-and-drop lists on modern browsers and touch devices. No jQuery required. Supports Meteor, AngularJS, React, Polymer, Vue, Knockout and any CSS library, e.g. Bootstrap.
vuedraggable
draggable component for vue
@types/sortablejs
TypeScript definitions for sortablejs
@timluo465/react-sortablejs
A React component built on top of Sortable (https://github.com/SortableJS/Sortable).
npm install react-sortablejs
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,069 Stars
550 Commits
211 Forks
25 Watching
2 Branches
41 Contributors
Updated on 27 Nov 2024
TypeScript (99.67%)
JavaScript (0.33%)
Cumulative downloads
Total Downloads
Last day
4.4%
41,494
Compared to previous day
Last week
4.3%
197,294
Compared to previous week
Last month
8.9%
825,393
Compared to previous month
Last year
19.1%
9,385,112
Compared to previous year
2
4
30
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 is only used if intellisense/typescript is desired.
1npm install --save react-sortablejs sortablejs 2npm install --save-dev @types/sortablejs 3 4# OR 5yarn add react-sortablejs sortablejs 6yarn add -D @types/sortablejs
Here is the TLDR of what sortable is:
1- Shopping List: # list of items / sortable. This represents `react-sortablejs` 2 - eggs # list item. These are all the items in the list and are what you move around. 3 - bread # list item 4 - milk # list item
1import React, { FC, useState } from "react"; 2import { ReactSortable } from "react-sortablejs"; 3 4interface ItemType { 5 id: number; 6 name: string; 7} 8 9export const BasicFunction: FC = (props) => { 10 const [state, setState] = useState<ItemType[]>([ 11 { id: 1, name: "shrek" }, 12 { id: 2, name: "fiona" }, 13 ]); 14 15 return ( 16 <ReactSortable list={state} setList={setState}> 17 {state.map((item) => ( 18 <div key={item.id}>{item.name}</div> 19 ))} 20 </ReactSortable> 21 ); 22};
1import React, { Component } from "react"; 2import { ReactSortable } from "react-sortablejs"; 3 4interface BasicClassState { 5 list: { id: string; name: string }[]; 6} 7 8export class BasicClass extends Component<{}, BasicClassState> { 9 state: BasicClassState = { 10 list: [{ id: "1", name: "shrek" }], 11 }; 12 render() { 13 return ( 14 <ReactSortable 15 list={this.state.list} 16 setList={(newState) => this.setState({ list: newState })} 17 > 18 {this.state.list.map((item) => ( 19 <div key={item.id}>{item.name}</div> 20 ))} 21 </ReactSortable> 22 ); 23 } 24}
Sortable has some pretty cool plugins such as MultiDrag and Swap.
By Default:
You must mount the plugin with sortable ONCE ONLY.
1import React from "react"; 2import { ReactSortable, Sortable, MultiDrag, Swap } from "react-sortablejs"; 3 4// mount whatever plugins you'd like to. These are the only current options. 5Sortable.mount(new MultiDrag(), new Swap()); 6 7const App = () => { 8 const [state, setState] = useState([ 9 { id: 1, name: "shrek" }, 10 { id: 2, name: "fiona" }, 11 ]); 12 13 return ( 14 <ReactSortable 15 multiDrag // enables mutidrag 16 // OR 17 swap // enables swap 18 > 19 {state.map((item) => ( 20 <div key={item.id}>{item.name}</div> 21 ))} 22 </ReactSortable> 23 ); 24};
For a comprehensive list of options, please visit https://github.com/SortableJS/Sortable#options.
Those options are applied as follows.
1Sortable.create(element, { 2 group: " groupName", 3 animation: 200, 4 delayOnTouchStart: true, 5 delay: 2, 6}); 7 8// -------------------------- 9// Will now be... 10// -------------------------- 11 12import React from "react"; 13import { ReactSortable } from "react-sortablejs"; 14 15const App = () => { 16 const [state, setState] = useState([ 17 { id: 1, name: "shrek" }, 18 { id: 2, name: "fiona" }, 19 ]); 20 21 return ( 22 <ReactSortable 23 // here they are! 24 group="groupName" 25 animation={200} 26 delayOnTouchStart={true} 27 delay={2} 28 > 29 {state.map((item) => ( 30 <div key={item.id}>{item.name}</div> 31 ))} 32 </ReactSortable> 33 ); 34};
These are all default 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:
1 /** The unique id associated with your item. It's recommended this is the same as the key prop for your list item. */ 2 id: string | number; 3 /** When true, the item is selected using MultiDrag */ 4 selected?: boolean; 5 /** When true, the item is deemed "chosen", which basically just a mousedown event. */ 6 chosen?: boolean; 7 /** When true, it will not be possible to pick this item up in the list. */ 8 filtered?: boolean; 9 [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 you're 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 it the component or the HTML element, should be passed down under props.tag
.
Let's explore both here.
Here we will use an ul
. You can use any HTML.
Just add the string and ReactSortable will use a ul
instead of a div
.
1import React, { FC, useState } from "react"; 2import { ReactSortable } from "react-sortablejs"; 3 4export const BasicFunction: FC = (props) => { 5 const [state, setState] = useState([{ id: "1", name: "shrek" }]); 6 7 return ( 8 <ReactSortable tag="ul" list={state} setList={setState}> 9 {state.map((item) => ( 10 <li key={item.id}>{item.name}</li> 11 ))} 12 </ReactSortable> 13 ); 14};
When using a custom component in the tag
prop, the only component it allows is a forwardRef
component.
Currently, we only support components that 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 it.
1import React, { FC, useState, forwardRef } from "react"; 2import { ReactSortable } from "react-sortablejs"; 3 4// This is just like a normal component, but now has a ref. 5const CustomComponent = forwardRef<HTMLDivElement, any>((props, ref) => { 6 return <div ref={ref}>{props.children}</div>; 7}); 8 9export const BasicFunction: FC = (props) => { 10 const [state, setState] = useState([ 11 { id: 1, name: "shrek" }, 12 { id: 2, name: "fiona" }, 13 ]); 14 15 return ( 16 <ReactSortable tag={CustomComponent} list={state} setList={setState}> 17 {state.map((item) => ( 18 <div key={item.id}>{item.name}</div> 19 ))} 20 </ReactSortable> 21 ); 22};
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 its 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 spitball some potential avenues to look down.
onMove
to handle state changes instead of onAdd
,onRemove
, etc.No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 7/18 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More