DioxusLabs / dioxus

Fullstack app framework for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
20.26k stars 775 forks source link

Ability to use capturing event handlers #1011

Open cormac-ainc opened 1 year ago

cormac-ainc commented 1 year ago

Specific Demand

On the web, EventTarget.addEventListener has an extra argument that is either an options object or a boolean. The boolean is known as useCapture, passing true is equivalent to { capture: true }.

When you do this, quoting MDN:

A boolean value indicating whether events of this type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. Events that are bubbling upward through the tree will not trigger a listener designated to use capture. Event bubbling and capturing are two ways of propagating events that occur in an element that is nested within another element, when both elements have registered a handle for that event. The event propagation mode determines the order in which elements receive the event. See DOM Level 3 Events and JavaScript Event order for a detailed explanation. If not specified, useCapture defaults to false.

I wish to be able to set an event handler in dioxus with the capture flag set to true, and have the synthetic event system figure out the details.

Implement Suggestion

EventHandler is already a struct with a call() method, right?. I assume it has a From impl for closures of the right type? If so, just let people construct an EventHandler explicitly and include the options in the constructor (or a builder pattern).

matthunz commented 11 months ago

I'd love to see something like axum's handler fn's here where certain kinds of functions map to different eventhandlers

cormac-ainc commented 11 months ago

I’m looking for the ability to pass a boolean flag, maybe leaving room for a couple more booleans from that options object. A highly generic type-level function argument extractor mechanism would be overkill.