Skip to content

Commit 464e74e

Browse files
committed
Adding support for drag handle
1 parent e19644a commit 464e74e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

lib/draggable.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ function canDragX(draggable) {
2424
draggable.props.axis === 'x';
2525
}
2626

27+
function isFunction(fn) {
28+
return typeof fn === 'function';
29+
}
30+
31+
function matchesSelector(el, selector) {
32+
if (isFunction(el.matches)) {
33+
return el.matches(selector);
34+
} else if (isFunction(el.webkitMatchesSelector)) {
35+
return el.webkitMatchesSelector(selector);
36+
} else if (isFunction(el.mozMatchesSelector)) {
37+
return el.mozMatchesSelector(selector);
38+
} else if (isFunction(el.msMatchesSelector)) {
39+
return el.msMatchesSelector(selector);
40+
} else if (isFunction(el.oMatchesSelector)) {
41+
return el.oMatchesSelector(selector);
42+
} else if (isFunction(el.webkitMatchesSelector)) {
43+
return el.webkitMatchesSelector(selector);
44+
}
45+
}
46+
2747
module.exports = React.createClass({
2848
displayName: 'Draggable',
2949

@@ -39,6 +59,28 @@ module.exports = React.createClass({
3959
*/
4060
axis: React.PropTypes.oneOf(['both', 'x', 'y']),
4161

62+
/**
63+
* `handle` specifies a selector to be used as the handle that initiates drag.
64+
*
65+
* Example:
66+
*
67+
* ```jsx
68+
* var App = React.createClass({
69+
* render: function () {
70+
* return (
71+
* <Draggable handle="h2">
72+
* <div>
73+
* <h2>Click me to drag</h2>
74+
* <div>This is some other content</div>
75+
* </div>
76+
* </Draggable>
77+
* );
78+
* }
79+
* });
80+
* ```
81+
*/
82+
handle: React.PropTypes.string,
83+
4284
/**
4385
* Called when dragging starts.
4486
*
@@ -103,6 +145,7 @@ module.exports = React.createClass({
103145
getDefaultProps: function () {
104146
return {
105147
axis: 'both',
148+
handle: null,
106149
onStart: emptyFunction,
107150
onDrag: emptyFunction,
108151
onStop: emptyFunction
@@ -126,6 +169,11 @@ module.exports = React.createClass({
126169

127170
handleMouseDown: function (e) {
128171
var node = this.getDOMNode();
172+
173+
if (this.props.handle && !matchesSelector(e.target, this.props.handle)) {
174+
return;
175+
}
176+
129177
this.setState({
130178
offsetX: e.clientX,
131179
offsetY: e.clientY,

0 commit comments

Comments
 (0)