|
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; |
3 | 4 |
|
4 | | -pub struct UseAspectRatioProps { |
5 | | - style: Style, |
6 | | -} |
| 5 | +const DEFAULT_RATIO: f64 = 1.0; |
7 | 6 |
|
8 | | -pub struct UseAspectRatioAttrs { |
9 | | - style: Style, |
10 | | -} |
| 7 | +/* ------------------------------------------------------------------------------------------------- |
| 8 | + * AspectRatio |
| 9 | + * -----------------------------------------------------------------------------------------------*/ |
11 | 10 |
|
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"; |
24 | 12 |
|
25 | 13 | #[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, |
29 | 30 | ) -> 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 | + }); |
31 | 46 |
|
32 | 47 | view! { |
| 48 | + // ensures inner element is contained |
33 | 49 | <div |
34 | | - // Ensures inner element is contained. |
35 | 50 | style:position="relative" |
36 | | - // Ensures padding bottom trick maths works. |
| 51 | + // ensures padding bottom trick works |
37 | 52 | style:width="100%" |
38 | | - style:padding-bottom=move || format!("{}%", 100.0 / ratio.get()) |
| 53 | + style:padding-bottom=move || format!("{}%", padding_bottom.get()) |
39 | 54 | data-radix-aspect-ratio-wrapper="" |
40 | 55 | > |
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 | + /> |
42 | 68 | </div> |
43 | 69 | } |
44 | 70 | } |
45 | 71 |
|
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 | + * -----------------------------------------------------------------------------------------------*/ |
74 | 75 |
|
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; |
80 | 79 | } |
0 commit comments