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

use-sortablejs

v1.1.6

Published

SortableJS ported to React hooks

Downloads

353

Readme

use-sortablejs

SortableJS ported to React hooks.

Demo: https://harrel56.github.io/use-sortablejs

build actions status

npm license types minified size minzip size

Created to serve as a refreshed alternative to react-sortablejs, with hook design inspired by @mui/base.

Installation

Currently, package is only available as ES module.

npm i use-sortablejs

Contains no external dependencies, only peer dependencies:

  • react: ^17.0.0 || ^18.0.0
  • sortablejs: ^1.0.0
  • @types/react: ^17.0.0 || ^18.0.0
  • @types/sortablejs: ^1.0.0

Package exports:

  • SortableProvider: sortable context provider,
  • useSortable: main hook which requires access to sortable context
  • and typescript definitions.

Supports:

  • all basic functionalities from SortableJS,
  • swap plugin (you have to mount it yourself)
  • and multidrag plugin (you have to mount it yourself).

Usage

Before using useSortable hook, it's required to wrap your application with SortableProvider. Preferably, there should be only one SortableProvider per whole application, but it's not mandatory. Nevertheless, interactions between two sortables in separate contexts have undefined behaviour.

Example:

// App.tsx
import {SortableProvider} from 'use-sortablejs'
import List from './List'

const App = () => {
  return (
    <SortableProvider>
      <List/>
    </SortableProvider>
  )
}
// List.tsx
import {useState} from 'react'
import {useSortable} from 'use-sortablejs'

const List = () => {
  const [items, setItems] = useState([
    'Item 1',
    'Item 2',
    'Item 3',
    'Item 4',
    'Item 5'
  ])
  const {getRootProps, getItemProps} = useSortable({setItems, options: {animation: 150}})
  return (
    <div {...getRootProps()}>
      {items.map(item => <div key={item} {...getItemProps(item)}>{item}</div>)}
    </div>
  )
}

Where item type can be possibly anything (primitive, object of any shape, function).

Details

All types definitions can be found in this file.

useSortable takes UseSortableProps parameter, which is an object containing:

  1. setItems: Dispatch<SetStateAction<T[]>>, where T is your item type. In most cases this should be a setState function returned from React useState hook.
  2. options: ExtendedOptions<T>, options object which you would normally pass to Sortable.create().
  3. (optional) cloneItem: (item: T) => T, clone function to perform when item is being cloned. Defaults to internal shallow clone function.
  4. (optional) sortableRef: LegacyRef<Sortable>, ref object or ref callback, which will be set/called with created Sortable object - set to null on dismount.

Additionally, all event functions that you pass to options object will have access to extended event object (SortableEventExtended<T>), which contains additional field stateItem, which corresponds to dragged item state and is directly mapped from item field.

Using sortableRef

Leveraging options reactivity is the preferred way of achieving dynamic changes to Sortable object, but if you need more control sortableRef is the way to go.

const myRef = useRef<Sortable>(null)
const {getRootProps, getItemProps} = useSortable({setItems, sortableRef: myRef})
const myCallbackRef = (sortable: Sortable | null) => {
  sortable?.option('sort', false)
}
const {getRootProps, getItemProps} = useSortable({setItems, sortableRef: myCallbackRef})

Constraints

  1. Each direct child of node with getRootProps() should have set props from getItemProps(item).
  2. Each direct child of node with getRootProps() should contain unique key prop (NOT list index).
  3. setItems function should cause rerender of sortable list to reflect items state.

Behaviour is undefined if any of these constraints is not met.