Gathering detailed insights and metrics for react-use-draggable-scroll
Gathering detailed insights and metrics for react-use-draggable-scroll
Gathering detailed insights and metrics for react-use-draggable-scroll
Gathering detailed insights and metrics for react-use-draggable-scroll
react-use-draggable-scroll-safe
## This is a fork based on the great work by [rfmiotto](https://github.com/rfmiotto) 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 com
react-draggable
React draggable component
react-remove-scroll
Disables scroll outside of `children` node.
react-remove-scroll-bar
Removes body scroll without content _shake_
npm install react-use-draggable-scroll
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
115 Stars
43 Commits
11 Forks
2 Watching
2 Branches
5 Contributors
Updated on 17 Oct 2024
Minified
Minified + Gzipped
TypeScript (96.93%)
HTML (1.59%)
JavaScript (1.21%)
CSS (0.27%)
Cumulative downloads
Total Downloads
Last day
35.8%
9,920
Compared to previous day
Last week
12.5%
40,155
Compared to previous week
Last month
5.2%
160,142
Compared to previous month
Last year
172.5%
1,954,489
Compared to previous year
1
25
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.
1yarn add react-use-draggable-scroll
1npm install react-use-draggable-scroll
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.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/21 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
50 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More