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-cool-draggable

v0.1.4

Published

Drag-n-drop react library (component) for horizontal and vertical lists.

Downloads

31

Readme

react-cool-draggable

Drag-n-drop react library (component) for horizontal and vertical lists.

Dark

  • 👨‍🎓 Easy to use
  • 💪 Small size (6 kb gzipped)
  • 🚀 60 FPS performance
  • 🎢 Animated transitions
  • 💅 Сustomizable appearance
  • 🙃 Touch devices support
  • 📏 Any element sizes
  • ✔️ No dependencies

One common frontend task is to create draggable cards in various lists. To simplify this task, I wrote a library that can work with horizontal and vertical lists, as well as their combinations (for example, as done in Trello).

Demo

Installation

npm:

npm install react-cool-draggable

yarn:

yarn add react-cool-draggable

Usage

import {
  DragDropContext,
  Droppable,
  Draggable,
  reorder,
} from 'react-cool-draggable';

const handleDragEnd = options => {
  const newItems = reorder({
    ...options,
    items,
    getDraggableID: x => x.ID,
    getDroppableID: () => 'row',
  });

  setItems(newItems);
};

<DragDropContext onDragEnd={handleDragEnd}>
  <Droppable
    direction='vertical'
    droppableID='row'
    droppableGroupID='board'>
    {({ snapshot, ...rest }) => (
      <div className='droppable' {...rest}>
        {items.map(x => (
          <Draggable key={x.ID} draggableID={x.ID}>
            {({ snapshot, rootProps, draggableProps }) => (
              <div className='draggable' {...rootProps} {...draggableProps}>
                {x.text}
              </div>
            )}
          </Draggable>
        ))}
      </div>
    )}
  </Droppable>
</DragDropContext>

You can also combine and nest droppable and draggable elements to create more complex components if you need to.

API

Some universal types here

type ID = string | number;

type Direction = 'vertical' | 'horizontal';

type DraggableElement = HTMLElement | SVGElement;

DragDropContext

This library uses the react context as a way to communicate between children in the tree, so the context must be explicitly invoked at the root of the component.

import { DragDropContext } from 'react-cool-draggable';
<DragDropContext onDragEnd={handleDragEnd}>
  Some children here...
</DragDropContext>
type DragDropContextProps = {
  onDragStart?: (options: OnDragStartOptions) => void;
  onDragOver?: (options: OnDragOverOptions) => void;
  onDragEnd: (options: OnDragEndOptions) => void;
  children: React.ReactElement;
};

All of these callback accept a number of options that may be useful to you later.

type OnDragStartOptions = {
  draggableID: ID;
  droppableID: ID;
  droppableGroupID: ID;
  targetNode: DraggableElement;
};

type OnDragOverOptions = {
  draggableID: ID;
  droppableID: ID;
  droppableGroupID: ID;
  targetNode: DraggableElement;
  nearestNode: DraggableElement | null;
};

type OnDragEndOptions = {
  draggableID: ID;
  droppableID: ID;
  droppableGroupID: ID;
  sourceIdx: number;
  destinationIdx: number;
  isMoving: boolean;
  targetNode: DraggableElement;
};

Droppable

Think of this component as a surface on which elements move. It is simple logic where all the logic is hidden. At the same time, it is based on the concept of RenderProps and expects to receive a render function as children.

import { Droppable } from 'react-cool-draggable';
<Droppable
  direction='vertical'
  droppableID='row'
  droppableGroupID='board'>
  {({ snapshot, ...rest }) => <div {...rest}>Some children here...</div>}
</Droppable>
type DroppableProps = {
  direction: Direction;
  droppableID: ID;
  droppableGroupID: ID;
  transitionTimeout?: number;
  transitionTimingFn?: string;
  disabled?: boolean;
  debounceTimeout?: number;
  children: (options: DroppableChildrenOptions) => React.ReactElement;
};

The direction - the main property that specifies how the component should deal with the drag direction. You will also need to provide some identifiers so that you can distinguish one surface from another.

type DroppableChildrenOptions = {
  snapshot: {
    isDragging: boolean;
  };
  ...some service options 
};

The snapshot can be useful for you to understand and react to real-time dragging, such as changing the surface color to let the user know that the dragging mode has been activated.

Draggable

You can think of this component as an element that is draggable by the user. And it is also just a component that implements logic without any external appearance. So he also needs a render function to tell it what it should look like.

import { Draggable } from 'react-cool-draggable';
<Draggable draggableID={x.ID}>
  {({ snapshot, rootProps, draggableProps }) =>
    <div {...rootProps} {...draggableProps}>Some children here...</div>}
</Draggable>
type DraggableProps = {
  draggableID: ID;
  children: (options: DraggableChildrenOptions) => React.ReactElement;
};
type DraggableChildrenOptions = {
  snapshot: {
    isDragging: boolean;
  };
  rootProps: SomeServiceOptions;
  draggableProps: SomeServiceOptions;
};

In the render function declaration, you must pass rootProps and draggableProps to the element. At the same time, if you want to make the draggable activation not be carried out on the entire element, but only on its part (for example, a button that needs to be captured in order to drag), then you must pass draggableProps to this button and pass only rootProps to the element itself.

reorder and move

import { reorder, move } from 'react-cool-draggable';

These are the functions you need to apply inside the onDragEnd callback to sort the list of items in the component's state correctly. In this case, reorder does the usual sorting, and move - moving an element from one group to another, if you implement an exchange of elements between two or more droppable surfaces. This may sound complicated, but it isn't, so please see examples/trello for examples of how to use these functions.

const newItems = reorder({
  ...options,
  items,
  getDraggableID: x => x.ID,
  getDroppableID: () => 'row',
});
const newItems = move({
  ...options,
  items,
  getDraggableID: x => x.ID,
  getDroppableID: x => x.groupID,
  setDroppableID: (x, droppableID) => {
    x.groupID = droppableID;
  },
});

LICENSE

MIT © Alex Plex