# react-dnd-scrolling

> A cross browser solution to scrolling during drag and drop.

Latest version **1.3.10** (published 2024-10-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-dnd-scrolling
pnpm add react-dnd-scrolling
yarn add react-dnd-scrolling
bun add react-dnd-scrolling
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.10 |
| Published | 2024-10-01 |
| First published | 2020-05-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 59.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 60 |
| Maintainers | ttt43ttt |
| Keywords | react, drag, drop, scroll, dnd, drag and drop |

## Links

- npm: https://www.npmjs.com/package/react-dnd-scrolling
- Repository: https://github.com/TechStark/react-dnd-scrolling
- Homepage: https://github.com/TechStark/react-dnd-scrolling#readme
- Issues: http://github.com/TechStark/react-dnd-scrolling/issues
- npm.io page: https://npm.io/package/react-dnd-scrolling

## Dependencies (4)

- [raf](https://npm.io/package/raf.md) ^3.4.1
- [prop-types](https://npm.io/package/prop-types.md) 15.x
- [lodash.throttle](https://npm.io/package/lodash.throttle.md) ^4.1.1
- [hoist-non-react-statics](https://npm.io/package/hoist-non-react-statics.md) 3.x

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.3.10 (latest) — 2024-10-01
- 1.3.9 — 2024-09-30
- 1.3.8 — 2024-05-24
- 1.3.7 — 2024-04-02
- 1.3.6 — 2024-04-01
- 1.3.5 — 2024-04-01
- 1.3.4 — 2024-03-25
- 1.3.3 — 2023-01-14
- 1.3.2 — 2023-01-13
- 1.3.1 — 2023-01-13
- 1.3.0 — 2022-12-15
- 1.2.5 — 2022-12-09
- 1.2.4 — 2022-04-21
- 1.2.2 — 2022-03-30
- 1.2.1 — 2021-07-25
- … 4 more at https://npm.io/package/react-dnd-scrolling/versions

## README

# react-dnd-scrolling

Cross browser compatible scrolling containers for drag and drop interactions.

Works with `react-dnd` `10.x`, `11.x`, `14.x`, `15.x`, `16.x`.

### Build Status

![CI](https://github.com/TechStark/react-dnd-scrolling/workflows/CI/badge.svg)

### Examples

- [Live Demo: Basic](https://codesandbox.io/s/react-dnd-scrolling-demo-vnp66)
- [Live Demo: Mobile](https://codesandbox.io/s/react-dnd-scrolling-mobile-demo-r35bxf)
- [Code Examples](https://github.com/TechStark/react-dnd-scrolling-examples)

| [Basic Example](https://codesandbox.io/s/react-dnd-scrolling-demo-vnp66) | [Mobile Example](https://codesandbox.io/s/react-dnd-scrolling-mobile-demo-r35bxf) |
| ------------------------------------------------------------------------ | --------------------------------------------------------------------------------- |
| <img src="./examples/recording-basic.gif" style="max-height: 500px;">    | <img src="./examples/recording-mobile.gif" style="max-height: 500px;">            |

### Get Started

```bash
npm install react-dnd-scrolling
```

- Using `withScrolling`

```js
import React, { Component } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import withScrolling from 'react-dnd-scrolling';
import DragItem from './DragItem';

const ScrollingComponent = withScrolling('div');

const ITEMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

export default class App extends Component {
  render() {
    return (
      <DndProvider backend={HTML5Backend}>
        <ScrollingComponent className="container">
          {ITEMS.map(n => (
            <DragItem key={n} label={`Item ${n}`} />
          ))}
        </ScrollingComponent>
      </DndProvider>
    );
  }
}
```

Note: You should replace the original `div` you would like to make scrollable with the `ScrollingComponent`.

- Using `useDndScrolling` hook

```js
import React, { useRef } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { useDndScrolling } from 'react-dnd-scrolling';
import DragItem from './DragItem';

const ITEMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const TestPage = () => {
  const ref = useRef();
  useDndScrolling(ref);
  return (
    <div ref={ref} className="container">
      {ITEMS.map(n => (
        <DragItem key={n} label={`Item ${n}`} />
      ))}
    </div>
  );
};

export default function App() {
  return (
    <DndProvider backend={HTML5Backend}>
      <TestPage />
    </DndProvider>
  );
}
```

### Easing Example

```js
import React, { Component } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import withScrolling, { createHorizontalStrength, createVerticalStrength } from 'react-dnd-scrolling';
import DragItem from './DragItem';
import './App.css';


const ScrollZone = withScrolling('ul');
const linearHorizontalStrength = createHorizontalStrength(150);
const linearVerticalStrength = createVerticalStrength(150);
const ITEMS = [1,2,3,4,5,6,7,8,9,10];

// this easing function is from https://gist.github.com/gre/1650294 and
// expects/returns a number between [0, 1], however strength functions
// expects/returns a value between [-1, 1]
function ease(val) {
  const t = (val + 1) / 2; // [-1, 1] -> [0, 1]
  const easedT = t<.5 ? 2*t*t : -1+(4-2*t)*t;
  return easedT * 2 - 1; // [0, 1] -> [-1, 1]
}

function hStrength(box, point) {
  return ease(linearHorizontalStrength(box, point));
}

function vStrength(box, point) {
  return ease(linearVerticalStrength(box, point));
}

export default App(props) {
  return (
    <DndProvider backend={HTML5Backend}>
      <ScrollingComponent
        className="App"
        verticalStrength={vStrength}
        horizontalStrength={hStrength} >

        {ITEMS.map(n => (
          <DragItem key={n} label={`Item ${n}`} />
        ))}
      </ScrollingComponent>
    </DndProvider>
  );
}
```

Note: You should replace the original `div` you would like to make scrollable with the `ScrollingComponent`.

### Virtualized Example

Since react-dnd-scrolling utilizes the Higher Order Components (HOC) pattern, drag and drop scrolling behaviour can easily be added to existing components. For example to speedup huge lists by using [react-virtualized](https://github.com/bvaughn/react-virtualized) for a windowed view where only the visible rows are rendered:

```js
import React from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import withScrolling from 'react-dnd-scrolling';
import { List } from 'react-virtualized';
import DragItem from './DragItem';
import './App.css';

const ScrollingVirtualList = withScrolling(List);

// creates array with 1000 entries
const ITEMS = Array.from(Array(1000)).map((e,i)=> `Item ${i}`);


export default App(props) {
  return (
    <DndProvider backend={HTML5Backend}>
      <ScrollingVirtualList
        className="App"
        height={600}
        width={800}
        rowCount={ITEMS.length}
        rowHeight={34}
        rowRenderer={
          ({ key, index, style }) => (
            <DragItem
              key={key}
              style={style}
              label={ITEMS[index]}
            />
          )
        }
       />
    </DndProvider>
  );
}
```

### API

#### `withScrolling`

A React higher order component with the following properties:

```js
const ScrollZone = withScrolling(String|Component);

<ScrollZone
  strengthMultiplier={Number}
  horizontalStrength={Function}
  verticalStrength={Function}
  onScrollChange={Function} >

  {children}
</Scrollzone>
```

Apply the withScrolling function to any html-identifier ("div", "ul" etc) or react component to add drag and drop scrolling behaviour.

- `horizontalStrength` a function that returns the strength of the horizontal scroll direction
- `verticalStrength` - a function that returns the strength of the vertical scroll direction
- `strengthMultiplier` - strength multiplier, play around with this (default 30)
- `onScrollChange` - a function that is called when `scrollLeft` or `scrollTop` of the component are changed. Called with those two arguments in that order.

The strength functions are both called with two arguments. An object representing the rectangle occupied by the Scrollzone, and an object representing the coordinates of mouse.

They should return a value between -1 and 1.

- Negative values scroll up or left.
- Positive values scroll down or right.
- 0 stops all scrolling.

#### `createVerticalStrength(buffer)` and `createHorizontalStrength(buffer)`

These allow you to create linearly scaling strength functions with a sensitivity different than the default value of 150px.

##### Example

```js
import withScrolling, {
  createVerticalStrength,
  createHorizontalStrength
} from 'react-dnd-scrolling';

const Scrollzone = withScrolling('ul');
const vStrength = createVerticalStrength(500);
const hStrength = createHorizontalStrength(300);

// zone will scroll when the cursor drags within
// 500px of the top/bottom and 300px of the left/right
const zone = (
  <Scrollzone verticalStrength={vStrength} horizontalStrength={hStrength}></Scrollzone>
);
```

---
_Source: https://npm.io/package/react-dnd-scrolling · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
