From be9f1a52ad2b04f101397ae34482ea7394df653b Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Fri, 7 Jan 2022 12:12:13 -0500 Subject: [PATCH] docs: better document the `EventHandler` type --- packages/core/src/nodes.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index 9917d4094..d5a486176 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -324,7 +324,30 @@ type InternalListenerCallback<'bump> = BumpBox<'bump, dyn FnMut(AnyEvent) + 'bum type ExternalListenerCallback<'bump, T> = BumpBox<'bump, dyn FnMut(T) + 'bump>; -/// The callback based through the `rsx!` macro. +/// The callback type generated by the `rsx!` macro when an `on` field is specified for components. +/// +/// This makes it possible to pass `move |evt| {}` style closures into components as property fields. +/// +/// ```rust +/// +/// rsx!{ +/// MyComponent { onclick: move |evt| log::info!("clicked"), } +/// } +/// +/// #[derive(Props)] +/// struct MyProps<'a> { +/// onclick: EventHandler<'a, MouseEvent>, +/// } +/// +/// fn MyComponent(cx: Scope<'a, MyProps<'a>>) -> Element { +/// cx.render(rsx!{ +/// button { +/// onclick: move |evt| cx.props.onclick.call(evt), +/// } +/// }) +/// } +/// +/// ``` pub struct EventHandler<'bump, T = ()> { pub callback: &'bump RefCell>>, }