Skip to content

Commit 875ce69

Browse files
Cleanup
1 parent 98dafa5 commit 875ce69

File tree

4 files changed

+33
-103
lines changed

4 files changed

+33
-103
lines changed

Cargo.lock

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

packages/primitives/leptos/primitive/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ version.workspace = true
1111
[dependencies]
1212
leptos.workspace = true
1313
leptos-node-ref.workspace = true
14-
leptos-typed-fallback-show.workspace = true
14+
leptos-typed-fallback-show.workspace = true
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<p align="center">
32
<a href="../../../../logo.svg">
43
<img src="../../../../logo.svg" width="300" height="200" alt="Rust Radix Logo">
@@ -11,88 +10,12 @@ This is an internal utility, not intended for public usage.
1110

1211
[Rust Radix](https://github.com/RustForWeb/radix) is a Rust port of [Radix](https://www.radix-ui.com/primitives).
1312

14-
## Overview
15-
16-
```rust
17-
use leptos::*;
18-
use leptos_node_ref::AnyNodeRef;
19-
use leptos_typed_fallback_show::TypedFallbackShow;
20-
21-
/// A generic Primitive component. Renders `element()` by default, or its
22-
/// children directly if `as_child` is `true`. We rely on `TypedChildrenFn`
23-
/// so that attributes can pass through at runtime—critical in Leptos v0.7
24-
/// because `Children`-based types block such passthrough.
25-
#[component]
26-
#[allow(non_snake_case)]
27-
pub fn Primitive<E, C>(
28-
element: fn() -> HtmlElement<E, (), ()>,
29-
children: TypedChildrenFn<C>,
30-
#[prop(optional, into)] as_child: MaybeProp<bool>,
31-
#[prop(optional, into)] node_ref: AnyNodeRef,
32-
) -> impl IntoView
33-
where
34-
E: ElementType + 'static,
35-
C: IntoView + 'static,
36-
{
37-
let children = StoredValue::new(children.into_inner());
38-
view! {
39-
<TypedFallbackShow
40-
when=move || as_child.get().unwrap_or_default()
41-
fallback=move || {
42-
element()
43-
.child(children.with_value(|c| c()))
44-
.add_any_attr(leptos_node_ref::any_node_ref(node_ref))
45-
}
46-
>
47-
{children.with_value(|c| c())
48-
.add_any_attr(leptos_node_ref::any_node_ref(node_ref))}
49-
</TypedFallbackShow>
50-
}
51-
}
52-
53-
/// Same idea, but for elements that do not take children (e.g. `img`, `input`).
54-
#[component]
55-
#[allow(non_snake_case)]
56-
pub fn VoidPrimitive<E, C>(
57-
element: fn() -> HtmlElement<E, (), ()>,
58-
children: TypedChildrenFn<C>,
59-
#[prop(optional, into)] as_child: MaybeProp<bool>,
60-
#[prop(optional, into)] node_ref: AnyNodeRef,
61-
) -> impl IntoView
62-
where
63-
E: ElementType + 'static,
64-
{
65-
let children = StoredValue::new(children.into_inner());
66-
view! {
67-
<TypedFallbackShow
68-
when=move || as_child.get().unwrap_or_default()
69-
fallback=move || {
70-
element().add_any_attr(leptos_node_ref::any_node_ref(node_ref))
71-
}
72-
>
73-
{children.with_value(|c| c())
74-
.add_any_attr(leptos_node_ref::any_node_ref(node_ref))}
75-
</TypedFallbackShow>
76-
}
77-
}
78-
79-
// (Compose callbacks is an internal piece from Radix Core; omitted for brevity.)
80-
```
81-
82-
## Notes
83-
84-
- **Why `TypedChildrenFn`?**: Leptos attribute passthrough only works if a component doesn't rely on `AnyView` or `Children`. Using typed children ensures classes, events, etc. from the parent can flow to the rendered DOM node.
85-
- **`as_child`**: Mimics `asChild` in Radix’s React version, but we skip an explicit `<Slot>`: Leptos’s approach to typed fallback rendering covers “slot-like” logic.
86-
- **Class Handling**: Static classes from a parent can overwrite child-defined classes. No built-in merging exists.
87-
- **Attribute System Limitations**: Leptos limits you to 26 dynamic attributes. Past that, nest components or try a custom approach.
88-
- **Parity with React**: In React, `...props` merges everything automatically. In Leptos, we rely on typed props/attributes and can intercept unknown ones with `AttributeInterceptor`.
89-
9013
## Documentation
9114

9215
See [the Rust Radix book](https://radix.rustforweb.org/) for documentation.
9316

9417
## Rust For Web
9518

96-
The Rust Radix project is part of the [Rust For Web](https://github.com/RustForWeb).
19+
The Rust Radix project is part of [Rust For Web](https://github.com/RustForWeb).
9720

9821
[Rust For Web](https://github.com/RustForWeb) creates and ports web UI libraries for Rust. All projects are free and open source.

packages/primitives/leptos/primitive/src/primitive.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@ use leptos::{
22
ev::Event,
33
html::{ElementType, HtmlElement},
44
prelude::*,
5+
tachys::html::node_ref::NodeRefContainer,
56
wasm_bindgen::JsCast,
6-
tachys::html::{node_ref::NodeRefContainer},
77
};
88
use leptos_node_ref::{any_node_ref, AnyNodeRef};
99
use leptos_typed_fallback_show::TypedFallbackShow;
1010

11-
/* -------------------------------------------------------------------------------------------------
12-
* Primitive
13-
* -----------------------------------------------------------------------------------------------*/
14-
1511
#[component]
16-
#[allow(non_snake_case)]
1712
pub fn Primitive<E, C>(
1813
element: fn() -> HtmlElement<E, (), ()>,
14+
#[prop(into, optional)] as_child: MaybeProp<bool>,
15+
#[prop(into, optional)] node_ref: AnyNodeRef,
1916
children: TypedChildrenFn<C>,
20-
#[prop(optional, into)] as_child: MaybeProp<bool>,
21-
#[prop(optional, into)] node_ref: AnyNodeRef,
2217
) -> impl IntoView
2318
where
2419
E: ElementType + 'static,
@@ -44,12 +39,11 @@ where
4439
}
4540

4641
#[component]
47-
#[allow(non_snake_case)]
4842
pub fn VoidPrimitive<E, C>(
4943
element: fn() -> HtmlElement<E, (), ()>,
50-
children: TypedChildrenFn<C>,
5144
#[prop(into, optional)] as_child: MaybeProp<bool>,
5245
#[prop(into, optional)] node_ref: AnyNodeRef,
46+
children: TypedChildrenFn<C>,
5347
) -> impl IntoView
5448
where
5549
E: ElementType + 'static,
@@ -59,6 +53,7 @@ where
5953
AnyNodeRef: NodeRefContainer<E>,
6054
{
6155
let children = StoredValue::new(children.into_inner());
56+
6257
view! {
6358
<TypedFallbackShow
6459
when=move || as_child.get().unwrap_or_default()
@@ -69,10 +64,6 @@ where
6964
}
7065
}
7166

72-
/* -------------------------------------------------------------------------------------------------
73-
* Utils
74-
* -----------------------------------------------------------------------------------------------*/
75-
7667
pub fn compose_callbacks<E>(
7768
original_handler: Option<Callback<E>>,
7869
our_handler: Option<Callback<E>>,
@@ -84,12 +75,10 @@ where
8475
let check_default_prevented = check_default_prevented.unwrap_or(true);
8576

8677
move |event: E| {
87-
// Run original handler first, matching TypeScript behavior
8878
if let Some(original) = &original_handler {
8979
original.run(event.clone());
9080
}
9181

92-
// Only run our handler if default wasn't prevented (when checking is enabled)
9382
if !check_default_prevented || !event.clone().into().default_prevented() {
9483
if let Some(our) = &our_handler {
9584
our.run(event);

0 commit comments

Comments
 (0)