MCPcopy Index your code
hub / github.com/danielstocks/react-sortable

github.com/danielstocks/react-sortable @2.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 2.0.0 ↗ · + Follow
803 symbols 1,831 edges 31 files 121 documented · 15%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

React Sortable

David npm npm

Higher-order component for creating sortable lists with minimalistic implementation and without polyfills. Using just React.js and HTML5 DragEvent interface.

Mainly tested in latest stable Webkit, Firefox and IE.

Check out demo and source.

Installation

To install a stable release use:

npm i react-sortable --save

Example

First import the necessary dependencies.

import React from 'react';
import ReactDOM from 'react-dom';
import { sortable } from 'react-sortable';

Then create a component for the single item of the list.

class Item extends React.Component {
  render() {
    return (
      <li {...this.props}>
        {this.props.children}
      </li>
    )
  }
}


var SortableItem = sortable(Item);

And create component for the whole list, which will be the main component.

class SortableList extends React.Component {

  state = {
    items: this.props.items
  };

  onSortItems = (items) => {
    this.setState({
      items: items
    });
  }

  render() {
    const { items } = this.state;
    var listItems = items.map((item, i) => {
      return (
        <SortableItem
          key={i}
          onSortItems={this.onSortItems}
          items={items}
          sortId={i}>{item}</SortableItem>
      );
    });

    return (
      <ul className='sortable-list'>
        {listItems}
      </ul>
    )
  }
};

Now you can pass a list of items to the main component and render the whole result.



var items = [
  "Gold",
  "Crimson",
  "Hotpink",
  "Blueviolet",
  "Cornflowerblue",
  "Skyblue",
  "Lightblue",
  "Aquamarine",
  "Burlywood"
]

ReactDOM.render(
  <SortableList items={items} />,
  document.body
);

You can see this simple working demo in the ./example folder. For visual styling, you can add className of your choice.

How it works

Component will automatically attach the necessary drag event handlers.

It expects the following properties to be defined:

  • key (position of item in virtaul dom recommendation)
  • onSortItems (function called when an item is moved - dispatching if Redux action)
  • items (array of items to be sorted)
  • sortId (index of item in array)

Differences from react-dnd

  • fewer lines of code, easier to understand and modify
  • can handle both horizontal and vertical dragging
  • code is documented and covered with unit tests

Touch support

Unfortunately, at the moment there is no support of this interface in mobile browsers.

Purpose of this repo

This repository was published back in 2014 and was pretty much the very first implementation of drag and drop sortable list for React.js. Nowadays since there are other repositories which are well maintained (react-beautiful-dnd, react-sortable-hoc), I can recommend to use some of them in your project. This repository is now rather a showcase of what can be done just with simple React.js component and bare HTML5 API, having as few lines of code as possible. It can serve as inspiration for somebody who would like to reimplement this functionality from scratch.

Mainteners

Core symbols most depended-on inside this repo

invariant
called by 119
bundle.js
w
called by 100
bundle.js
f
called by 28
bundle.js
d
called by 25
bundle.js
b
called by 23
bundle.js
c
called by 22
bundle.js
x
called by 17
bundle.js
h
called by 16
bundle.js

Shape

Function 776
Class 18
Method 9

Languages

TypeScript100%

Modules by API surface

bundle.js699 symbols
lib/standalone/react-sortable.js53 symbols
lib/standalone/react-sortable.min.js8 symbols
lib/SortableComposition.js8 symbols
src/SortableComposition.js4 symbols
examples/redux/index.js3 symbols
examples/redux/SortableList.js3 symbols
examples/redux/SortableItem.js3 symbols
examples/html-table/SortableList.js3 symbols
examples/html-table/SortableItem.js3 symbols
examples/basic-list/SortableList.js3 symbols
examples/basic-list/SortableItem.js3 symbols

For agents

$ claude mcp add react-sortable \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact