Gathering detailed insights and metrics for react-use-draggable-scroll-safe
Gathering detailed insights and metrics for react-use-draggable-scroll-safe
Gathering detailed insights and metrics for react-use-draggable-scroll-safe
Gathering detailed insights and metrics for react-use-draggable-scroll-safe
react-use-draggable-scroll
[![NPM Version](https://img.shields.io/npm/v/react-use-draggable-scroll)](https://www.npmjs.com/package/react-use-draggable-scroll)
react-draggable
React draggable component
react-remove-scroll
Disables scroll outside of `children` node.
react-remove-scroll-bar
Removes body scroll without content _shake_
useDraggable is a React hook that allows a wrapping div to be scrolled with the mouse. It adds momentum for a nice UX and has a high performance since it does not rely on any state changes.
npm install react-use-draggable-scroll-safe
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
53 Commits
1 Forks
1 Watching
1 Branches
1 Contributors
Updated on 04 Feb 2024
TypeScript (96.98%)
HTML (1.56%)
JavaScript (1.18%)
CSS (0.27%)
Cumulative downloads
Total Downloads
Last day
-56.1%
90
Compared to previous day
Last week
-2.5%
769
Compared to previous week
Last month
2.7%
3,208
Compared to previous month
Last year
0%
11,148
Compared to previous year
1
25
The main difference is that this version does not crash when passed a null or undefined reference (ref) therefore makes it suitable for use in conditionally rendederd components.
useDraggable is a React hook that allows a wrapping div to have a draggable scroll with an inertial effect. It is completely unstyled and just adds the functionality you are looking for so your application gives the best user experience possible. It works in both x- and y-coordinate directions.
See DEMO.
Differently from other hooks designed for the same purpose, this hook does not rely on any state changes. The functionality is built entirely on event listeners. This means that the wrapping div and its children elements are not re-rendered, resulting in a better performance.
1npm install react-use-draggable-scroll-safe
All you have to do is to create a reference to the wrapping div and pass it as parameter to to the useDraggable hook. The hook is totally unstyled. You can use any library of your choice to style the div and the child components as you would normally do. In the example below, we use TailwindCSS to illustrate.
Just recapping some basics of CSS that you will probably use along with this hook: It is important to set overflow-x: scroll;
property in the CSS of the wrapping div to create the scroll (same goes for y-direction, if that is your case). To prevent a
flex item from growing or shrinking, use the CSS property flex: none;
.
In Javascript:
1import { useRef } from "react"; 2import { useDraggable } from "react-use-draggable-scroll"; 3 4export default function MyComponent() { 5 const ref = useRef(); // We will use React useRef hook to reference the wrapping div: 6 const { events } = useDraggable(ref); // Now we pass the reference to the useDraggable hook: 7 8 return ( 9 <div 10 className="flex max-w-xl space-x-3 overflow-x-scroll scrollbar-hide" 11 {...events} 12 ref={ref} // add reference and events to the wrapping div 13 > 14 <div className="flex-none w-52 h-32 bg-red-200" /> 15 <div className="flex-none w-52 h-32 bg-red-200" /> 16 <div className="flex-none w-52 h-32 bg-red-200" /> 17 <div className="flex-none w-52 h-32 bg-red-200" /> 18 <div className="flex-none w-52 h-32 bg-red-200" /> 19 <div className="flex-none w-52 h-32 bg-red-200" /> 20 <div className="flex-none w-52 h-32 bg-red-200" /> 21 <div className="flex-none w-52 h-32 bg-red-200" /> 22 <div className="flex-none w-52 h-32 bg-red-200" /> 23 </div> 24 ); 25}
In Typescript:
1import { useRef } from "react"; 2import { useDraggable } from "react-use-draggable-scroll"; 3 4export default function MyComponent(): JSX.Element { 5 // We will use React useRef hook to reference the wrapping div: 6 const ref = 7 useRef<HTMLDivElement>() as React.MutableRefObject<HTMLInputElement>; 8 const { events } = useDraggable(ref); // Now we pass the reference to the useDraggable hook: 9 10 return ( 11 <div 12 className="flex max-w-xl space-x-3 overflow-x-scroll scrollbar-hide" 13 {...events} 14 ref={ref} // add reference and events to the wrapping div 15 > 16 <div className="flex-none w-52 h-32 bg-red-200" /> 17 <div className="flex-none w-52 h-32 bg-red-200" /> 18 <div className="flex-none w-52 h-32 bg-red-200" /> 19 <div className="flex-none w-52 h-32 bg-red-200" /> 20 <div className="flex-none w-52 h-32 bg-red-200" /> 21 <div className="flex-none w-52 h-32 bg-red-200" /> 22 <div className="flex-none w-52 h-32 bg-red-200" /> 23 <div className="flex-none w-52 h-32 bg-red-200" /> 24 <div className="flex-none w-52 h-32 bg-red-200" /> 25 </div> 26 ); 27}
activeMouseButton
: Change which mouse button starts a scroll
By default, holding left mouse button will start a scroll. However, you can also use the middle or right mouse button to start a scroll.
Accepts "Left" | "Right" | "Middle
1const { events } = useDraggable(ref, { 2 activeMouseButton?: "Middle"; // Sets which mouse button starts a scroll 3});
applyRubberBandEffect
: Rubber Band Effect
It is possible to toggle a rubber band effect on and off for when the user scrolls past the end of the container. This effect is turned off by default to avoid conflicting CSS style rules in code that uses earlier versions of this hook.
1const { events } = useDraggable(ref, { 2 applyRubberBandEffect: true, // activate rubber band effect 3});
:warning: If you are using rubber band effect: This effect is applied using the
transform
CSS property. User-defined styles can be overridden whenapplyRubberBandEffect
istrue
(default value isfalse
).
decayRate
: Control the decay rate of the inertial effect
You can also control the decay rate of the inertial effect by using an optional parameter. The default value is 0.95, which means that at the speed will decay 5% of its current value at every 1/60 seconds.
1const { events } = useDraggable(ref, { 2 decayRate: 0.96, // specify the decay rate 3});
safeDisplacement
: Control the drag sensitivity
Finally, you can control drag sensitivity by using an optional parameter that states the minimum distance in order to distinguish an intentional drag movement from an unwanted one, which should be instead considered as a click. The default value is 10, which means that when a drag movement travels for 10 pixels or less it is considered unintentional. In this scenario, the drag operation would still be performed, but the closing mouse-up event would still be propagated to the rest of the DOM.
1const { events } = useDraggable(ref, { 2 safeDisplacement: 11, // specify the drag sensitivity 3});
isMounted
: Determine if ref is available or not, default true
In some use cases, such as you need to use useImperativeHandle
,
you will need to wait for the component to be rendered and the Ref to be accessible before
using the useDraggable
hook. In this case, use isMounted
as a controllable switch.
1const { events } = useDraggable(ref, { 2 isMounted: true, 3});
See CONTRIBUTING.md.
No vulnerabilities found.
No security vulnerabilities found.