on master help sign in
Move the `on` event listener constructor to the `builder` module
aa8d0f9Nick Fitzgerald
src/builder.rs+27−1
@@ -39,7 +39,7 @@ where
39 /// # Example
39 /// # Example
40 ///
40 ///
41 /// ```no_run
41 /// ```no_run
42− /// use dodrio::{builder::*, bumpalo::Bump, on};
42+ /// use dodrio::{builder::*, bumpalo::Bump};
43 ///
43 ///
44 /// let b = Bump::new();
44 /// let b = Bump::new();
45 ///
45 ///
@@ -856,3 +856,29 @@ pub fn text<'a>(contents: &'a str) -> Node<'a> {
856pub fn attr<'a>(name: &'a str, value: &'a str) -> Attribute<'a> {
856pub fn attr<'a>(name: &'a str, value: &'a str) -> Attribute<'a> {
857 Attribute { name, value }
857 Attribute { name, value }
858}
858}
859+
860+/// Create an event listener.
861+///
862+/// `event` is the type of event to listen for, e.g. `"click"`. The `callback`
863+/// is the function that will be invoked when the event occurs.
864+///
865+/// # Example
866+///
867+/// ```no_run
868+/// use dodrio::{builder::*, bumpalo::Bump};
869+///
870+/// let b = Bump::new();
871+///
872+/// let listener = on(&b, "click", |root, vdom, event| {
873+/// // do something when a click happens...
874+/// });
875+/// ```
876+pub fn on<'a, F>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
877+where
878+ F: Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static,
879+{
880+ Listener {
881+ event,
882+ callback: bump.alloc(callback),
883+ }
884+}
src/lib.rs+2−2
@@ -3,7 +3,7 @@
3//! ## Example
3//! ## Example
4//!
4//!
5//! ```no_run
5//! ```no_run
6−//! use dodrio::{on, bumpalo::Bump, Attribute, Node, Render};
6+//! use dodrio::{bumpalo::Bump, Attribute, Node, Render};
7//! use wasm_bindgen::UnwrapThrowExt;
7//! use wasm_bindgen::UnwrapThrowExt;
8//!
8//!
9//! /// A component that greets someone.
9//! /// A component that greets someone.
@@ -67,6 +67,6 @@ pub mod builder;
67
67
68// Re-export items at the top level.
68// Re-export items at the top level.
69pub use self::cached::Cached;
69pub use self::cached::Cached;
70−pub use self::node::{on, Attribute, ElementNode, Listener, ListenerCallback, Node, TextNode};
70+pub use self::node::{Attribute, ElementNode, Listener, ListenerCallback, Node, TextNode};
71pub use self::render::{Render, RootRender};
71pub use self::render::{Render, RootRender};
72pub use self::vdom::{Vdom, VdomWeak};
72pub use self::vdom::{Vdom, VdomWeak};
src/node.rs+2−18
@@ -43,9 +43,9 @@ pub type ListenerCallback<'a> =
43/// An event listener.
43/// An event listener.
44pub struct Listener<'a> {
44pub struct Listener<'a> {
45 /// The type of event to listen for.
45 /// The type of event to listen for.
46− pub event: &'a str,
46+ pub(crate) event: &'a str,
47 /// The callback to invoke when the event happens.
47 /// The callback to invoke when the event happens.
48− pub callback: ListenerCallback<'a>,
48+ pub(crate) callback: ListenerCallback<'a>,
49}
49}
50
50
51/// An attribute on a DOM node, such as `id="my-thing"` or
51/// An attribute on a DOM node, such as `id="my-thing"` or
@@ -196,19 +196,3 @@ impl Listener<'_> {
196 }
196 }
197 }
197 }
198}
198}
199−
200−/// Utility function for creating event listeners and downcasting the component
201−/// to its `RootRender` concrete type.
202−pub fn on<'a, F>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
203−where
204− F: Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static,
205−{
206− Listener {
207− event,
208− callback: bump.alloc(
209− move |component: &mut dyn RootRender, vdom: VdomWeak, event: web_sys::Event| {
210− callback(component, vdom, event);
211− },
212− ),
213− }
214−}