-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Element.current_target()
always points to the HtmlBodyElement
#2572
Comments
I'd suggest to use |
Just want to clarify that this is expected behaviour and there are no near-future plans to change this. We could introduce synthetic event types to limit the amount of implementation details we expose of the internal event handling, but that'd break more existing components than help, imo. Actionable items: improve event handling documentation and explain what and what is expected to work "like normal". For example, I think it's also not mentioned, that all events are caught in the "capture" phase, currently, and the bubbling behaviour is purely virtual. This can be confusing if you're working with manually registered event handlers also. Apart from that, the event passed to handlers is if installed on the root element (either nearest portal, or the app root). In this sense, handlers are "grouped", the order of handlers is as if they are run during the bubbling phase, from bottom to top. Capture handlers are not support currently. |
Thank you, that's got me another good step further. To complete my full use case, I'm actually trying to reference elements which are generated programatically in a loop, something like: html! {
list_of_things.iter().map(|thing: &String| html! {
<li ref={???)}>{thing}</li>
}).collect::<Html>()
} Does |
It's less clear, but one approach could be to create a component for the inner things. #[derive(PartialEq, Properties)]
struct ThingProps { thing: String }
#[function_component]
fn Thing(props: ThingProps) -> Html {
let ref = use_node_ref();
html! {
<li ref={ref}>{props.thing}</li>
}
}
...
list_of_things.iter().map(|thing: &String| html! {
<Thing {thing} />
}).collect::<Html>() |
Environment:
wasm32-unknown-unknown
trunk
I spent a long time going around in circles trying to get a handle for the html element I'm interacting with so that I can do some further processing. After a lot of print-debugging, I realised that whenever an event is received,
current_target()
appears to point to theHtmlBodyElement
.I would expect it to return a handle to the parent element from which the event was fired.
I reported it over at rustwasm #2849, but to save a click, the maintainer said:
Here is a minimal yew app:
Here is the Cargo.toml:
In the app I'm developing, I noticed this behaviour on an
<svg>
element, to which I had added a click handler (i.e. aMouseEvent
). The<svg>
element has several<path>
element children.Element.target()
correctly gives me either the<svg>
or the<path>
elements, but I specifically always want the<svg>
element, which is what I would expectcurrent_target()
to do.The text was updated successfully, but these errors were encountered: