Gathering detailed insights and metrics for react-draggable
Gathering detailed insights and metrics for react-draggable
Gathering detailed insights and metrics for react-draggable
Gathering detailed insights and metrics for react-draggable
@azabraao/react-draggable
React draggable component
react-draggable-tags
[![Version](https://img.shields.io/npm/v/react-draggable-tags?logo=npm&style=flat-square&color=blue)](https://www.npmjs.com/package/react-draggable-tags) [![Downloads](https://img.shields.io/npm/dm/react-draggable-tags.svg?logo=npm&style=flat-square&color
low-react-draggable
React draggable component
@bokuweb/react-draggable-custom
React draggable component
npm install react-draggable
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
9,040 Stars
508 Commits
1,030 Forks
78 Watching
19 Branches
64 Contributors
Updated on 27 Nov 2024
JavaScript (94.87%)
HTML (1.78%)
TypeScript (1.78%)
Makefile (1.58%)
Cumulative downloads
Total Downloads
Last day
-6.6%
437,957
Compared to previous day
Last week
3.4%
2,325,132
Compared to previous week
Last month
10.5%
9,909,628
Compared to previous month
Last year
-7.2%
109,809,760
Compared to previous year
2
42
A simple component for making elements draggable.
1<Draggable> 2 <div>I can now be moved around!</div> 3</Draggable>
Version | Compatibility |
---|---|
4.x | React 16.3+ |
3.x | React 15-16 |
2.x | React 0.14 - 15 |
1.x | React 0.13 - 0.14 |
0.x | React 0.10 - 0.13 |
1$ npm install react-draggable
If you aren't using browserify/webpack, a
UMD version of react-draggable is available. It is updated per-release only.
This bundle is also what is loaded when installing from npm. It expects external React
and ReactDOM
.
If you want a UMD version of the latest master
revision, you can generate it yourself from master by cloning this
repository and running $ make
. This will create umd dist files in the dist/
folder.
The default export is <Draggable>
. At the .DraggableCore
property is <DraggableCore>
.
Here's how to use it:
1// ES6 2import Draggable from 'react-draggable'; // The default 3import {DraggableCore} from 'react-draggable'; // <DraggableCore> 4import Draggable, {DraggableCore} from 'react-draggable'; // Both at the same time 5 6// CommonJS 7let Draggable = require('react-draggable'); 8let DraggableCore = Draggable.DraggableCore;
<Draggable>
A <Draggable>
element wraps an existing element and extends it with new event handlers and styles.
It does not create a wrapper element in the DOM.
Draggable items are moved using CSS Transforms. This allows items to be dragged regardless of their current positioning (relative, absolute, or static). Elements can also be moved between drags without incident.
If the item you are dragging already has a CSS Transform applied, it will be overwritten by <Draggable>
. Use
an intermediate wrapper (<Draggable><span>...</span></Draggable>
) in this case.
View the Demo and its source for more.
1import React from 'react'; 2import ReactDOM from 'react-dom'; 3import Draggable from 'react-draggable'; 4 5class App extends React.Component { 6 7 eventLogger = (e: MouseEvent, data: Object) => { 8 console.log('Event: ', e); 9 console.log('Data: ', data); 10 }; 11 12 render() { 13 return ( 14 <Draggable 15 axis="x" 16 handle=".handle" 17 defaultPosition={{x: 0, y: 0}} 18 position={null} 19 grid={[25, 25]} 20 scale={1} 21 onStart={this.handleStart} 22 onDrag={this.handleDrag} 23 onStop={this.handleStop}> 24 <div> 25 <div className="handle">Drag from here</div> 26 <div>This readme is really dragging on...</div> 27 </div> 28 </Draggable> 29 ); 30 } 31} 32 33ReactDOM.render(<App/>, document.body);
The <Draggable/>
component transparently adds draggability to its children.
Note: Only a single child is allowed or an Error will be thrown.
For the <Draggable/>
component to correctly attach itself to its child, the child element must provide support
for the following props:
style
is used to give the transform css to the child.className
is used to apply the proper classes to the object being dragged.onMouseDown
, onMouseUp
, onTouchStart
, and onTouchEnd
are used to keep track of dragging state.React.DOM elements support the above properties by default, so you may use those elements as children without any changes. If you wish to use a React component you created, you'll need to be sure to transfer prop.
<Draggable>
Props:1// 2// Types: 3// 4type DraggableEventHandler = (e: Event, data: DraggableData) => void | false; 5type DraggableData = { 6 node: HTMLElement, 7 // lastX + deltaX === x 8 x: number, y: number, 9 deltaX: number, deltaY: number, 10 lastX: number, lastY: number 11}; 12 13// 14// Props: 15// 16{ 17// If set to `true`, will allow dragging on non left-button clicks. 18allowAnyClick: boolean, 19 20// Default `false` and default behavior before 4.5.0. 21// If set to `true`, the 'touchstart' event will not be prevented, 22// which will allow scrolling inside containers. We recommend 23// using the 'handle' / 'cancel' props when possible instead of enabling this. 24// 25// See https://github.com/react-grid-layout/react-draggable/issues/728 26allowMobileScroll: boolean, 27 28// Determines which axis the draggable can move. This only affects 29// flushing to the DOM. Callbacks will still include all values. 30// Accepted values: 31// - `both` allows movement horizontally and vertically (default). 32// - `x` limits movement to horizontal axis. 33// - `y` limits movement to vertical axis. 34// - 'none' stops all movement. 35axis: string, 36 37// Specifies movement boundaries. Accepted values: 38// - `parent` restricts movement within the node's offsetParent 39// (nearest node with position relative or absolute), or 40// - a selector, restricts movement within the targeted node 41// - An object with `left, top, right, and bottom` properties. 42// These indicate how far in each direction the draggable 43// can be moved. 44bounds: {left?: number, top?: number, right?: number, bottom?: number} | string, 45 46// Specifies a selector to be used to prevent drag initialization. The string is passed to 47// Element.matches, so it's possible to use multiple selectors like `.first, .second`. 48// Example: '.body' 49cancel: string, 50 51// Class names for draggable UI. 52// Default to 'react-draggable', 'react-draggable-dragging', and 'react-draggable-dragged' 53defaultClassName: string, 54defaultClassNameDragging: string, 55defaultClassNameDragged: string, 56 57// Specifies the `x` and `y` that the dragged item should start at. 58// This is generally not necessary to use (you can use absolute or relative 59// positioning of the child directly), but can be helpful for uniformity in 60// your callbacks and with css transforms. 61defaultPosition: {x: number, y: number}, 62 63// If true, will not call any drag handlers. 64disabled: boolean, 65 66// Default `true`. Adds "user-select: none" while dragging to avoid selecting text. 67enableUserSelectHack: boolean, 68 69// Specifies the x and y that dragging should snap to. 70grid: [number, number], 71 72// Specifies a selector to be used as the handle that initiates drag. 73// Example: '.handle' 74handle: string, 75 76// If desired, you can provide your own offsetParent for drag calculations. 77// By default, we use the Draggable's offsetParent. This can be useful for elements 78// with odd display types or floats. 79offsetParent: HTMLElement, 80 81// Called whenever the user mouses down. Called regardless of handle or 82// disabled status. 83onMouseDown: (e: MouseEvent) => void, 84 85// Called when dragging starts. If `false` is returned any handler, 86// the action will cancel. 87onStart: DraggableEventHandler, 88 89// Called while dragging. 90onDrag: DraggableEventHandler, 91 92// Called when dragging stops. 93onStop: DraggableEventHandler, 94 95// If running in React Strict mode, ReactDOM.findDOMNode() is deprecated. 96// Unfortunately, in order for <Draggable> to work properly, we need raw access 97// to the underlying DOM node. If you want to avoid the warning, pass a `nodeRef` 98// as in this example: 99// 100// function MyComponent() { 101// const nodeRef = React.useRef(null); 102// return ( 103// <Draggable nodeRef={nodeRef}> 104// <div ref={nodeRef}>Example Target</div> 105// </Draggable> 106// ); 107// } 108// 109// This can be used for arbitrarily nested components, so long as the ref ends up 110// pointing to the actual child DOM node and not a custom component. 111// 112// For rich components, you need to both forward the ref *and props* to the underlying DOM 113// element. Props must be forwarded so that DOM event handlers can be attached. 114// For example: 115// 116// const Component1 = React.forwardRef(function (props, ref) { 117// return <div {...props} ref={ref}>Nested component</div>; 118// }); 119// 120// const nodeRef = React.useRef(null); 121// <DraggableCore onDrag={onDrag} nodeRef={nodeRef}> 122// <Component1 ref={nodeRef} /> 123// </DraggableCore> 124// 125// Thanks to react-transition-group for the inspiration. 126// 127// `nodeRef` is also available on <DraggableCore>. 128nodeRef: React.Ref<typeof React.Component>, 129 130// Much like React form elements, if this property is present, the item 131// becomes 'controlled' and is not responsive to user input. Use `position` 132// if you need to have direct control of the element. 133position: {x: number, y: number} 134 135// A position offset to start with. Useful for giving an initial position 136// to the element. Differs from `defaultPosition` in that it does not 137// affect the position returned in draggable callbacks, and in that it 138// accepts strings, like `{x: '10%', y: '10%'}`. 139positionOffset: {x: number | string, y: number | string}, 140 141// Specifies the scale of the canvas your are dragging this element on. This allows 142// you to, for example, get the correct drag deltas while you are zoomed in or out via 143// a transform or matrix in the parent of this element. 144scale: number 145}
Note that sending className
, style
, or transform
as properties will error - set them on the child element
directly.
<Draggable>
is a 'batteries-included' component that manages its own state. If you want to completely
control the lifecycle of the component, use <DraggableCore>
.
For some users, they may want the nice state management that <Draggable>
provides, but occasionally want
to programmatically reposition their components. <Draggable>
allows this customization via a system that
is similar to how React handles form components.
If the prop position: {x: number, y: number}
is defined, the <Draggable>
will ignore its internal state and use
the provided position instead. Alternatively, you can seed the position using defaultPosition
. Technically, since
<Draggable>
works only on position deltas, you could also seed the initial position using CSS top/left
.
We make one modification to the React philosophy here - we still allow dragging while a component is controlled.
We then expect you to use at least an onDrag
or onStop
handler to synchronize state.
To disable dragging while controlled, send the prop disabled={true}
- at this point the <Draggable>
will operate
like a completely static component.
<DraggableCore>
For users that require absolute control, a <DraggableCore>
element is available. This is useful as an abstraction
over touch and mouse events, but with full control. <DraggableCore>
has no internal state.
See React-Resizable and React-Grid-Layout for some usage examples.
<DraggableCore>
is a useful building block for other libraries that simply want to abstract browser-specific
quirks and receive callbacks when a user attempts to move an element. It does not set styles or transforms
on itself and thus must have callbacks attached to be useful.
<DraggableCore>
takes a limited subset of options:
1{ 2 allowAnyClick: boolean, 3 allowMobileScroll: boolean, 4 cancel: string, 5 disabled: boolean, 6 enableUserSelectHack: boolean, 7 offsetParent: HTMLElement, 8 grid: [number, number], 9 handle: string, 10 onStart: DraggableEventHandler, 11 onDrag: DraggableEventHandler, 12 onStop: DraggableEventHandler, 13 onMouseDown: (e: MouseEvent) => void, 14 scale: number 15}
Note that there is no start position. <DraggableCore>
simply calls drag
handlers with the below parameters,
indicating its position (as inferred from the underlying MouseEvent) and deltas. It is up to the parent
to set actual positions on <DraggableCore>
.
Drag callbacks (onStart
, onDrag
, onStop
) are called with the same arguments as <Draggable>
.
$ npm run dev
$ npm test
make release-patch
, make release-minor
, or make-release-major
make publish
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 12/30 approved changesets -- score normalized to 4
Reason
1 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
security policy file not detected
Details
Reason
project is not fuzzed
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
28 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