GithubHelp home page GithubHelp logo

nosferatu500 / react-dnd-scrollzone Goto Github PK

View Code? Open in Web Editor NEW

This project forked from frontend-collective/frontend-collective-react-dnd-scrollzone

2.0 2.0 2.0 2.13 MB

A smooth scrolling container for draggable items

Home Page: https://npmjs.com/@nosferatu500/react-dnd-scrollzone

License: MIT License

JavaScript 19.72% TypeScript 80.28%
dnd-scroll scroll-zone

react-dnd-scrollzone's Introduction

react-beautifull-dnd-scrollzone

Forked from https://github.com/azuqua/react-dnd-scrollzone with support for react-dnd@16.

Cross browser compatible scrolling containers for drag and drop interactions.

import React from 'react'
import { HTML5Backend } from 'react-dnd-html5-backend'
import { DndProvider } from 'react-dnd'
import withScrolling, {createVerticalStrength, createHorizontalStrength} from '@nosferatu500/react-dnd-scrollzone'
import {DragItem} from './DragItem'
import './App.css'

const ScrollingComponent = withScrolling(
  React.forwardRef((props, ref) => {
    const {dragDropManager, ...otherProps} = props;
    return <div ref={ref} {...otherProps} />
  }))

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

const verticalStrength = createVerticalStrength(150);
const horizontalStrength = createHorizontalStrength(150);

const App = () => (
  <DndProvider backend={HTML5Backend}>
    <ScrollingComponent className="App" verticalStrength={verticalStrength} horizontalStrength={horizontalStrength}>
      {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.

Example
import withScrolling, { createVerticalStrength, createHorizontalStrength } from '@nosferatu500/react-dnd-scrollzone';

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>
);

API

withScrolling

A React higher order component with the following properties:

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.

react-dnd-scrollzone's People

Contributors

nosferatu500 avatar nickclaw avatar wuweiweiwu avatar dependabot[bot] avatar dolezel avatar gulderov avatar criticerz avatar mattkrick avatar mathieuu avatar fritz-c avatar jas-per avatar

Stargazers

Ricardo Canelas avatar Igor Matias avatar

Watchers

James Cloos avatar Constantin Ehrensberger avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.