Skip to main content

orbital_base_components/data/image/
base.rs

1use leptos::prelude::*;
2
3#[derive(Clone, Copy, Default, PartialEq, Eq)]
4pub enum ImageShape {
5    Circular,
6    Rounded,
7    #[default]
8    Square,
9}
10
11impl ImageShape {
12    pub fn as_str(&self) -> &'static str {
13        match self {
14            Self::Circular => "circular",
15            Self::Rounded => "rounded",
16            Self::Square => "square",
17        }
18    }
19}
20
21#[derive(Clone, Copy, Default, PartialEq, Eq)]
22pub enum ImageFit {
23    None,
24    Contain,
25    Cover,
26    #[default]
27    Fill,
28    ScaleDown,
29}
30
31impl ImageFit {
32    pub fn as_str(&self) -> &'static str {
33        match self {
34            Self::None => "none",
35            Self::Contain => "contain",
36            Self::Cover => "cover",
37            Self::Fill => "fill",
38            Self::ScaleDown => "scale-down",
39        }
40    }
41}
42
43#[component]
44pub fn BaseImage(
45    #[prop(optional, into)] class: MaybeProp<String>,
46    #[prop(optional, into)] src: MaybeProp<String>,
47    #[prop(optional, into)] alt: MaybeProp<String>,
48    #[prop(optional, into)] width: MaybeProp<String>,
49    #[prop(optional, into)] height: MaybeProp<String>,
50    #[prop(optional, into)] shape: Signal<ImageShape>,
51    #[prop(optional, into)] block: Signal<bool>,
52    #[prop(optional, into)] shadow: Signal<bool>,
53    #[prop(optional, into)] fit: Signal<ImageFit>,
54) -> impl IntoView {
55    view! {
56        <img
57            class=move || {
58                let mut parts = vec![
59                    "orbital-image".to_string(),
60                    format!("orbital-image--{}", shape.get().as_str()),
61                    format!("orbital-image--fit-{}", fit.get().as_str()),
62                ];
63                if block.get() {
64                    parts.push("orbital-image--block".to_string());
65                }
66                if shadow.get() {
67                    parts.push("orbital-image--shadow".to_string());
68                }
69                if let Some(extra) = class.get() {
70                    if !extra.is_empty() {
71                        parts.push(extra);
72                    }
73                }
74                parts.join(" ")
75            }
76            src=move || src.get()
77            alt=move || alt.get()
78            width=move || width.get()
79            height=move || height.get()
80        />
81    }
82}