Skip to content

Commit cf016f1

Browse files
geoffreygarrettgeoffreygarrettDanielleHuisman
authored
Add MaybeCallback (#15)
* feat(maybe-callback): convenience for leptos 0.7 * docs(cargo): add description * docs(readme): repository url * Cleanup --------- Co-authored-by: geoffreygarrett <[email protected]> Co-authored-by: Daniëlle Huisman <[email protected]>
1 parent f09042d commit cf016f1

File tree

10 files changed

+618
-3
lines changed

10 files changed

+618
-3
lines changed

Cargo.lock

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ version = "0.0.3"
1111

1212
[workspace.dependencies]
1313
leptos = "0.7.0"
14+
log = "0.4.25"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "leptos-maybe-callback"
3+
description = "Optional callbacks for Leptos."
4+
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
version.workspace = true
10+
11+
[dependencies]
12+
leptos.workspace = true
13+
14+
[dev-dependencies]
15+
log.workspace = true
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Leptos Maybe Callback
2+
3+
Optional callbacks for [Leptos](https://leptos.dev/).
4+
5+
## Documentation
6+
7+
Documentation for the crate is available on [Docs.rs](https://docs.rs/):
8+
9+
- [`leptos-node-ref`](https://docs.rs/leptos-maybe-callback/latest/leptos_maybe_callback/)
10+
11+
## Example
12+
13+
### Component with Optional Callback Prop
14+
15+
Define a component that accepts an optional callback using `#[prop(into, optional)]`. This allows passing a closure, a
16+
`Callback`, or omitting the prop.
17+
18+
```rust
19+
use leptos::{ev::MouseEvent, prelude::*};
20+
use leptos_maybe_callback::MaybeCallback;
21+
22+
/// A button component with an optional `onclick` callback.
23+
#[component]
24+
pub fn Button(
25+
#[prop(into, optional)]
26+
onclick: MaybeCallback<MouseEvent>,
27+
) -> impl IntoView {
28+
view! {
29+
<button on:click=onclick.into_handler()>
30+
"Click me"
31+
</button>
32+
}
33+
}
34+
```
35+
36+
### Using the Component with a Closure
37+
38+
Use the `Button` component and provide a closure for the `onclick` prop.
39+
40+
```rust
41+
use leptos::prelude::*;
42+
use leptos_maybe_callback::MaybeCallback;
43+
44+
/// Parent component using `Button` with a closure.
45+
#[component]
46+
pub fn ButtonWithClosure() -> impl IntoView {
47+
view! {
48+
<div>
49+
<Button onclick=|_| log::info!("Clicked via closure!") />
50+
<Button />
51+
</div>
52+
}
53+
}
54+
```
55+
56+
### Using the Component with a `Callback`
57+
58+
Alternatively, pass a `Callback` as the `onclick` prop.
59+
60+
```rust
61+
use leptos::{ev::MouseEvent, prelude::*};
62+
use leptos_maybe_callback::MaybeCallback;
63+
64+
/// Parent component using `Button` with a `Callback`.
65+
#[component]
66+
pub fn ButtonWithCallback() -> impl IntoView {
67+
let on_click = Callback::new(|event: MouseEvent| {
68+
log::info!("Clicked with event: {:?}", event);
69+
});
70+
71+
view! {
72+
<div>
73+
<Button onclick=on_click />
74+
<Button />
75+
</div>
76+
}
77+
}
78+
```
79+
80+
### Omitting the Callback
81+
82+
If no callback is needed, omit the `onclick` prop or pass `None`.
83+
84+
```rust
85+
use leptos::{ev::MouseEvent, prelude::*};
86+
use leptos_maybe_callback::MaybeCallback;
87+
88+
/// Parent component using `Button` without a callback.
89+
#[component]
90+
pub fn ButtonWithoutCallback() -> impl IntoView {
91+
view! {
92+
<div>
93+
<Button />
94+
<Button onclick={None::<Callback<MouseEvent>>} />
95+
</div>
96+
}
97+
}
98+
```
99+
100+
## Rust For Web
101+
102+
The Leptos Maybe Callback project is part of [Rust For Web](https://github.com/RustForWeb).
103+
104+
[Rust For Web](https://github.com/RustForWeb) creates and ports web UI libraries for Rust. All projects are free and open source.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//! Optional callbacks for [Leptos](https://leptos.dev/).
2+
//!
3+
//! # Example
4+
//!
5+
//! ## Component with Optional Callback Prop
6+
//!
7+
//! Define a component that accepts an optional callback using `#[prop(into, optional)]`. This allows passing a closure, a
8+
//! `Callback`, or omitting the prop.
9+
//!
10+
//! ```
11+
//! use leptos::{ev::MouseEvent, prelude::*};
12+
//! use leptos_maybe_callback::MaybeCallback;
13+
//!
14+
//! /// A button component with an optional `onclick` callback.
15+
//! #[component]
16+
//! pub fn Button(
17+
//! #[prop(into, optional)]
18+
//! onclick: MaybeCallback<MouseEvent>,
19+
//! ) -> impl IntoView {
20+
//! view! {
21+
//! <button on:click=onclick.into_handler()>
22+
//! "Click me"
23+
//! </button>
24+
//! }
25+
//! }
26+
//! ```
27+
//!
28+
//! ## Using the Component with a Closure
29+
//!
30+
//! Use the `Button` component and provide a closure for the `onclick` prop.
31+
//!
32+
//! ```
33+
//! # use leptos::ev::MouseEvent;
34+
//! use leptos::prelude::*;
35+
//! use leptos_maybe_callback::MaybeCallback;
36+
//!
37+
//! # #[component]
38+
//! # pub fn Button(
39+
//! # #[prop(into, optional)]
40+
//! # onclick: MaybeCallback<MouseEvent>,
41+
//! # ) -> impl IntoView {
42+
//! # view! {
43+
//! # <button on:click=onclick.into_handler()>
44+
//! # "Click me"
45+
//! # </button>
46+
//! # }
47+
//! # }
48+
//! #
49+
//! /// Parent component using `Button` with a closure.
50+
//! #[component]
51+
//! pub fn ButtonWithClosure() -> impl IntoView {
52+
//! view! {
53+
//! <div>
54+
//! <Button onclick=|_| log::info!("Clicked via closure!") />
55+
//! <Button />
56+
//! </div>
57+
//! }
58+
//! }
59+
//! ```
60+
//!
61+
//! ## Using the Component with a `Callback`
62+
//!
63+
//! Alternatively, pass a `Callback` as the `onclick` prop.
64+
//!
65+
//! ```rust
66+
//! use leptos::{ev::MouseEvent, prelude::*};
67+
//! use leptos_maybe_callback::MaybeCallback;
68+
//!
69+
//! # #[component]
70+
//! # pub fn Button(
71+
//! # #[prop(into, optional)]
72+
//! # onclick: MaybeCallback<MouseEvent>,
73+
//! # ) -> impl IntoView {
74+
//! # view! {
75+
//! # <button on:click=onclick.into_handler()>
76+
//! # "Click me"
77+
//! # </button>
78+
//! # }
79+
//! # }
80+
//! #
81+
//! /// Parent component using `Button` with a `Callback`.
82+
//! #[component]
83+
//! pub fn ButtonWithCallback() -> impl IntoView {
84+
//! let on_click = Callback::new(|event: MouseEvent| {
85+
//! log::info!("Clicked with event: {:?}", event);
86+
//! });
87+
//!
88+
//! view! {
89+
//! <div>
90+
//! <Button onclick=on_click />
91+
//! <Button />
92+
//! </div>
93+
//! }
94+
//! }
95+
//! ```
96+
//!
97+
//! ## Omitting the Callback
98+
//!
99+
//! If no callback is needed, omit the `onclick` prop or pass `None`.
100+
//!
101+
//! ```rust
102+
//! use leptos::{ev::MouseEvent, prelude::*};
103+
//! use leptos_maybe_callback::MaybeCallback;
104+
//!
105+
//! # #[component]
106+
//! # pub fn Button(
107+
//! # #[prop(into, optional)]
108+
//! # onclick: MaybeCallback<MouseEvent>,
109+
//! # ) -> impl IntoView {
110+
//! # view! {
111+
//! # <button on:click=onclick.into_handler()>
112+
//! # "Click me"
113+
//! # </button>
114+
//! # }
115+
//! # }
116+
//! #
117+
//! /// Parent component using `Button` without a callback.
118+
//! #[component]
119+
//! pub fn ButtonWithoutCallback() -> impl IntoView {
120+
//! view! {
121+
//! <div>
122+
//! <Button />
123+
//! <Button onclick={None::<Callback<MouseEvent>>} />
124+
//! </div>
125+
//! }
126+
//! }
127+
//! ```
128+
129+
mod maybe_callback;
130+
131+
pub use maybe_callback::*;

0 commit comments

Comments
 (0)