react-dnd-scrollzone
Cross browser compatible scrolling containers for drag and drop interactions.
Basic Example
;;;;;; const ScrollingComponent = ; const ITEMS = 12345678910; { return <DragDropContextProvider backend=HTML5Backend> <ScrollingComponent className="App"> ITEMS </ScrollingComponent> </DragDropContextProvider> ; }
Note: You should replace the original div
you would like to make scrollable with the ScrollingComponent
.
Easing Example
;;;;;; const ScrollZone = ;const linearHorizontalStrength = ;const linearVerticalStrength = ;const ITEMS = 12345678910; // 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] { 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]} { return ;} { return ;} props return <DragDropContextProvider backend=HTML5Backend> <ScrollingComponent className="App" verticalStrength=vStrength horizontalStrength=hStrength > ITEMS </ScrollingComponent> </DragDropContextProvider> ;
Note: You should replace the original div
you would like to make scrollable with the ScrollingComponent
.
Virtualized Example
Since react-dnd-scrollzone 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 for a windowed view where only the visible rows are rendered:
;;;;;;; const ScrollingVirtualList = ; // creates array with 1000 entriesconst ITEMS = Array; props return <DragDropContextProvider backend=HTML5Backend> <ScrollingVirtualList className="App" height=600 width=800 rowCount=ITEMSlength rowHeight=34 rowRenderer= <DragItem key=key style=style label=ITEMSindex /> /> </DragDropContextProvider> ;
API
withScrolling
A React higher order component with the following properties:
const ScrollZone = ; <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 directionverticalStrength
- a function that returns the strength of the vertical scroll directionstrengthMultiplier
- strength multiplier, play around with this (default 30)onScrollChange
- a function that is called whenscrollLeft
orscrollTop
of the component are changed. Called with those two arguments in that order.getScrollContainer
- optional parameter: a function that returns scrolling container (useful for complex custom scroll components). It recieves one argument: root node of wrapped mounted component
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
; const Scrollzone = ;const vStrength = ;const hStrength = ; // zone will scroll when the cursor drags within// 500px of the top/bottom and 300px of the left/rightconst zone = <Scrollzone verticalStrength=vStrength horizontalStrength=hStrength> </Scrollzone>;