question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IMPORTANT: draggable DOM attribute blocks text selection

See original GitHub issue

Describe the bug If you pass down the drag property to the ref attribute of your DOM element, then a draggable native html attribute will be set to true. It doesn’t matter if canDrag is set to false. The draggable HTML attribute is always true and because of this, the user cannot select a text in this component.

Reproduction https://codesandbox.io/s/quirky-chaplygin-89xqe

Steps to reproduce the behavior: Just try to select the text “My Draggable component”. You will see that this doesn’t work. Afterwards you can take a look into the MyDraggableComponent.tsx. As you can see, the canDrag option is set to false.

Expected behavior If canDrag is set to false, I expected that the HTML attribute draggable is set to false.

Screenshots image

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36
  • Version react-dnd: latest

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maclockardcommented, Sep 22, 2020

Related https://github.com/react-dnd/react-dnd/issues/2565 I also tested that this prevents text selection in both chrome and safari as well.

~Another workaround that’s slightly more declarative is to set draggable={false} on a container element lower down then the drag element.~

EDIT: my workaround does not work. ended up just conditionally attaching drag ref depending on if it wanted something to be draggable.

1reaction
ffjanhoeckcommented, Jul 25, 2020

Edit 2: If anyone have the same problem: I have found a workaround for this. It does fix the issue, but isn’t a recommended way.

useLayoutEffect(() => {
    if (rootRef.current) {
        if (disabled) {
            rootRef.current.setAttribute('draggable', 'false');
        } else {
            rootRef.current.setAttribute('draggable', 'true');
        }
    }
}, [disabled, rootRef.current]);
Read more comments on GitHub >

github_iconTop Results From Across the Web

draggable - HTML: HyperText Markup Language | MDN
The draggable global attribute is an enumerated attribute that indicates whether the element can be dragged, either with native browser ...
Read more >
Using the HTML5 Drag and Drop API - web.dev
The HTML5 Drag and Drop API means that we can make almost any element on our page draggable. In this post we'll explain...
Read more >
7.7 Drag and drop — HTML5
The event handler typically needs to check that it's not a text selection that is being dragged, and then needs to store data...
Read more >
How To Create a Draggable HTML Element
Learn how to create a draggable HTML element with JavaScript and CSS. ... The only important style is position: absolute , the rest...
Read more >
Recommended way of making React component/div ...
@ssorallen pointed out that because draggable is more of an attribute than ... to disable text selection during drag and drop // operations...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found