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
use crate::{use_theme, Icon, IconKind, Ripple};
use dioxus::prelude::*;

/// Chip component.
///
/// Chips help people enter information, make selections, filter content, or trigger actions.
///
/// [material.io](https://m3.material.io/components/chips)
///
/// ## Panics
/// This component requires access to a [`Theme`](crate::Theme) and [`IconFont`](crate::IconFont).
///
/// ## Examples
/// ```rust
///
/// use dioxus::prelude::*;
/// use dioxus_material::{Chip, Theme, IconFont};
///
/// fn app(cx: Scope) -> Element {
///     render!(Theme {
///         IconFont {}
///         div { display: "flex", gap: "10px",
///             Chip { onclick: |_| {}, "Asset chip" }
///             Chip { is_selected: true, onclick: |_| {}, "Asset chip" }
///         }
///     })
/// }
/// ```
#[component]
pub fn Chip<'a>(
    cx: Scope,
    children: Element<'a>,
    is_selected: Option<bool>,
    onclick: EventHandler<'a, Event<MouseData>>,
) -> Element<'a> {
    let theme = use_theme(cx);
    let (border_color, background) = if *is_selected == Some(true) {
        (
            &*theme.secondary_container_color,
            &*theme.secondary_container_color,
        )
    } else {
        ("#79747E", "none")
    };

    render!(
        div {
            display: "inline-flex",
            flex_direction: "row",
            align_items: "center",
            height: "32px",
            line_height: "32px",
            border_radius: "{theme.border_radius_small}",
            padding: "0 14px",
            font_family: "sans-serif",
            font_size: "14px",
            font_weight: 500,
            border: "1px solid {border_color}",
            background: background,
            Ripple { onclick: |event| onclick.call(event),
                if *is_selected == Some(true) {
                    render!(Icon { kind: IconKind::Check })
                } else {
                    None
                }
                children
            }
        }
    )
}