DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
19.3k stars 735 forks source link

`ondrop` event not getting fired #2222

Open wooden-worm opened 3 months ago

wooden-worm commented 3 months ago

Problem

I'm trying to implement drag & drop on the web, but ondrop is not getting called.

                        ondrop: move |event| {
                            debug!("on drop");
                        },
                        ondragenter: move |event| {
                            event.stop_propagation();
                            debug!("ondragenter");
                        },
                        ondragover: move |event| {
                            event.stop_propagation();
                            debug!("ondragover");
                        },

I think I need to call event.preventDefault() on the JS event (not the Rust event). However, it's not clear to me how to do so.

Expected behavior

ondrop should be called.

Environment:

Questionnaire

JustSomeRandomUsername commented 2 months ago

I had the same problem not long ago. Indeed the problem is that you have to preventDefaults() on the on ondragover event of the target. In Dioxus you do this be setting an attribute on the rsx Element instead of calling a function on the event.

div {
    prevent_default: "ondragover",
}

Last time I checked i didn't find any documentation for this and found out about this attribute after reading a code snipped in a random issue. It seems to be documented now: Event Handlers

wooden-worm commented 2 months ago

I had the same problem not long ago. Indeed the problem is that you have to preventDefaults() on the on ondragover event of the target. In Dioxus you do this be setting an attribute on the rsx Element instead of calling a function on the event.

div {
    prevent_default: "ondragover",
}

Last time I checked i didn't find any documentation for this and found out about this attribute after reading a code snipped in a random issue. It seems to be documented now: Event Handlers

Thank you, that works for me 👍