yew_bs/components/
color_background.rs

1use yew::prelude::*;
2#[derive(Clone, Copy, PartialEq, Debug)]
3pub enum BackgroundColor {
4    Primary,
5    Secondary,
6    Success,
7    Danger,
8    Warning,
9    Info,
10    Light,
11    Dark,
12    White,
13    Transparent,
14}
15impl BackgroundColor {
16    pub fn as_str(&self) -> &'static str {
17        match self {
18            BackgroundColor::Primary => "bg-primary",
19            BackgroundColor::Secondary => "bg-secondary",
20            BackgroundColor::Success => "bg-success",
21            BackgroundColor::Danger => "bg-danger",
22            BackgroundColor::Warning => "bg-warning",
23            BackgroundColor::Info => "bg-info",
24            BackgroundColor::Light => "bg-light",
25            BackgroundColor::Dark => "bg-dark",
26            BackgroundColor::White => "bg-white",
27            BackgroundColor::Transparent => "bg-transparent",
28        }
29    }
30}
31/// Background opacity levels
32#[derive(Clone, Copy, PartialEq, Debug)]
33pub enum BackgroundOpacity {
34    Opacity10,
35    Opacity25,
36    Opacity50,
37    Opacity75,
38    Opacity100,
39}
40impl BackgroundOpacity {
41    pub fn as_str(&self) -> &'static str {
42        match self {
43            BackgroundOpacity::Opacity10 => "bg-opacity-10",
44            BackgroundOpacity::Opacity25 => "bg-opacity-25",
45            BackgroundOpacity::Opacity50 => "bg-opacity-50",
46            BackgroundOpacity::Opacity75 => "bg-opacity-75",
47            BackgroundOpacity::Opacity100 => "bg-opacity-100",
48        }
49    }
50}
51#[derive(Properties, PartialEq)]
52pub struct ColorBackgroundProps {
53    pub color: BackgroundColor,
54    #[prop_or_default]
55    pub opacity: Option<BackgroundOpacity>,
56    #[prop_or_default]
57    pub children: Children,
58    #[prop_or_default]
59    pub class: Option<AttrValue>,
60}
61#[function_component(ColorBackground)]
62pub fn color_background(props: &ColorBackgroundProps) -> Html {
63    let mut classes = Classes::new();
64    classes.push(props.color.as_str());
65    if let Some(opacity) = &props.opacity {
66        classes.push(opacity.as_str());
67    }
68    if let Some(custom_class) = &props.class {
69        classes.push(custom_class.to_string());
70    }
71    html! {
72        < div class = { classes } > { for props.children.iter() } </ div >
73    }
74}