-
-
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
No way to listen to custom events exposed by custom elements #1777
Comments
I created a discussion to discuss potential solutions: #1779 |
For a pub fn attach_event_listener<S, E>(
&self,
event: S,
callback: Callback<E>,
) -> Option<gloo::events::EventListener>
where
S: Into<std::borrow::Cow<'static, str>>,
E: AsRef<web_sys::Event> + JsCast + 'static,
{
use gloo::events::*;
let event_target: EventTarget = self.get()?.into();
match &callback {
Callback::Callback { passive, .. } => {
let passive = passive.unwrap_or_default();
let listener = EventListener::new_with_options(
&event_target,
event,
EventListenerOptions {
phase: EventListenerPhase::Bubble,
passive,
},
move |e| callback.emit(e.clone().unchecked_into()),
);
Some(listener)
}
Callback::CallbackOnce(_) => {
let listener = EventListener::once(&event_target, event, move |e| {
callback.emit(e.clone().unchecked_into())
});
Some(listener)
}
}
} We'd probably avoid leaking the Then in the rendered function of a component you might do something like this: fn rendered(&mut self, ctx: &Context<Self>) {
match (self.node_ref.get(), self.click_listener.as_ref()) {
(None, Some(_)) => {
// element has not been rendered so remove old listener
self.click_listener.take();
}
(Some(_), None) => {
// element has been rendered and no event listener has been attached
// so add one this time.
self.click_listener = self.node_ref.attach_event_listener(
"click",
ctx.link().callback(|_: yew::MouseEvent| Msg::Increment),
);
}
// either the element is not rendered and there is no old listener OR
// both the element and listener are still set - with both we do nothing :)
_ => {}
}
} Also #1991 even though closed I have solved the hiccup and I'm currently keeping it up to date in my fork just in case there was any interest in that :) |
Sorry for the accidental closing 😢 |
Is there no workaround for this issue 😢 ? newbie here |
@dfenerski Check the documentation here |
Tracking issue for the point, "Custom events" described in #1666
Problem
from #1666
Questionnaire
I apologize it took me so long to create this issue after being requested here (#1666 (comment))
The text was updated successfully, but these errors were encountered: