Skip to main content

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! (<i class={classes} aria-hidden="true" />)
47    }
48}
49
50pub(crate) fn fas(name: &str) -> [&str; 2] {
51    ["fas", name]
52}
53
54#[cfg(feature = "icons-far")]
55pub(crate) fn far(name: &str) -> [&str; 2] {
56    ["far", name]
57}
58
59#[cfg(feature = "icons-fab")]
60pub(crate) fn fab(name: &str) -> [&str; 2] {
61    ["fab", name]
62}
63
64pub(crate) fn pf(name: &str) -> [&str; 2] {
65    ["pf-v6-pficon", name]
66}
67
68impl From<Icon> for VNode {
69    fn from(icon: Icon) -> Self {
70        icon.as_html()
71    }
72}
73
74impl IntoPropValue<Option<Html>> for Icon {
75    fn into_prop_value(self) -> Option<Html> {
76        Some(self.as_html())
77    }
78}
79
80impl IntoPropValue<Html> for Icon {
81    fn into_prop_value(self) -> Html {
82        self.as_html()
83    }
84}
85
86impl IntoPropValue<ChildrenRenderer<VNode>> for Icon {
87    fn into_prop_value(self) -> ChildrenRenderer<VNode> {
88        ChildrenRenderer::new(vec![self.into()])
89    }
90}