Scroll your elements by dragging your mouse
You can see the simplest demo here: Live demo
$ npm install --save react-scroll-ondragRun examples:
$ npm ci
$ cd examples
$ npm ci
$ npm startimport{useRef}from'react';importuseScrollOnDragfrom'react-scroll-ondrag';constApp=()=>{constref=useRef();const{ events }=useScrollOnDrag(ref);return<div{...events}ref={ref}/>;};Type: a React ref, required
A ref to the DOM element whose scroll position you want to control
Type: object
Type: function: ({ dx: Integer, dy: Integer }) => void
Default:
// ref is the first argument to the hook, documented above({ dx, dy })=>{constmaxHorizontalScroll=dom=>dom.scrollWidth-dom.clientWidth;constmaxVerticalScroll=dom=>dom.scrollHeight-dom.clientHeight;constoffsetX=Math.min(maxHorizontalScroll(ref.current),ref.current.scrollLeft+dx);ref.current.scrollLeft=offsetX;// eslint-disable-line no-param-reassignconstoffsetY=Math.min(maxVerticalScroll(ref.current),ref.current.scrollTop+dy);ref.current.scrollTop=offsetY;// eslint-disable-line no-param-reassign}Used to customize scroll, i.e., scroll only in horizontal direction, change scroll speed, etc
Type: function: () => void
Called when scrolling by dragging starts
Type: function: () => void
Called when scrolling by dragging ends
Type: object, shape: { events: { onMouseDown } }
An object with the events to inject to the controlled element.
See the LICENSE file for license rights and limitations (MIT).