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
ἔρως
ἔρως7d ago
prevent text selection then just add user-select: none
Jochem
Jochem7d 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
micOP7d ago
I did that on the list element and now I can't drag anything
ἔρως
ἔρως7d ago
exactly why i suggested this then put the text in a span and add that to the span
mic
micOP7d ago
text selection was the only thing that allowed for some drag events to fire (when dragging selected text)
ἔρως
ἔρως7d ago
eh?
mic
micOP7d ago
I hate drag and drop lol it never does what I want it to one sec
ἔρως
ἔρως7d ago
i just use javascript for it
Chris Bolson
Chris Bolson7d ago
If you add the draggable attribute to the draggble elements (li) and change the JS selector to [draggable] it resolves the issue.
mic
micOP7d ago
mic
micOP7d ago
I am using js for it one sec I'll try
Chris Bolson
Chris Bolson7d ago
at least it did in my fork of your code 🤔
mic
micOP7d 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
ἔρως
ἔρως7d ago
just use a pre-existing thing to do this
Chris Bolson
Chris Bolson7d ago
I was just comparing them - I can't see anything obvious.. https://codepen.io/cbolson/pen/QwWxzZ
ἔρως
ἔρως7d 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
micOP7d ago
I feel like I'm at the precipice of npm install sortablejsing but I feel like that's overkill
ἔρως
ἔρως7d 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
micOP7d ago
does dragstart not work on divs in general? I was sure it does yea seems like that's the move lmao
ἔρως
ἔρως7d 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
micOP7d ago
I mean I just need to capture that a drag is happening, the content itself is just the view layer for the user
ἔρως
ἔρως7d ago
mdn explicitly states that you have to select the elements with the mouse aka: select the text and drag
mic
micOP7d 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
ἔρως
ἔρως7d ago
mdn does have an interesting example that does what you want
Chris Bolson
Chris Bolson7d 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.
ἔρως
ἔρως7d 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
micOP7d ago
this 100% worked amazing thanks both of you
ἔρως
ἔρως7d ago
you're welcome, sorry i was wrong back there for a second
mic
micOP7d ago
I'll still use a lib because there's no way this is the last time I shoot my foot here lol
ἔρως
ἔρως7d ago
lol i would use one too
mic
micOP7d ago
now to make a valid drop target 😭
ἔρως
ἔρως7d ago
good luck
mic
micOP7d ago
thanks we're getting there my codepen is working my svelte code isn't
ἔρως
ἔρως7d ago
cache?
mic
micOP7d ago
no, things are changing, it just doesn't accept the drop target for some reason might be a svelte issue at this point
ἔρως
ἔρως7d ago
any errors in the console?
mic
micOP7d 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
ἔρως
ἔρως7d ago
yeah, that doesnt help at all
mic
micOP7d 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
ἔρως
ἔρως7d 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?