|
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; |
45 | 4 |
|
46 | 5 | #[component] |
47 | 6 | 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>, |
51 | 11 | ) -> impl IntoView { |
52 | | - let attrs = use_aspect_ratio(UseAspectRatioProps { style }); |
| 12 | + let children = StoredValue::new(children.into_inner()); |
53 | 13 |
|
54 | 14 | 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> |
58 | 38 | </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> |
79 | 40 | } |
80 | 41 | } |
0 commit comments