patternfly_yew/icon/
mod.rs

1mod generated;
2mod state;
3
4pub use generated::*;
5pub use state::*;
6
7use crate::{core::AsClasses, prelude::Styled};
8use yew::html::ChildrenRenderer;
9use yew::{html::IntoPropValue, prelude::*, virtual_dom::VNode};
10
11impl Icon {
12    pub fn as_html(&self) -> Html {
13        self.with_classes(Classes::new())
14    }
15
16    /// Wrap an [`Icon`] with a CSS style
17    pub fn with_style(&self, style: impl Into<AttrValue>) -> Styled<Icon> {
18        Styled {
19            content: *self,
20            style: Some(style.into()),
21        }
22    }
23
24    /// Wrap an [`Icon`] with a optional CSS style
25    pub fn with_optional_style(&self, style: impl Into<Option<AttrValue>>) -> Styled<Icon> {
26        Styled {
27            content: *self,
28            style: style.into(),
29        }
30    }
31
32    pub fn with_state(&self, state: State) -> Html {
33        self.with_state_weight(state, 200)
34    }
35
36    pub fn with_state_weight(&self, state: State, weight: usize) -> Html {
37        let style = state
38            .as_var(weight)
39            .map(|v| format!("color: var({});", v).into());
40        self.with_optional_style(style).into_html()
41    }
42
43    pub fn with_classes(&self, mut classes: Classes) -> Html {
44        self.extend_classes(&mut classes);
45
46        html! (
47            <i class={classes} aria-hidden="true"></i>
48        )
49    }
50}
51
52pub(crate) fn fas(name: &str) -> [&str; 2] {
53    ["fas", name]
54}
55
56#[cfg(feature = "icons-far")]
57pub(crate) fn far(name: &str) -> [&str; 2] {
58    ["far", name]
59}
60
61#[cfg(feature = "icons-fab")]
62pub(crate) fn fab(name: &str) -> [&str; 2] {
63    ["fab", name]
64}
65
66pub(crate) fn pf(name: &str) -> [&str; 2] {
67    ["pf-v5-pficon", name]
68}
69
70impl From<Icon> for VNode {
71    fn from(icon: Icon) -> Self {
72        icon.as_html()
73    }
74}
75
76impl IntoPropValue<Option<Html>> for Icon {
77    fn into_prop_value(self) -> Option<Html> {
78        Some(self.as_html())
79    }
80}
81
82impl ToHtml for Icon {
83    fn to_html(&self) -> Html {
84        self.as_html()
85    }
86}
87
88impl IntoPropValue<ChildrenRenderer<VNode>> for Icon {
89    fn into_prop_value(self) -> ChildrenRenderer<VNode> {
90        ChildrenRenderer::new(vec![self.into()])
91    }
92}