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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![warn(missing_docs)]

//! `yew_octicons` is an easy interface for using
//! [Octicons](https://primer.style/octicons/) in [yew](https://yew.rs)
//! projects.
//!
//! ## Example
//!
//! ```
//! use yew::html;
//! use yew_octicons::Icon;
//! use yew_octicons::IconKind;
//!
//! let code = html! {
//!     <span>
//!         { Icon::new(IconKind::Alert) }
//!     </span>
//! };
//! ```

mod generated;

pub use generated::IconKind;

use yew::html;
use yew::html::Html;

/// An Octicon with size information. This can be used in yew's `html!` macro.
///
/// ```
/// use yew::html;
/// use yew_octicons::Icon;
/// use yew_octicons::IconKind;
///
/// let code = html! {
///     <span>
///         { Icon::new(IconKind::Alert) }
///     </span>
/// };
/// ```
#[derive(PartialEq, PartialOrd, Clone, Copy, Hash, Debug)]
pub struct Icon {
    kind: IconKind,
    size: usize,
}

impl Icon {
    /// Create a default-sized 16px icon
    ///
    /// ```
    /// use yew::html;
    /// use yew_octicons::Icon;
    /// use yew_octicons::IconKind;
    ///
    /// let code = html! {
    ///     <span>
    ///         { Icon::new(IconKind::Alert) }
    ///     </span>
    /// };
    /// ```
    pub fn new(kind: IconKind) -> Self {
        Self::new_sized(kind, 16)
    }

    /// Create a big 24px icon
    ///
    /// ```
    /// use yew::html;
    /// use yew_octicons::Icon;
    /// use yew_octicons::IconKind;
    ///
    /// let code = html! {
    ///     <span>
    ///         { Icon::new_big(IconKind::Alert) }
    ///     </span>
    /// };
    /// ```
    pub fn new_big(kind: IconKind) -> Self {
        Self::new_sized(kind, 24)
    }

    /// Create an icon of an arbitrary size. The `size` is the side length of
    /// the icon in pixels.
    ///
    /// ```
    /// use yew::html;
    /// use yew_octicons::Icon;
    /// use yew_octicons::IconKind;
    ///
    /// let code = html! {
    ///     <span>
    ///         { Icon::new_sized(IconKind::Alert, 50) }
    ///     </span>
    /// };
    /// ```
    pub fn new_sized(kind: IconKind, size: usize) -> Self {
        Self { kind, size }
    }
}

impl From<Icon> for Html {
    /// Convert icon to yew HTML. This allows icons to be used in yew's `html!`
    /// macro.
    fn from(icon: Icon) -> Self {
        let big = icon.size >= 24;
        let (path, big) = icon.kind.path(big);
        // The `viewBox` size should be the actual size of the icon, not the
        // requested size.
        let viewbox_size = if big { 24 } else { 16 };
        html! {
            <svg
                width={ icon.size.to_string() }
                height={ icon.size.to_string() }
                viewBox={ format!("0 0 {viewbox_size} {viewbox_size}") }
                fill="currentColor">

                <path fill-rule="evenodd" d={ path } />
            </svg>
        }
    }
}