Skip to content

Commit 40d94c4

Browse files
committed
First commit
0 parents  commit 40d94c4

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": ["react", "es2015"],
3+
"plugins": [
4+
"transform-object-rest-spread",
5+
"transform-react-jsx"
6+
]
7+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/package-lock.json

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v6.10

build/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "react-drag-element",
3+
"version": "0.0.1",
4+
"description": "Put a description here",
5+
"main": "build/index.js",
6+
"peerDependencies": {
7+
"react": "16.6.1"
8+
},
9+
"dependencies": {
10+
"prop-types": "^15.6.2",
11+
"react": "16.6.1",
12+
"webpack": "4.25.1"
13+
},
14+
"scripts": {
15+
"test": "echo \"Error: no test specified\" && exit 1",
16+
"start": "webpack --watch",
17+
"build": "webpack"
18+
},
19+
"author": {
20+
"name": "Akash Anand",
21+
"email": "[email protected]"
22+
},
23+
"devDependencies": {
24+
"babel-cli": "^6.26.0",
25+
"babel-preset-es2015": "^6.24.1",
26+
"babel-core": "^6.24.1",
27+
"babel-loader": "^7.0.0",
28+
"babel-plugin-transform-class-properties": "^6.24.1",
29+
"babel-plugin-transform-object-rest-spread": "^6.23.0",
30+
"babel-plugin-transform-react-jsx": "^6.24.1",
31+
"babel-preset-env": "^1.5.1",
32+
"babel-preset-react": "^6.24.1",
33+
"webpack-cli": "^3.1.2"
34+
}
35+
}

src/index.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const HORIZONTAL = 'horizontal',
5+
VERTICAL = 'vertical';
6+
class CustomDrag extends Component {
7+
constructor(props) {
8+
super(props);
9+
this.state = {
10+
pos1: 0,
11+
pos2: 0,
12+
pos3: 0,
13+
pos4: 0,
14+
top: 0,
15+
left: 0,
16+
};
17+
18+
}
19+
componentDidMount() {
20+
let { dragId, dragItemId } = this.props;
21+
if (dragId !== undefined && dragId !== '' && dragId !== null &&
22+
dragItemId !== undefined && dragItemId !== '' && dragItemId !== null) {
23+
let elmnt = document.getElementById(`${dragItemId}`);
24+
let dragIdElement = document.getElementById(`${dragId}`);
25+
if (elmnt && dragIdElement) {
26+
this.dragElement(elmnt, dragIdElement);
27+
}
28+
}
29+
}
30+
dragElement = (elmnt, dragIdElement) => {
31+
if (dragIdElement && elmnt) {
32+
/* if present, the header is where you move the DIV from:*/
33+
dragIdElement.onmousedown = (e) => this.dragMouseDown(e, elmnt);
34+
}
35+
}
36+
37+
dragMouseDown = (e, elmnt) => {
38+
let { pos1, pos2, pos3, pos4 } = this.state;
39+
e = e || window.event;
40+
e.preventDefault();
41+
// get the mouse cursor position at startup:
42+
pos3 = e.clientX;
43+
pos4 = e.clientY;
44+
document.onmouseup = this.closeDragElement;
45+
// call a function whenever the cursor moves:
46+
document.onmousemove = (e) => this.elementDrag(e, elmnt);
47+
this.setState({ pos1, pos2, pos3, pos4 });
48+
}
49+
50+
elementDrag = (e, elmnt) => {
51+
let { pos1, pos2, pos3, pos4 } = this.state;
52+
e = e || window.event;
53+
e.preventDefault();
54+
// calculate the new cursor position:
55+
pos1 = pos3 - e.clientX;
56+
pos2 = pos4 - e.clientY;
57+
pos3 = e.clientX;
58+
pos4 = e.clientY;
59+
// set the element's new position:
60+
let offsetTop = elmnt.offsetTop;
61+
let offsetLeft = elmnt.offsetLeft;
62+
if (offsetTop < 0) {
63+
offsetTop = 0;
64+
}
65+
if (offsetLeft < 0) {
66+
offsetLeft = 0;
67+
}
68+
let top = (offsetTop - pos2),
69+
left = (offsetLeft - pos1);
70+
let position = {};
71+
let { draggingType } = this.props;
72+
switch (draggingType) {
73+
case HORIZONTAL:
74+
position = { top: null, left };
75+
elmnt.style.left = (offsetLeft - pos1) + 'px';
76+
break;
77+
case VERTICAL:
78+
position = { top, left: null };
79+
elmnt.style.top = (offsetTop - pos2) + 'px';
80+
break;
81+
default:
82+
position = { top, left };
83+
elmnt.style.top = (offsetTop - pos2) + 'px';
84+
elmnt.style.left = (offsetLeft - pos1) + 'px';
85+
break;
86+
}
87+
this.setState({ pos1, pos2, pos3, pos4, top, left });
88+
this.props.getPositions(position)
89+
}
90+
91+
closeDragElement = () => {
92+
/* stop moving when mouse button is released:*/
93+
document.onmouseup = null;
94+
document.onmousemove = null;
95+
}
96+
97+
render() {
98+
return (
99+
<div id="custom-drag-id">
100+
{this.props.children}
101+
</div>
102+
);
103+
}
104+
}
105+
CustomDrag.defaultProps = {
106+
draggingType: 'RANDOM',
107+
getPositions: () => { }
108+
};
109+
CustomDrag.propTypes = {
110+
draggingType: PropTypes.string,
111+
getPositions: PropTypes.func,
112+
dragId: PropTypes.string.isRequired,
113+
dragItemId: PropTypes.string.isRequired
114+
};
115+
116+
export default CustomDrag;

webpack.config.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var path = require('path');
2+
module.exports = {
3+
entry: './src/index.js',
4+
output: {
5+
path: path.resolve(__dirname, 'build'),
6+
filename: 'index.js',
7+
libraryTarget: 'commonjs2' // THIS IS THE MOST IMPORTANT LINE! :mindblow: I wasted more than 2 days until realize this was the line most important in all this guide.
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.jsx?$/,
13+
include: path.resolve(__dirname, 'src'),
14+
exclude: /(node_modules|bower_components|build)/,
15+
use: {
16+
loader: 'babel-loader',
17+
options: {
18+
presets: ['react', 'es2015'],
19+
plugins: ['transform-class-properties']
20+
}
21+
}
22+
}
23+
]
24+
},
25+
externals: {
26+
'react': 'commonjs react' // this line is just to use the React dependency of our parent-testing-project instead of using our own React.
27+
}
28+
};

0 commit comments

Comments
 (0)