yew_bs/components/
colors.rs

1use yew::prelude::*;
2#[derive(Clone, Copy, PartialEq, Debug)]
3pub enum TextColor {
4    Primary,
5    Secondary,
6    Success,
7    Danger,
8    Warning,
9    Info,
10    Light,
11    Dark,
12    White,
13    Muted,
14    Black50,
15    White50,
16}
17impl TextColor {
18    pub fn as_str(&self) -> &'static str {
19        match self {
20            TextColor::Primary => "text-primary",
21            TextColor::Secondary => "text-secondary",
22            TextColor::Success => "text-success",
23            TextColor::Danger => "text-danger",
24            TextColor::Warning => "text-warning",
25            TextColor::Info => "text-info",
26            TextColor::Light => "text-light",
27            TextColor::Dark => "text-dark",
28            TextColor::White => "text-white",
29            TextColor::Muted => "text-muted",
30            TextColor::Black50 => "text-black-50",
31            TextColor::White50 => "text-white-50",
32        }
33    }
34}
35/// Background color variants (re-exporting from common for consistency)
36pub use crate::components::color_background::BackgroundColor;
37/// Opacity levels for colors
38#[derive(Clone, Copy, PartialEq, Debug)]
39pub enum ColorOpacity {
40    Opacity10,
41    Opacity25,
42    Opacity50,
43    Opacity75,
44    Opacity100,
45}
46impl ColorOpacity {
47    pub fn as_str(&self) -> &'static str {
48        match self {
49            ColorOpacity::Opacity10 => "opacity-10",
50            ColorOpacity::Opacity25 => "opacity-25",
51            ColorOpacity::Opacity50 => "opacity-50",
52            ColorOpacity::Opacity75 => "opacity-75",
53            ColorOpacity::Opacity100 => "opacity-100",
54        }
55    }
56}
57#[derive(Properties, PartialEq)]
58pub struct TextColorProps {
59    pub color: TextColor,
60    #[prop_or_default]
61    pub opacity: Option<ColorOpacity>,
62    #[prop_or_default]
63    pub children: Children,
64    #[prop_or_default]
65    pub class: Option<AttrValue>,
66    #[prop_or("span".into())]
67    pub tag: AttrValue,
68}
69#[function_component(TextColorComponent)]
70pub fn text_color(props: &TextColorProps) -> Html {
71    let mut class_string = props.color.as_str().to_string();
72    if let Some(opacity) = &props.opacity {
73        class_string.push(' ');
74        class_string.push_str(opacity.as_str());
75    }
76    if let Some(custom_class) = &props.class {
77        class_string.push(' ');
78        class_string.push_str(custom_class.as_str());
79    }
80    let tag = props.tag.as_str();
81    if tag == "span" {
82        html! {
83            < span class = { class_string } > { for props.children.iter() } </ span >
84        }
85    } else if tag == "div" {
86        html! {
87            < div class = { class_string } > { for props.children.iter() } </ div >
88        }
89    } else if tag == "p" {
90        html! {
91            < p class = { class_string } > { for props.children.iter() } </ p >
92        }
93    } else {
94        html! {
95            < span class = { class_string } > { for props.children.iter() } </ span >
96        }
97    }
98}