How to stop events bubbling/propagating #2896
-
I've written a modal component with a background By default, clicking on the modal body propagates the click event up to the background I can turn of event bubbling globally with yew::events::set_event_bubbling(false); which has the desired effect for this single interaction, but is a complete sledgehammer and breaks other interactions. What's the cannonical way to do the equivalent of <div onclick="event.preventDefault()">blah</div> on a component/element rendered from My component view is like this: fn view(&self, ctx: &Context<Self>) -> Html {
let bg_on_click = ctx.link().callback(|_| Msg::BgClicked);
html! {
<div class="search-modal-bg" onclick={ bg_on_click }>
<div class="modal-dialog">
{ "modal content here, please don't propagate click!" }
</div>
</div>
}
} Adding |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
prevent_default
does not prevent bubbling, it prevents the browser from executing the default action associated with that event. For example making a POST request when you submit a form. The method you are after is calledstop_propagation
and should work inyew
.