dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

Add support for attaching event listeners to the global window

Open vultix opened this issue 1 year ago • 2 comments

Feature Request

It's common to want to attach event listeners to the global window. For example, a custom slider element will want to listen to mousedown locally on the slider, but mousemove globally on the window.

Svelte has a svelte:window special element for exactly these use cases.

Implement Suggestion

Taking a cue from svelte, we could add something like a window special element that events can be attached to:

rsx! {
    dioxus:window {
        onmousemove: move |event| handle_mousemove(event)
    }
    div {
        id: "slider",
        onmousedown: move |event| handle_mousedown(event),
        ...
    }
}

We could also introduce a new use_window_event hook or something similar.

vultix avatar May 10 '24 15:05 vultix

On the desktop platform there is use_wry_event_handler. We use it in SDK here. I don't believe anything similar exists for Web though, aside from using the web-sys crate.

DogeDark avatar May 11 '24 01:05 DogeDark

There is some overlap with https://github.com/DioxusLabs/dioxus/issues/628, and https://github.com/DioxusLabs/dioxus/issues/1169. Some way to attach attributes/listeners to somewhere outside of children in Dioxus unlocks a bunch of new usecases

ealmloff avatar May 14 '24 14:05 ealmloff