Skip to content

Commit ad7bd20

Browse files
committed
Only apply user-select hack if drag is started
Previously the user-select hack is applied whenever mouseDown on an element matching the selector, with the right kind of click. But if the onStart method returns false then the drag is aborted, but the user-select hack is still applied. Because the drag is not running the hack is not unapplied on mouseUp. This leads to a whole series of ";user-select: none;; user-select: none;; ..." appearing on the body tag and the inability to select text on the page until a valid drag happens. This commit moves the hack application to after the last short circuit to ensure dragging is actually happening.
1 parent f5d003c commit ad7bd20

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)