@edorivai/react-smooth-dnd

中文文档
0.11.2发布于 2022/09/08ISCTypeScriptCommonJS近 30 天下载 1.1万

Drag and Drop library

原始使用文档
文档目录20 节
  1. Demo page
  2. Installation
  3. Usage
  4. API
  5. Container
  6. Props
  7. onDragStart
  8. onDragEnd
  9. onDropReady
  10. onDrop
  11. getChildPayload
  12. shouldAnimateDrop
  13. shouldAcceptDrop
  14. onDragEnter
  15. onDragLeave
  16. getGhostParent
  17. render
  18. Draggable
  19. Props
  20. render

This is a fork!

Sept 8, 2022

This is a fork of react-smooth-dnd. The sole purpose of this fork is to widen the React peer dependency as submitted in PR #80. If that PR gets merged in, this fork is not required any longer.

react-smooth-dnd

A fast and lightweight drag&drop, sortable library for React with many configuration options covering many d&d scenarios. It uses css transitions for animations so it's hardware accelerated whenever possible.

This library consists wrapper React components over smooth-dnd library.

Show, don't tell !

Demo page

Installation

npm i @edorivai/react-smooth-dnd

Usage

JSX

import React, { Component } from "react";
import { Container, Draggable } from "@edorivai/react-smooth-dnd";

class SimpleSortableList extends Component {
  render() {
    return (
      <div>
        <Container onDrop={this.props.onDrop}>
          {this.props.items.map((item) => {
            return (
              <Draggable key={item.id}>{this.props.renderItem(item)}</Draggable>
            );
          })}
        </Container>
      </div>
    );
  }
}

API

Container

Component that contains the draggable elements or components. Each of its children should be wrapped by Draggable component

Props

PropertyTypeDefaultDescription
childrenDraggablenullReact children prop. Should be of type Draggable.
orientationstringverticalOrientation of the container. Can be horizontal or vertical.
behaviourstringmoveProperty to describe weather the dragging item will be moved or copied to target container. If drop-zone is set no draggable will slide when container dragged over. Can be move or copy or drop-zone or contain
groupNamestringundefinedDraggables can be moved between the containers having the same group names. If not set container will not accept drags from outside. This behaviour can be overriden by shouldAcceptDrop function. See below.
lockAxisstringundefinedLocks the movement axis of the dragging. Possible values are x, y or undefined.
dragHandleSelectorstringundefinedCss selector to test for enabling dragging. If not given item can be grabbed from anywhere in its boundaries.
nonDragAreaSelectorstringundefinedCss selector to prevent dragging. Can be useful when you have form elements or selectable text somewhere inside your draggable item. It has a precedence over dragHandleSelector.
dragBeginDelaynumber0 (200 for touch devices)Time in milisecond. Delay to start dragging after item is pressed. Moving cursor before the delay more than 5px will cancel dragging.
animationDurationnumber250Animation duration in milisecond. To be consistent this animation duration will be applied to both drop and reorder animations.
autoScrollEnabledbooleantrueFirst scrollable parent will scroll automatically if dragging item is close to boundaries.
dragClassstringundefinedClass to be added to the ghost item being dragged. The class will be added after it's added to the DOM so any transition in the class will be applied as intended.
dropClassstringundefinedClass to be added to the ghost item just before the drop animation begins.
removeOnDropOutbooleanundefinedWhen set true onDrop will be called with a removedIndex if you drop element out of any relevant container
dropPlaceholderboolean,objectundefinedOptions for drop placeholder. className, animationDuration, showOnTop
onDragStartfunctionundefinedSee descriptions below
onDragEndfunctionundefinedSee descriptions below
onDropReadyfunctionundefinedSee descriptions below
onDropfunctionundefinedSee descriptions below
getChildPayloadfunctionundefinedSee descriptions below
shouldAnimateDropfunctionundefinedSee descriptions below
shouldAcceptDropfunctionundefinedSee descriptions below
onDragEnterfunctionundefinedSee descriptions below
onDragLeavefunctionundefinedSee descriptions below
getGhostParentfunctionundefinedSee descriptions below
renderfunctionundefinedSee descriptions below

onDragStart

The function to be called by all container when drag start.

function onDragStart({isSource, payload, willAcceptDrop}) {
  ...
}

Parameters

  • isSource : boolean : true if it is called by the container which drag starts from otherwise false.
  • payload : object : the payload object that is returned by getChildPayload function. It will be undefined in case getChildPayload is not set.
  • willAcceptDrop : boolean : true if the dragged item can be dropped into the container, otherwise false.

onDragEnd

The function to be called by all container when drag ends. Called before onDrop function

function onDragEnd({isSource, payload, willAcceptDrop}) {
  ...
}

Parameters

  • isSource : boolean : true if it is called by the container which drag starts from, otherwise false.
  • payload : object : the payload object that is returned by getChildPayload function. It will be undefined in case getChildPayload is not set.
  • willAcceptDrop : boolean : true if the dragged item can be dropped into the container, otherwise false.

onDropReady

