Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(user-select): move addUserSelectStyles from handleDragStart to ha… #447

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/DraggableCore.js
Expand Up @@ -297,10 +297,6 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
const shouldUpdate = this.props.onStart(e, coreEvent);
if (shouldUpdate === false || this.mounted === false) return;

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

// Initiate dragging. Set the current x and y as offsets
// so we know how much we've moved during the drag. This allows us
// to drag elements around even if they have been moved, without issue.
Expand Down Expand Up @@ -354,6 +350,13 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
return;
}

const thisNode = ReactDOM.findDOMNode(this);
if (thisNode) {
// Add a style to the body to disable user-select. This prevents text from
// being selected all over the page.
if (this.props.enableUserSelectHack) addUserSelectStyles(thisNode.ownerDocument);
}

this.setState({
lastX: x,
lastY: y
Expand Down
31 changes: 18 additions & 13 deletions specs/draggable.spec.jsx
Expand Up @@ -357,20 +357,22 @@ describe('react-draggable', function () {
});

it('should add and remove transparent selection class', function () {
drag = TestUtils.renderIntoDocument(
<Draggable>
<div />
</Draggable>
);

const node = ReactDOM.findDOMNode(drag);
drag = TestUtils.renderIntoDocument(
<Draggable>
<div />
</Draggable>
);

assert(!document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert(document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseUp(node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
});
const node = ReactDOM.findDOMNode(drag);

assert(!document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
mouseMove(100, 100, node);
assert(document.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseUp(node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
});

it('should not add and remove transparent selection class when disabled', function () {

Expand Down Expand Up @@ -493,6 +495,9 @@ describe('react-draggable', function () {
assert(!iframeDoc.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
assert(!iframeDoc.body.classList.contains('react-draggable-transparent-selection'));
mouseMove(100, 100, node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
assert(iframeDoc.body.classList.contains('react-draggable-transparent-selection'));
TestUtils.Simulate.mouseUp(node);
assert(!document.body.classList.contains('react-draggable-transparent-selection'));
Expand Down