Skip to content

Commit 1f22dd9

Browse files
geoffreygarrettgeoffreygarrettDanielleHuisman
authored
chore(aspect-ratio): update to Leptos 0.7 (#419)
* Update Aspect Ratio to Leptos 0.7 * Cleanup --------- Co-authored-by: geoffreygarrett <[email protected]> Co-authored-by: Daniëlle Huisman <[email protected]>
1 parent da8ed7d commit 1f22dd9

File tree

4 files changed

+40
-86
lines changed

4 files changed

+40
-86
lines changed

Cargo.lock

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

book/src/primitives/components/aspect-ratio.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ Contains the content you want to constrain to a given ratio.
102102
{{#tabs global="framework" }}
103103
{{#tab name="Leptos" }}
104104

105-
| Prop | Type | Default |
106-
| ------- | ---------------- | ------- |
107-
| `ratio` | `MaybeProp<f64>` | `1.0` |
105+
| Prop | Type | Default |
106+
| ---------- | ----------------- | ------- |
107+
| `as_child` | `MaybeProp<bool>` | `false` |
108+
| `ratio` | `Signal<f64>` | `1.0` |
108109

109110
{{#endtab }}
110111
{{#tab name="Yew" }}

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

Lines changed: 2 additions & 1 deletion
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 = { path = "../primitive", version = "0.0.2" }
Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,41 @@
1-
use leptos::prelude::*;
2-
use leptos_style::Style;
3-
4-
pub struct UseAspectRatioProps {
5-
style: Style,
6-
}
7-
8-
pub struct UseAspectRatioAttrs {
9-
style: Style,
10-
}
11-
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-
}
24-
25-
#[component]
26-
pub fn BaseAspectRatio(
27-
#[prop(into, optional)] ratio: MaybeProp<f64>,
28-
#[prop(optional)] children: Option<Children>,
29-
) -> impl IntoView {
30-
let ratio = Signal::derive(move || ratio.get().unwrap_or(1.0));
31-
32-
view! {
33-
<div
34-
// Ensures inner element is contained.
35-
style:position="relative"
36-
// Ensures padding bottom trick maths works.
37-
style:width="100%"
38-
style:padding-bottom=move || format!("{}%", 100.0 / ratio.get())
39-
data-radix-aspect-ratio-wrapper=""
40-
>
41-
{children.map(|children| children())}
42-
</div>
43-
}
44-
}
1+
use leptos::{attribute_interceptor::AttributeInterceptor, html, prelude::*};
2+
use leptos_node_ref::AnyNodeRef;
3+
use radix_leptos_primitive::Primitive;
454

465
#[component]
476
pub fn AspectRatio(
48-
#[prop(into, optional)] ratio: MaybeProp<f64>,
49-
#[prop(into, optional)] style: Style,
50-
#[prop(optional)] children: Option<Children>,
7+
#[prop(into, optional, default = 1.0.into())] ratio: Signal<f64>,
8+
#[prop(into, optional)] as_child: MaybeProp<bool>,
9+
#[prop(optional)] node_ref: AnyNodeRef,
10+
children: TypedChildrenFn<impl IntoView + 'static>,
5111
) -> impl IntoView {
52-
let attrs = use_aspect_ratio(UseAspectRatioProps { style });
12+
let children = StoredValue::new(children.into_inner());
5313

5414
view! {
55-
<BaseAspectRatio ratio=ratio>
56-
<div style={attrs.style}>
57-
{children.map(|children| children())}
15+
<AttributeInterceptor let:attrs>
16+
<div
17+
// Ensures inner element is contained
18+
style:position="relative"
19+
// Ensures padding bottom trick maths works
20+
style:width="100%"
21+
style:padding-bottom=move || format!("{}%", 100.0 / ratio.get())
22+
data-radix-aspect-ratio-wrapper=""
23+
>
24+
<Primitive
25+
element=html::div
26+
as_child=as_child
27+
node_ref=node_ref
28+
// Ensures children expand in ratio
29+
style:position="absolute"
30+
style:top="0px"
31+
style:right="0px"
32+
style:bottom="0px"
33+
style:left="0px"
34+
{..attrs}
35+
>
36+
{children.with_value(|children| children())}
37+
</Primitive>
5838
</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 });
74-
75-
view! {
76-
<BaseAspectRatio ratio=ratio>
77-
{render(attrs)}
78-
</BaseAspectRatio>
39+
</AttributeInterceptor>
7940
}
8041
}

0 commit comments

Comments
 (0)