yew_bs/components/
object_fit.rs

1use yew::prelude::*;
2#[derive(Clone, Copy, PartialEq, Debug)]
3pub enum ObjectFitVariant {
4    Contain,
5    Cover,
6    Fill,
7    Scale,
8    None,
9}
10impl ObjectFitVariant {
11    pub fn as_str(&self) -> &'static str {
12        match self {
13            ObjectFitVariant::Contain => "object-fit-contain",
14            ObjectFitVariant::Cover => "object-fit-cover",
15            ObjectFitVariant::Fill => "object-fit-fill",
16            ObjectFitVariant::Scale => "object-fit-scale",
17            ObjectFitVariant::None => "object-fit-none",
18        }
19    }
20}
21/// Props for the ObjectFit component
22#[derive(Properties, PartialEq)]
23pub struct ObjectFitProps {
24    /// Object fit behavior
25    pub fit: ObjectFitVariant,
26    /// The child elements to be wrapped (typically an img element)
27    #[prop_or_default]
28    pub children: Children,
29    /// Additional CSS classes to apply
30    #[prop_or_default]
31    pub class: Option<AttrValue>,
32}
33/// A utility component for applying Bootstrap object-fit utilities
34///
35/// The ObjectFit component provides easy access to Bootstrap's object-fit
36#[function_component(ObjectFit)]
37pub fn object_fit(props: &ObjectFitProps) -> Html {
38    let mut classes = Classes::new();
39    classes.push(props.fit.as_str());
40    if let Some(custom_class) = &props.class {
41        classes.push(custom_class.to_string());
42    }
43    html! {
44        < div class = { classes } > { for props.children.iter() } </ div >
45    }
46}