Skip to content

Commit 96367d2

Browse files
geoffreygarrettgeoffreygarrettDanielleHuisman
authored
Update Primitive to Leptos 0.7 (#417)
* Update Workspace for Leptos 0.7 * Update Primitive to Leptos 0.7 * Update Primitive for Leptos 0.7 * Update Primitive for Leptos 0.7 * Cleanup --------- Co-authored-by: geoffreygarrett <geoffrey@apollo.xyz> Co-authored-by: Daniëlle Huisman <danielle@huisman.me>
1 parent a54d7a0 commit 96367d2

File tree

6 files changed

+164
-11
lines changed

6 files changed

+164
-11
lines changed

Cargo.lock

+26-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ members = [
2121
"packages/primitives/leptos/focus-guards",
2222
"packages/primitives/leptos/id",
2323
"packages/primitives/leptos/label",
24+
"packages/primitives/leptos/primitive",
2425
"packages/primitives/leptos/use-controllable-state",
2526
"packages/primitives/leptos/use-escape-keydown",
2627
"packages/primitives/leptos/use-previous",
@@ -41,14 +42,16 @@ repository = "https://github.com/RustForWeb/radix"
4142
version = "0.0.2"
4243

4344
[workspace.dependencies]
44-
console_log = "1.0.0"
4545
console_error_panic_hook = "0.1.7"
46+
console_log = "1.0.0"
4647
dioxus = "0.6.1"
4748
leptos = "0.7.2"
4849
leptos_dom = "0.7.2"
4950
leptos_router = "0.7.2"
50-
leptos-node-ref = "0.0.3"
51-
leptos-style = "0.0.3"
51+
leptos-maybe-callback = "0.1.0"
52+
leptos-node-ref = "0.1.0"
53+
leptos-style = "0.1.0"
54+
leptos-typed-fallback-show = "0.1.0"
5255
log = "0.4.22"
5356
send_wrapper = "0.6.0"
5457
serde = "1.0.198"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "radix-leptos-primitive"
3+
description = "Leptos port of Radix Primitive."
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+
leptos-node-ref.workspace = true
14+
leptos-typed-fallback-show.workspace = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<p align="center">
2+
<a href="../../../../logo.svg">
3+
<img src="../../../../logo.svg" width="300" height="200" alt="Rust Radix Logo">
4+
</a>
5+
</p>
6+
7+
<h1 align="center">radix-leptos-primitive</h1>
8+
9+
This is an internal utility, not intended for public usage.
10+
11+
[Rust Radix](https://github.com/RustForWeb/radix) is a Rust port of [Radix](https://www.radix-ui.com/primitives).
12+
13+
## Documentation
14+
15+
See [the Rust Radix book](https://radix.rustforweb.org/) for documentation.
16+
17+
## Rust For Web
18+
19+
The Rust Radix project is part of [Rust For Web](https://github.com/RustForWeb).
20+
21+
[Rust For Web](https://github.com/RustForWeb) creates and ports web UI libraries for Rust. All projects are free and open source.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Leptos port of [Radix Primitive](https://www.radix-ui.com/primitives).
2+
//!
3+
//! This is an internal utility, not intended for public usage.
4+
5+
//! See [`@radix-ui/react-primitive`](https://www.npmjs.com/package/@radix-ui/react-primitive) for the original package.
6+
7+
mod primitive;
8+
9+
pub use primitive::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use leptos::{
2+
ev::Event,
3+
html::{ElementType, HtmlElement},
4+
prelude::*,
5+
tachys::html::node_ref::NodeRefContainer,
6+
wasm_bindgen::JsCast,
7+
};
8+
use leptos_node_ref::{any_node_ref, AnyNodeRef};
9+
use leptos_typed_fallback_show::TypedFallbackShow;
10+
11+
#[component]
12+
pub fn Primitive<E, C>(
13+
element: fn() -> HtmlElement<E, (), ()>,
14+
#[prop(into, optional)] as_child: MaybeProp<bool>,
15+
#[prop(into, optional)] node_ref: AnyNodeRef,
16+
children: TypedChildrenFn<C>,
17+
) -> impl IntoView
18+
where
19+
E: ElementType + 'static,
20+
C: IntoView + 'static,
21+
View<C>: RenderHtml,
22+
HtmlElement<E, (), ()>: ElementChild<View<C>>,
23+
<HtmlElement<E, (), ()> as ElementChild<View<C>>>::Output: IntoView,
24+
<E as ElementType>::Output: JsCast,
25+
AnyNodeRef: NodeRefContainer<E>,
26+
{
27+
let children = StoredValue::new(children.into_inner());
28+
29+
view! {
30+
<TypedFallbackShow
31+
when=move || as_child.get().unwrap_or_default()
32+
fallback=move || {
33+
element().child(children.with_value(|children| children())).add_any_attr(any_node_ref(node_ref))
34+
}
35+
>
36+
{children.with_value(|children| children()).add_any_attr(any_node_ref(node_ref))}
37+
</TypedFallbackShow>
38+
}
39+
}
40+
41+
#[component]
42+
pub fn VoidPrimitive<E, C>(
43+
element: fn() -> HtmlElement<E, (), ()>,
44+
#[prop(into, optional)] as_child: MaybeProp<bool>,
45+
#[prop(into, optional)] node_ref: AnyNodeRef,
46+
children: TypedChildrenFn<C>,
47+
) -> impl IntoView
48+
where
49+
E: ElementType + 'static,
50+
C: IntoView + 'static,
51+
View<C>: RenderHtml,
52+
<E as ElementType>::Output: JsCast,
53+
AnyNodeRef: NodeRefContainer<E>,
54+
{
55+
let children = StoredValue::new(children.into_inner());
56+
57+
view! {
58+
<TypedFallbackShow
59+
when=move || as_child.get().unwrap_or_default()
60+
fallback=move || { element().add_any_attr(any_node_ref(node_ref)) }
61+
>
62+
{children.with_value(|children| children()).add_any_attr(any_node_ref(node_ref))}
63+
</TypedFallbackShow>
64+
}
65+
}
66+
67+
pub fn compose_callbacks<E>(
68+
original_handler: Option<Callback<E>>,
69+
our_handler: Option<Callback<E>>,
70+
check_default_prevented: Option<bool>,
71+
) -> impl Fn(E)
72+
where
73+
E: Clone + Into<Event> + 'static,
74+
{
75+
let check_default_prevented = check_default_prevented.unwrap_or(true);
76+
77+
move |event: E| {
78+
if let Some(original) = &original_handler {
79+
original.run(event.clone());
80+
}
81+
82+
if !check_default_prevented || !event.clone().into().default_prevented() {
83+
if let Some(our) = &our_handler {
84+
our.run(event);
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)