drag and drop but my text is in the way

I have a webapp where I list a bunch of items, and I decided to make the list drag and drop enabled, but when I try dragging the text gets selected and the drag event doesn't fire unless I already have selected text. How would I make the element drag instead of select the internal text? I tried stopPropagation() and it did nothing, same with preventDefault() The project itself is in svelte, but I added here an ugly rudimentary recreation of this part in vanilla js in a codepen, any help at all is appreciated https://codepen.io/mbaklor/pen/EaxRder?editors=1011
40 Replies
ἔρως
ἔρως4w ago
prevent text selection then just add user-select: none
Jochem
Jochem4w ago
normally I'd say that's a terrible idea, but drag-and-drop things are one of the few situations where it's justified
mic
micOP4w ago
I did that on the list element and now I can't drag anything
ἔρως
ἔρως4w ago
exactly why i suggested this then put the text in a span and add that to the span
mic
micOP4w ago
text selection was the only thing that allowed for some drag events to fire (when dragging selected text)
ἔρως
ἔρως4w ago
eh?
mic
micOP4w ago
I hate drag and drop lol it never does what I want it to one sec
ἔρως
ἔρως4w ago
i just use javascript for it
Chris Bolson
Chris Bolson4w ago
If you add the draggable attribute to the draggble elements (li) and change the JS selector to [draggable] it resolves the issue.
mic
micOP4w ago
mic
micOP4w ago
I am using js for it one sec I'll try
Chris Bolson
Chris Bolson4w ago
at least it did in my fork of your code 🤔
mic
micOP4w ago
.... is this a firefox issue? 😭 can you send the working fork? maybe I did something wrong this didn't help either btw, it still won't fire the drag event without selected text
ἔρως
ἔρως4w ago
just use a pre-existing thing to do this
Chris Bolson
Chris Bolson4w ago
I was just comparing them - I can't see anything obvious.. https://codepen.io/cbolson/pen/QwWxzZ
ἔρως
ἔρως4w ago
you are dragging the text, not the element the drag and drop is for things like files and text, not elements in the page
mic
micOP4w ago
I feel like I'm at the precipice of npm install sortablejsing but I feel like that's overkill
ἔρως
ἔρως4w ago
no, it isnt implementing this type of thing yourself will take you to the depths of insanity and drown you ABSO-FUCKING-LUTELY usr something premade for this save your sanity and profusely thank the kind souls that wrote this for you to use
mic
micOP4w ago
does dragstart not work on divs in general? I was sure it does yea seems like that's the move lmao
ἔρως
ἔρως4w ago
from what ive seen, the html drag and drop works with content being dragged and dropped if you select a div, it should drop html
mic
micOP4w ago
I mean I just need to capture that a drag is happening, the content itself is just the view layer for the user
ἔρως
ἔρως4w ago
mdn explicitly states that you have to select the elements with the mouse aka: select the text and drag
mic
micOP4w ago
that's weird I remember a while ago making a bad freecell game and it was js drag events for divs iirc, idk maybe I did something different back then
ἔρως
ἔρως4w ago
mdn does have an interesting example that does what you want
Chris Bolson
Chris Bolson4w ago
The difference appears to be in the pug where you defined the draggable I have specifically defined li.draggable.label-content(draggable="true") whereas you defined li.draggable.label-content(draggable) So yours compiles to <li class="draggable label-content" draggable="draggable"> rather than <li class="draggable label-content" draggable="true"> If I modify your pug with draggable="true" they drag correctly.
ἔρως
ἔρως4w ago
that last sentencr sentence that is the key i checked the example, you need the attribute and it does automatically what i said: adds user-select: none by default
mic
micOP4w ago
this 100% worked amazing thanks both of you
ἔρως
ἔρως4w ago
you're welcome, sorry i was wrong back there for a second
mic
micOP4w ago
I'll still use a lib because there's no way this is the last time I shoot my foot here lol
ἔρως
ἔρως4w ago
lol i would use one too
mic
micOP4w ago
now to make a valid drop target 😭
ἔρως
ἔρως4w ago
good luck
mic
micOP4w ago
thanks we're getting there my codepen is working my svelte code isn't
ἔρως
ἔρως4w ago
cache?
mic
micOP4w ago
no, things are changing, it just doesn't accept the drop target for some reason might be a svelte issue at this point
ἔρως
ἔρως4w ago
any errors in the console?
mic
micOP4w ago
nothing it gets worse lol it works on the svelte playground this might be a webview issue which is not something I can handle actually
ἔρως
ἔρως4w ago
yeah, that doesnt help at all
mic
micOP4w ago
Update just for the curious, yeah it seems to be a webview issue, I'll see if I can get it moving upstream but in any case as far as my own code goes we're good thanks again for all the help
ἔρως
ἔρως4w ago
you're welcome probably drag and drop is disabled for the web view either a bug in vscode, a preventive measure or something else

Did you find this page helpful?