Skip to main content

next_rust_ui/
feedback.rs

1//! Spinners, chips and dividers.
2
3use next_rust_view::{Element, Node, View};
4
5use crate::button::{common_methods, with_parts};
6use crate::{Color, Extra, Radius, Size, Variant, apply, attr, root, styled};
7
8/// The spinner markup used inside other components.
9pub(crate) fn spinner_node(class: &str) -> Node {
10    Element::new("span")
11        .with(attr("class", format!("nr-spinner {class}")))
12        .with(attr("aria-hidden", "true"))
13        .with(Element::new("i").with(attr("class", "nr-spinner-ring")))
14        .into_node()
15}
16
17/// A loading indicator.
18#[derive(Default)]
19pub struct Spinner {
20    color: Color,
21    size: Size,
22    label: Option<String>,
23    unstyled: bool,
24    extra: Extra,
25}
26
27impl Spinner {
28    pub fn new() -> Self {
29        Self::default()
30    }
31    common_methods!();
32    with_parts!();
33
34    pub fn color(mut self, v: Color) -> Self {
35        self.color = v;
36        self
37    }
38    pub fn size(mut self, v: Size) -> Self {
39        self.size = v;
40        self
41    }
42    /// Text shown next to the spinner (also its accessible name).
43    pub fn label(mut self, v: impl Into<String>) -> Self {
44        self.label = Some(v.into());
45        self
46    }
47}
48
49impl View for Spinner {
50    fn into_node(self) -> Node {
51        let (class, attrs, children) = self.extra.split();
52        let size = format!("nr-spinner-{}", self.size.suffix());
53        let mut el = root("span", &["nr-spinner", &size, self.color.class()], self.unstyled, class);
54        el.set_attr(attr("role", "status"));
55        el.set_attr(attr("aria-label", self.label.clone().unwrap_or_else(|| "Loading".into())));
56        apply(&mut el, attrs);
57        el.children.push(Element::new("i").with(attr("class", "nr-spinner-ring")).into_node());
58        if let Some(label) = self.label {
59            el.children.push(Element::new("span").with(attr("class", "nr-spinner-label")).with(label).into_node());
60        }
61        el.children.extend(children);
62        styled(el)
63    }
64}
65
66/// A small label: a tag, a status, a count.
67#[derive(Default)]
68pub struct Chip {
69    color: Color,
70    variant: Variant,
71    size: Size,
72    radius: Option<Radius>,
73    dot: bool,
74    start_content: Option<Node>,
75    end_content: Option<Node>,
76    unstyled: bool,
77    extra: Extra,
78}
79
80impl Chip {
81    pub fn new() -> Self {
82        Self { color: Color::Default, ..Self::default() }
83    }
84    common_methods!();
85    with_parts!();
86
87    pub fn color(mut self, v: Color) -> Self {
88        self.color = v;
89        self
90    }
91    pub fn variant(mut self, v: Variant) -> Self {
92        self.variant = v;
93        self
94    }
95    pub fn size(mut self, v: Size) -> Self {
96        self.size = v;
97        self
98    }
99    pub fn radius(mut self, v: Radius) -> Self {
100        self.radius = Some(v);
101        self
102    }
103    /// A colored dot before the text.
104    pub fn dot(mut self, v: bool) -> Self {
105        self.dot = v;
106        self
107    }
108    pub fn start_content(mut self, v: impl View) -> Self {
109        self.start_content = Some(v.into_node());
110        self
111    }
112    pub fn end_content(mut self, v: impl View) -> Self {
113        self.end_content = Some(v.into_node());
114        self
115    }
116}
117
118impl View for Chip {
119    fn into_node(self) -> Node {
120        let (class, attrs, children) = self.extra.split();
121        let variant = format!("nr-chip-{}", self.variant.suffix());
122        let size = format!("nr-chip-{}", self.size.suffix());
123        let classes = ["nr-chip", &variant, self.color.class(), &size, self.radius.map(Radius::class).unwrap_or("")];
124        let mut el = root("span", &classes, self.unstyled, class);
125        apply(&mut el, attrs);
126        if self.dot {
127            el.children.push(Element::new("span").with(attr("class", "nr-chip-dot")).into_node());
128        }
129        el.children.extend(self.start_content);
130        el.children.push(Element::new("span").with(attr("class", "nr-chip-content")).with(children).into_node());
131        el.children.extend(self.end_content);
132        styled(el)
133    }
134}
135
136/// A thin line between content.
137#[derive(Default)]
138pub struct Divider {
139    vertical: bool,
140    unstyled: bool,
141    extra: Extra,
142}
143
144impl Divider {
145    pub fn new() -> Self {
146        Self::default()
147    }
148    common_methods!();
149    with_parts!();
150
151    pub fn vertical(mut self, v: bool) -> Self {
152        self.vertical = v;
153        self
154    }
155}
156
157impl View for Divider {
158    fn into_node(self) -> Node {
159        let (class, attrs, _) = self.extra.split();
160        let mut el = root("hr", &["nr-divider", if self.vertical { "nr-divider-v" } else { "" }], self.unstyled, class);
161        el.void = true;
162        el.set_attr(attr("aria-orientation", if self.vertical { "vertical" } else { "horizontal" }));
163        apply(&mut el, attrs);
164        styled(el)
165    }
166}