zino_dioxus/icon/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Icon fonts or SVG icon shapes.

use crate::class::Class;
use dioxus::prelude::*;
use dioxus_free_icons::IconShape;

/// A container for any type of icon fonts.
pub fn Icon(props: IconProps) -> Element {
    if props.icon_class.is_empty() {
        rsx! {
            span {
                class: props.class,
                { props.children }
            }
        }
    } else {
        rsx! {
            span {
                class: props.class,
                i {
                    class: props.icon_class,
                }
            }
        }
    }
}

/// The [`Icon`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct IconProps {
    /// The class attribute for the component.
    #[props(into, default = "icon")]
    pub class: Class,
    /// The class to apply to the `<i>` element for a icon font.
    #[props(into, default)]
    pub icon_class: Class,
    /// The children to render within the component.
    children: Element,
}

/// A container for a SVG icon.
pub fn SvgIcon<T: IconShape + Clone + PartialEq + 'static>(props: SvgIconProps<T>) -> Element {
    let width = props.width;
    let height = props.height.unwrap_or(width);
    let style = if props.intrinsic {
        format!("width:{width}px;height:{height}px")
    } else {
        String::new()
    };
    rsx! {
        span {
            class: props.class,
            style: "{style}",
            dioxus_free_icons::Icon {
                icon: props.shape,
                width: width,
                height: height,
            }
        }
    }
}

/// The [`SvgIcon`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct SvgIconProps<T: IconShape + Clone + PartialEq + 'static> {
    /// The class attribute for the component.
    #[props(into, default = "icon")]
    pub class: Class,
    /// The icon shape to use.
    pub shape: T,
    /// The width of the `<svg>` element. Defaults to 20.
    #[props(default = 20)]
    pub width: u32,
    /// The height of the `<svg>` element.
    #[props(into)]
    pub height: Option<u32>,
    /// A flag to use the instrinsic size for the icon.
    #[props(default = false)]
    pub intrinsic: bool,
}

/// A wrapper for combining an icon with text.
pub fn IconText(props: IconTextProps) -> Element {
    rsx! {
        span {
            class: "{props.class}",
            { props.children }
        }
    }
}

/// The [`IconText`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct IconTextProps {
    /// The class attribute for the component.
    #[props(into, default = "icon-text")]
    pub class: Class,
    /// The children to render within the component.
    children: Element,
}