Skip to content

Commit fc8f7cd

Browse files
author
geoffreygarrett
committed
Update Aspect Ratio to Leptos 0.7
1 parent 98abf14 commit fc8f7cd

File tree

4 files changed

+65
-63
lines changed

4 files changed

+65
-63
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ yew = "0.21.0"
8282
yew-router = "0.18.0"
8383
yew-struct-component = "0.1.4"
8484
yew-style = "0.1.4"
85+
radix-leptos-primitive.version = "0.0.2"
8586

8687
# Subcrate packages (paths remain the same; you can comment out any subcrate not yet upgraded).
8788
# We centralize shared dependencies in [workspace.dependencies] for a single source of truth,
@@ -123,3 +124,4 @@ radix-leptos-primitive.path = "./packages/primitives/leptos/primitive"
123124
yew = { git = "https://github.com/RustForWeb/yew.git", branch = "feature/use-composed-ref" }
124125
yew-router = { git = "https://github.com/RustForWeb/yew.git", branch = "feature/use-composed-ref" }
125126
leptos-node-ref = { git = "https://github.com/geoffreygarrett/leptos-utils", branch = "feature/any-node-ref" }
127+
radix-leptos-primitive = { git = "https://github.com/geoffreygarrett/radix", branch = "update/leptos-0.7-primitive" }

packages/primitives/leptos/aspect-ratio/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ version.workspace = true
1111

1212
[dependencies]
1313
leptos.workspace = true
14-
leptos-style.workspace = true
14+
leptos-node-ref.workspace = true
15+
radix-leptos-primitive.workspace = true

packages/primitives/leptos/aspect-ratio/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ See [the Rust Radix book](https://radix.rustforweb.org/) for documentation.
1616

1717
## Rust For Web
1818

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

2121
[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
@@ -1,80 +1,79 @@
1-
use leptos::prelude::*;
2-
use leptos_style::Style;
1+
use leptos::{prelude::*, html};
2+
use radix_leptos_primitive::Primitive;
3+
use leptos_node_ref::AnyNodeRef;
34

4-
pub struct UseAspectRatioProps {
5-
style: Style,
6-
}
5+
const DEFAULT_RATIO: f64 = 1.0;
76

8-
pub struct UseAspectRatioAttrs {
9-
style: Style,
10-
}
7+
/* -------------------------------------------------------------------------------------------------
8+
* AspectRatio
9+
* -----------------------------------------------------------------------------------------------*/
1110

12-
pub fn use_aspect_ratio(props: UseAspectRatioProps) -> UseAspectRatioAttrs {
13-
UseAspectRatioAttrs {
14-
style: props.style.with_defaults([
15-
// Ensures children expand in ratio.
16-
("position", "absolute"),
17-
("top", "0px"),
18-
("right", "0px"),
19-
("bottom", "0px"),
20-
("left", "0px"),
21-
]),
22-
}
23-
}
11+
const NAME: &'static str = "AspectRatio";
2412

2513
#[component]
26-
pub fn BaseAspectRatio(
27-
#[prop(into, optional)] ratio: MaybeProp<f64>,
28-
#[prop(optional)] children: Option<Children>,
14+
#[allow(non_snake_case)]
15+
pub fn AspectRatio(
16+
/// Children passed to the AspectRatio component
17+
children: TypedChildrenFn<impl IntoView + 'static>,
18+
19+
/// Change the default rendered element for the one passed as a child
20+
#[prop(into, optional, default = false.into())]
21+
as_child: MaybeProp<bool>,
22+
23+
/// The desired ratio when rendering the content (e.g., 16/9). Defaults to 1.0 if not specified.
24+
#[prop(into, optional, default = DEFAULT_RATIO.into())]
25+
ratio: MaybeProp<f64>,
26+
27+
/// Reference to the underlying DOM node
28+
#[prop(into, optional)]
29+
node_ref: AnyNodeRef,
2930
) -> impl IntoView {
30-
let ratio = Signal::derive(move || ratio.get().unwrap_or(1.0));
31+
// calculates the percent-based padding for the aspect ratio
32+
let padding_bottom = Signal::derive(move || {
33+
100.0
34+
/ ratio
35+
.get()
36+
.unwrap_or(DEFAULT_RATIO)
37+
.clamp(f64::EPSILON, f64::MAX)
38+
});
39+
40+
#[cfg(debug_assertions)]
41+
Effect::new(move |_| {
42+
leptos::logging::log!("[{NAME}] ratio: {:?}", ratio.get());
43+
leptos::logging::log!("[{NAME}] as_child: {:?}", as_child.get());
44+
leptos::logging::log!("[{NAME}] node_ref: {:?}", node_ref.get());
45+
});
3146

3247
view! {
48+
// ensures inner element is contained
3349
<div
34-
// Ensures inner element is contained.
3550
style:position="relative"
36-
// Ensures padding bottom trick maths works.
51+
// ensures padding bottom trick works
3752
style:width="100%"
38-
style:padding-bottom=move || format!("{}%", 100.0 / ratio.get())
53+
style:padding-bottom=move || format!("{}%", padding_bottom.get())
3954
data-radix-aspect-ratio-wrapper=""
4055
>
41-
{children.map(|children| children())}
56+
<Primitive
57+
// ensures children expand to fill the ratio
58+
element=html::div
59+
as_child=as_child
60+
node_ref=node_ref
61+
children=children
62+
style:position="absolute"
63+
style:top="0"
64+
style:right="0"
65+
style:bottom="0"
66+
style:left="0"
67+
/>
4268
</div>
4369
}
4470
}
4571

46-
#[component]
47-
pub fn AspectRatio(
48-
#[prop(into, optional)] ratio: MaybeProp<f64>,
49-
#[prop(into, optional)] style: Style,
50-
#[prop(optional)] children: Option<Children>,
51-
) -> impl IntoView {
52-
let attrs = use_aspect_ratio(UseAspectRatioProps { style });
53-
54-
view! {
55-
<BaseAspectRatio ratio=ratio>
56-
<div style={attrs.style}>
57-
{children.map(|children| children())}
58-
</div>
59-
</BaseAspectRatio>
60-
}
61-
}
62-
63-
#[component]
64-
pub fn AspectRatioAsChild<R, RV>(
65-
#[prop(into, optional)] ratio: MaybeProp<f64>,
66-
#[prop(into, optional)] style: Style,
67-
render: R,
68-
) -> impl IntoView
69-
where
70-
R: Fn(UseAspectRatioAttrs) -> RV + Send + 'static,
71-
RV: IntoView + 'static,
72-
{
73-
let attrs = use_aspect_ratio(UseAspectRatioProps { style });
72+
/* -------------------------------------------------------------------------------------------------
73+
* Primitive re-exports
74+
* -----------------------------------------------------------------------------------------------*/
7475

75-
view! {
76-
<BaseAspectRatio ratio=ratio>
77-
{render(attrs)}
78-
</BaseAspectRatio>
79-
}
76+
pub mod primitive {
77+
pub use super::*;
78+
pub use AspectRatio as Root;
8079
}

0 commit comments

Comments
 (0)