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

github.com/clauderic/react-sortable-hoc @v1.11.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.11.0 ↗
90 symbols 208 edges 18 files 0 documented · 0% 12 cross-repo links
README

React Sortable HOC

A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list

npm version npm downloads license Gitter gzip size

Examples available here: http://clauderic.github.io/react-sortable-hoc/

Features

  • Higher Order Components – Integrates with your existing components
  • Drag handle, auto-scrolling, locked axis, events, and more!
  • Suuuper smooth animations – Chasing the 60FPS dream 🌈
  • Works with virtualization libraries: react-virtualized, react-tiny-virtual-list, react-infinite, etc.
  • Horizontal lists, vertical lists, or a grid ↔ ↕ ⤡
  • Touch support 👌
  • Accessible: supports keyboard sorting

Installation

Using npm:

$ npm install react-sortable-hoc --save

Then, using a module bundler that supports either CommonJS or ES2015 modules, such as webpack:

// Using an ES6 transpiler like Babel
import {SortableContainer, SortableElement} from 'react-sortable-hoc';

// Not using an ES6 transpiler
var Sortable = require('react-sortable-hoc');
var SortableContainer = Sortable.SortableContainer;
var SortableElement = Sortable.SortableElement;

Alternatively, an UMD build is also available:

<script src="https://github.com/clauderic/react-sortable-hoc/raw/v1.11.0/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} 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 Component {
  state = {
    items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'],
  };
  onSortEnd = ({oldIndex, newIndex}) => {
    this.setState(({items}) => ({
      items: arrayMove(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.

Why should I use this?

There are already a number of great Drag & Drop libraries out there (for instance, react-dnd is fantastic). If those libraries fit your needs, you should definitely give them a try first. However, most of those libraries rely on the HTML5 Drag & Drop API, which has some severe limitations. For instance, things rapidly become tricky if you need to support touch devices, if you need to lock dragging to an axis, or want to animate the nodes as they're being sorted. React Sortable HOC aims to provide a simple set of higher-order components to fill those gaps. If you're looking for a dead-simple, mobile-friendly way to add sortable functionality to your lists, then you're in the right place.

Prop Types

SortableContainer HOC

Property Type Default Description
axis String y Items can be sorted horizontally, vertically or in a grid. Possible values: x, y or xy
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. Possible values: x or y.
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
keyboardSortingTransitionDuration Number transitionDuration The duration of the transition when the helper is shifted during keyboard sorting. Set this to 0 if you'd like to disable transitions for the keyboard sorting helper. Defaults to the value set for transitionDuration if undefined
keyCodes Array {

  lift: [32],

  drop: [32],

  cancel: [27],

  up: [38, 37],

  down: [40, 39]

} | A object, containing an array of keycodes for each keyboard-accessible action. | | 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. Cannot be used in conjunction with the distance prop. | | pressThreshold | Number | 5 | Number of pixels of movement to tolerate before ignoring a press event. | | distance | Number | 0 | If you'd like elements to only become sortable after being dragged a certain number of pixels. Cannot be used in conjunction with the pressDelay prop. | | shouldCancelStart | Function | Function | This function is invoked before sorting begins, and can be used to programatically cancel sorting before it begins. By default, it will cancel sorting if the event target is either an input, textarea, select or option. | | updateBeforeSortStart | Function | | This function is invoked before sorting begins. It can return a promise, allowing you to run asynchronous updates (such as setState) before sorting begins. function({node, index, collection, isKeySorting}, event) | | onSortStart | Function | | Callback that is invoked when sorting begins. function({node, index, collection, isKeySorting}, event) | | onSortMove | Function | | Callback that is invoked during sorting as the cursor moves. function(event)

Extension points exported contracts — how you extend this code

SortStart (Interface)
(no doc)
types/index.d.ts
SortOver (Interface)
(no doc)
types/index.d.ts
SortEnd (Interface)
(no doc)
types/index.d.ts
Dimensions (Interface)
(no doc)
types/index.d.ts
SortableContainerProps (Interface)
(no doc)
types/index.d.ts

Core symbols most depended-on inside this repo

arrayMove
called by 6
src/utils.js
sortableElement
called by 6
src/SortableElement/index.js
closest
called by 5
src/utils.js
getPosition
called by 5
src/utils.js
setInlineStyles
called by 4
src/utils.js
setTranslate3d
called by 4
src/utils.js
getPixelValue
called by 4
src/utils.js
sortableContainer
called by 4
src/SortableContainer/index.js

Shape

Method 33
Function 31
Class 19
Interface 7

Languages

TypeScript100%

Modules by API surface

src/utils.js20 symbols
src/SortableContainer/index.js15 symbols
src/SortableElement/index.js10 symbols
src/Manager/index.js9 symbols
types/index.d.ts7 symbols
src/SortableHandle/index.js7 symbols
src/AutoScroller/index.js5 symbols
examples/react-virtualized.js4 symbols
rollup.config.js3 symbols
examples/react-infinite.js3 symbols
src/SortableContainer/props.js1 symbols
src/SortableContainer/defaultShouldCancelStart.js1 symbols

Dependencies from manifests, versioned

@babel/core7.2.2 · 1×
@babel/plugin-proposal-class-properties7.2.3 · 1×
@babel/plugin-transform-runtime7.2.0 · 1×
@babel/preset-env7.2.3 · 1×
@babel/preset-react7.0.0 · 1×
@babel/runtime7.2.0 · 1×
@storybook/addon-options5.1.11 · 1×
@storybook/react5.1.11 · 1×
@storybook/theming5.1.11 · 1×
array-move1.0.0 · 1×
autoprefixer6.3.6 · 1×
babel-loader8.0.5 · 1×

For agents

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

⬇ download graph artifact