Skip to content

Commit fcf80e1

Browse files
committed
Merge pull request #153 from davidstubbs/master
Only apply user-select hack if drag is started
2 parents f5d003c + ad7bd20 commit fcf80e1

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

example/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ <h1>React Draggable</h1>
135135
<Draggable axis="y" {...dragHandlers}>
136136
<div className="box cursor-y">I can only be dragged vertically</div>
137137
</Draggable>
138+
<Draggable onStart={() => false}>
139+
<div className="box">I don't want to be dragged</div>
140+
</Draggable>
138141
<Draggable onDrag={this.handleDrag} {...dragHandlers}>
139142
<div className="box">
140143
<div>I track my deltas</div>

lib/DraggableCore.es6

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,6 @@ export default class DraggableCore extends React.Component {
199199
this.setState({touchIdentifier: e.targetTouches[0].identifier});
200200
}
201201

202-
// Add a style to the body to disable user-select. This prevents text from
203-
// being selected all over the page.
204-
if (this.props.enableUserSelectHack) addUserSelectStyles();
205-
206202
// Get the current drag point from the event. This is used as the offset.
207203
const {x, y} = getControlPosition(e, this);
208204

@@ -216,6 +212,9 @@ export default class DraggableCore extends React.Component {
216212
const shouldUpdate = this.props.onStart(e, coreEvent);
217213
if (shouldUpdate === false) return;
218214

215+
// Add a style to the body to disable user-select. This prevents text from
216+
// being selected all over the page.
217+
if (this.props.enableUserSelectHack) addUserSelectStyles();
219218

220219
// Initiate dragging. Set the current x and y as offsets
221220
// so we know how much we've moved during the drag. This allows us

specs/draggable.spec.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,24 @@ describe('react-draggable', function () {
301301
TestUtils.Simulate.mouseUp(node);
302302
expect(document.body.getAttribute('style')).toEqual('');
303303
});
304+
305+
it('should not add and remove user-select styles when onStart returns false', function () {
306+
function onStart() { return false; }
307+
308+
drag = TestUtils.renderIntoDocument(
309+
<Draggable onStart={onStart}>
310+
<div />
311+
</Draggable>
312+
);
313+
314+
var node = ReactDOM.findDOMNode(drag);
315+
316+
expect(document.body.getAttribute('style')).toEqual('');
317+
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
318+
expect(document.body.getAttribute('style')).toEqual('');
319+
TestUtils.Simulate.mouseUp(node);
320+
expect(document.body.getAttribute('style')).toEqual('');
321+
});
304322
});
305323

306324
describe('interaction', function () {

0 commit comments

Comments
 (0)