Skip to content

Commit c1c936c

Browse files
committed
Move user-select:none hack to body from element.
Prevents ugly text selection if you move the mouse too quickly.
1 parent 2fd9744 commit c1c936c

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

lib/draggable.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,8 @@ function snapToGrid(grid, pendingX, pendingY) {
132132
}
133133

134134
// Useful for preventing blue highlights all over everything when dragging.
135-
var userSelectStyle = {
136-
WebkitUserSelect: 'none',
137-
MozUserSelect: 'none',
138-
msUserSelect: 'none',
139-
OUserSelect: 'none',
140-
userSelect: 'none',
141-
};
135+
var userSelectStyle = ';user-select: none;-webkit-user-select:none;-moz-user-select:none;' +
136+
'-o-user-select:none;-ms-user-select:none;';
142137

143138
function createCSSTransform(style) {
144139
if (!style.x && !style.y) return {};
@@ -178,6 +173,13 @@ module.exports = React.createClass({
178173
*/
179174
axis: React.PropTypes.oneOf(['both', 'x', 'y']),
180175

176+
/**
177+
* By default, we add 'user-select:none' attributes to the document body
178+
* to prevent ugly text selection during drag. If this is causing problems
179+
* for your app, set this to `false`.
180+
*/
181+
enableUserSelectHack: React.PropTypes.bool,
182+
181183
/**
182184
* `handle` specifies a selector to be used as the handle that initiates drag.
183185
*
@@ -340,6 +342,7 @@ module.exports = React.createClass({
340342
cancel: null,
341343
grid: null,
342344
zIndex: NaN,
345+
enableUserSelectHack: true,
343346
onStart: emptyFunction,
344347
onDrag: emptyFunction,
345348
onStop: emptyFunction,
@@ -377,6 +380,13 @@ module.exports = React.createClass({
377380
return;
378381
}
379382

383+
// Add a style to the body to disable user-select. This prevents text from
384+
// being selected all over the page.
385+
if (this.props.enableUserSelectHack) {
386+
var style = document.body.getAttribute('style') || '';
387+
document.body.setAttribute('style', style + userSelectStyle);
388+
}
389+
380390
var dragPoint = getControlPosition(e);
381391

382392
// Initiate dragging. Set the current x and y as offsets
@@ -402,6 +412,12 @@ module.exports = React.createClass({
402412
return;
403413
}
404414

415+
// Remove user-select styles.
416+
if (this.props.enableUserSelectHack) {
417+
var style = document.body.getAttribute('style') || '';
418+
document.body.setAttribute('style', style.replace(userSelectStyle, ''));
419+
}
420+
405421
// Turn off dragging
406422
this.setState({
407423
dragging: false
@@ -458,7 +474,7 @@ module.exports = React.createClass({
458474
this.state.clientY :
459475
0
460476
});
461-
var style = assign({}, userSelectStyle, childStyle, transform);
477+
var style = assign({}, childStyle, transform);
462478

463479
// Set zIndex if currently dragging and prop has been provided
464480
if (this.state.dragging && !isNaN(this.props.zIndex)) {

0 commit comments

Comments
 (0)