The function to be called by the container which is being drag over, when the index of possible drop position changed in container. Basically it is called each time the draggables in a container slides for opening a space for dragged item. dropResult is the only parameter passed to the function which contains the following properties.

function onDropReady(dropResult) {
  const { removedIndex, addedIndex, payload, element } = dropResult;
  ...
}

Parameters

  • dropResult : object
    • removedIndex : number : index of the removed children. Will be null if no item is removed.
    • addedIndex : number : index to add droppped item. Will be null if no item is added.
    • payload : object : the payload object retrieved by calling getChildPayload function.
    • element : DOMElement : the DOM element that is moved

onDrop

The function to be called by any relevant container when drop is over. (After drop animation ends). Source container and any container that could accept drop is considered relevant. dropResult is the only parameter passed to the function which contains the following properties.

function onDrop(dropResult) {
  const { removedIndex, addedIndex, payload, element } = dropResult;
  ...
}

Parameters

  • dropResult : object
    • removedIndex : number : index of the removed children. Will be null if no item is removed.
    • addedIndex : number : index to add droppped item. Will be null if no item is added.
    • payload : object : the payload object retrieved by calling getChildPayload function.
    • element : DOMElement : the DOM element that is moved

getChildPayload

The function to be called to get the payload object to be passed onDrop function.

function getChildPayload(index) {
  return {
    ...
  }
}

Parameters

  • index : number : index of the child item

Returns

  • payload : object

shouldAnimateDrop

The function to be called by the target container to which the dragged item will be droppped. Sometimes dragged item's dimensions are not suitable with the target container and dropping animation can be wierd. So it can be disabled by returning false. If not set drop animations are enabled.

function shouldAnimateDrop(sourceContainerOptions, payload) {
  return false;
}

Parameters

  • sourceContainerOptions : object : options of the source container. (parent container of the dragged item)
  • payload : object : the payload object retrieved by calling getChildPayload function.

Returns

  • boolean : true / false

shouldAcceptDrop

The function to be called by all containers before drag starts to determine the containers to which the drop is possible. Setting this function will override the groupName property and only the return value of this function will be taken into account.

function shouldAcceptDrop(sourceContainerOptions, payload) {
  return true;
}

Parameters

  • sourceContainerOptions : object : options of the source container. (parent container of the dragged item)
  • payload : object : the payload object retrieved by calling getChildPayload function.

Returns

  • boolean : true / false

onDragEnter

The function to be called by the relevant container whenever a dragged item enters its boundaries while dragging.

function onDragEnter() {
  ...
}

onDragLeave

The function to be called by the relevant container whenever a dragged item leaves its boundaries while dragging.

function onDragLeave() {
  ...
}

getGhostParent

The function to be called to get the element that the dragged ghost will be appended. Default parent element is the container itself. The ghost element is positioned as 'fixed' and appended to given parent element. But if any anchestor of container has a transform property, ghost element will be positioned relative to that element which breaks the calculations. Thats why if you have any transformed parent element of Containers you should set this property so that it returns any element that has not transformed parent element.

function getGhostParent() {
  // i.e return document.body;
}

render

By default Container uses a div element for component root. You can define what to render as root element by using render function. If render function is set, children prop of Container will be ignored and return value of the render will be used to render draggables.

function render(ref) {
  // return ReactElement
}

Parameters

  • ref : object : React reference object created by React.createRef()

Returns

  • React Element
<Container
  render={(ref) => {
    return (
      <ul ref={ref}>
        ...place your <Draggable /> components here
      </ul>
    );
  }}
/>

Draggable

Wrapper component for Container's children. Every draggable element should be wrapped with Draggable component.

Make sure to set unique key to Draggable especially when it contains other Container components

Props

PropertyTypeDefaultDescription
renderfunctionundefinedSee descriptions below

render

By default Draggable uses a div element for component root. You can define what to render as root element by using render function. If render function is set, children prop of Draggable will be ignored and return value of the render will be used to render draggable.

<Draggable
  render={() => {
    return <li>...</li>;
  }}
/>

Parameters

  • ref : object : React reference object created by React.createRef()

Returns

  • React Element

版本与发布记录

@edorivai/react-smooth-dnd 共发布 1 个版本,首次发布于 2022/09/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.11.2latest2022/09/084 年前

关于 @edorivai/react-smooth-dnd 的常见问题

@edorivai/react-smooth-dnd 是什么?

Drag and Drop library

如何安装 @edorivai/react-smooth-dnd?

在项目目录运行 npm install @edorivai/react-smooth-dnd,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@edorivai/react-smooth-dnd 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@edorivai/react-smooth-dnd 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@edorivai/react-smooth-dnd 可以商用吗?

@edorivai/react-smooth-dnd 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@edorivai/react-smooth-dnd 的下载量有多少?

根据 npm 官方下载统计,@edorivai/react-smooth-dnd 在 2026/10/13 – 2026/11/11 这 30 天内被下载 10,692 次,日均约 356 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@edorivai/react-smooth-dnd 还在维护吗?

最新版本 0.11.2 发布于 2022/09/08(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/09/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。