npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-sortable

v2.0.0

Published

Make your React components sortable.

Downloads

11,227

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