1use next_rust_view::{Element, Node, View};
5
6use crate::button::{common_methods, with_parts};
7use crate::icons::{self, icon};
8use crate::{Color, Extra, Radius, Size, Variant, apply, attr, flag, next_id, root, styled};
9
10#[derive(Default)]
19pub struct Alert {
20 color: Color,
21 variant: Variant,
22 radius: Option<Radius>,
23 title: Option<String>,
24 icon: Option<Node>,
25 hide_icon: bool,
26 closable: bool,
27 end_content: Option<Node>,
28 unstyled: bool,
29 extra: Extra,
30}
31
32impl Alert {
33 pub fn new() -> Self {
34 Self { variant: Variant::Flat, ..Self::default() }
35 }
36 common_methods!();
37 with_parts!();
38
39 pub fn color(mut self, v: Color) -> Self {
40 self.color = v;
41 self
42 }
43 pub fn variant(mut self, v: Variant) -> Self {
45 self.variant = v;
46 self
47 }
48 pub fn radius(mut self, v: Radius) -> Self {
49 self.radius = Some(v);
50 self
51 }
52 pub fn title(mut self, v: impl Into<String>) -> Self {
54 self.title = Some(v.into());
55 self
56 }
57 pub fn icon(mut self, v: impl View) -> Self {
59 self.icon = Some(v.into_node());
60 self
61 }
62 pub fn hide_icon(mut self, v: bool) -> Self {
63 self.hide_icon = v;
64 self
65 }
66 pub fn closable(mut self, v: bool) -> Self {
68 self.closable = v;
69 self
70 }
71 pub fn end_content(mut self, v: impl View) -> Self {
73 self.end_content = Some(v.into_node());
74 self
75 }
76}
77
78impl View for Alert {
79 fn into_node(self) -> Node {
80 let (class, attrs, children) = self.extra.split();
81 let variant = match self.variant {
82 Variant::Solid => "nr-alert-solid",
83 Variant::Bordered => "nr-alert-bordered",
84 Variant::Faded => "nr-alert-faded",
85 _ => "nr-alert-flat",
86 };
87 let classes = ["nr-alert", variant, self.color.class(), self.radius.map(Radius::class).unwrap_or("")];
88 let mut el = root("div", &classes, self.unstyled, class);
89 el.set_attr(attr(
90 "role",
91 if matches!(self.color, Color::Danger | Color::Warning) { "alert" } else { "status" },
92 ));
93 if self.closable {
94 el.set_attr(attr("data-nr-ui", "alert"));
95 }
96 apply(&mut el, attrs);
97 if !self.hide_icon {
98 let glyph = self.icon.unwrap_or_else(|| {
99 icon(match self.color {
100 Color::Success => icons::circle_check(),
101 Color::Warning => icons::triangle_alert(),
102 Color::Danger => icons::circle_x(),
103 _ => icons::info(),
104 })
105 });
106 el.children.push(Element::new("span").with(attr("class", "nr-alert-icon")).with(glyph).into_node());
107 }
108 let mut body = Element::new("div").with(attr("class", "nr-alert-body"));
109 if let Some(title) = self.title {
110 body.children.push(Element::new("strong").with(attr("class", "nr-alert-title")).with(title).into_node());
111 }
112 if !children.is_empty() {
113 body.children.push(Element::new("div").with(attr("class", "nr-alert-text")).with(children).into_node());
114 }
115 el.children.push(body.into_node());
116 if let Some(end) = self.end_content {
117 el.children.push(Element::new("div").with(attr("class", "nr-alert-end")).with(end).into_node());
118 }
119 if self.closable {
120 el.children.push(
121 Element::new("button")
122 .with(attr("type", "button"))
123 .with(attr("class", "nr-alert-close"))
124 .with(attr("aria-label", "Dismiss"))
125 .with(attr("data-nr-dismiss", ""))
126 .with(icon(icons::close()))
127 .into_node(),
128 );
129 }
130 styled(el)
131 }
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
136pub enum Placement {
137 #[default]
138 TopRight,
139 TopLeft,
140 BottomRight,
141 BottomLeft,
142}
143
144impl Placement {
145 fn class(self) -> &'static str {
146 match self {
147 Placement::TopRight => "nr-badge-tr",
148 Placement::TopLeft => "nr-badge-tl",
149 Placement::BottomRight => "nr-badge-br",
150 Placement::BottomLeft => "nr-badge-bl",
151 }
152 }
153}
154
155#[derive(Default)]
163pub struct Badge {
164 content: Option<String>,
165 color: Color,
166 variant: Variant,
167 size: Size,
168 placement: Placement,
169 dot: bool,
170 hidden: bool,
171 unstyled: bool,
172 extra: Extra,
173}
174
175impl Badge {
176 pub fn new() -> Self {
177 Self { color: Color::Danger, ..Self::default() }
178 }
179 common_methods!();
180 with_parts!();
181
182 pub fn content(mut self, v: impl Into<String>) -> Self {
184 self.content = Some(v.into());
185 self
186 }
187 pub fn color(mut self, v: Color) -> Self {
188 self.color = v;
189 self
190 }
191 pub fn variant(mut self, v: Variant) -> Self {
193 self.variant = v;
194 self
195 }
196 pub fn size(mut self, v: Size) -> Self {
197 self.size = v;
198 self
199 }
200 pub fn placement(mut self, v: Placement) -> Self {
201 self.placement = v;
202 self
203 }
204 pub fn dot(mut self, v: bool) -> Self {
206 self.dot = v;
207 self
208 }
209 pub fn hidden(mut self, v: bool) -> Self {
211 self.hidden = v;
212 self
213 }
214}
215
216impl View for Badge {
217 fn into_node(self) -> Node {
218 let (class, attrs, children) = self.extra.split();
219 let mut el = root("span", &["nr-badge"], self.unstyled, class);
220 apply(&mut el, attrs);
221 el.children = children;
222 if !self.hidden {
223 let variant = match self.variant {
224 Variant::Flat => "nr-badge-flat",
225 Variant::Faded => "nr-badge-faded",
226 _ => "",
227 };
228 let size = format!("nr-badge-{}", self.size.suffix());
229 let mut badge = Element::new("span").with(attr(
230 "class",
231 [
232 "nr-badge-content",
233 self.placement.class(),
234 self.color.class(),
235 &size,
236 variant,
237 if self.dot { "nr-badge-dot" } else { "" },
238 ]
239 .iter()
240 .filter(|c| !c.is_empty())
241 .copied()
242 .collect::<Vec<_>>()
243 .join(" "),
244 ));
245 if !self.dot {
246 badge = badge.with(self.content.unwrap_or_default());
247 }
248 el.children.push(badge.into_node());
249 }
250 styled(el)
251 }
252}
253
254#[derive(Default)]
263pub struct Progress {
264 value: Option<f64>,
265 min: f64,
266 max: Option<f64>,
267 color: Color,
268 size: Size,
269 radius: Option<Radius>,
270 label: Option<String>,
271 show_value: bool,
272 value_label: Option<String>,
273 striped: bool,
274 unstyled: bool,
275 extra: Extra,
276}
277
278impl Progress {
279 pub fn new() -> Self {
280 Self::default()
281 }
282 common_methods!();
283 with_parts!();
284
285 pub fn value(mut self, v: f64) -> Self {
287 self.value = Some(v);
288 self
289 }
290 pub fn min(mut self, v: f64) -> Self {
291 self.min = v;
292 self
293 }
294 pub fn max(mut self, v: f64) -> Self {
296 self.max = Some(v);
297 self
298 }
299 pub fn color(mut self, v: Color) -> Self {
300 self.color = v;
301 self
302 }
303 pub fn size(mut self, v: Size) -> Self {
304 self.size = v;
305 self
306 }
307 pub fn radius(mut self, v: Radius) -> Self {
308 self.radius = Some(v);
309 self
310 }
311 pub fn label(mut self, v: impl Into<String>) -> Self {
313 self.label = Some(v.into());
314 self
315 }
316 pub fn show_value(mut self, v: bool) -> Self {
318 self.show_value = v;
319 self
320 }
321 pub fn value_label(mut self, v: impl Into<String>) -> Self {
323 self.value_label = Some(v.into());
324 self
325 }
326 pub fn striped(mut self, v: bool) -> Self {
327 self.striped = v;
328 self
329 }
330}
331
332fn percent(value: f64, min: f64, max: f64) -> (f64, String) {
334 let span = (max - min).max(f64::EPSILON);
335 let ratio = ((value - min) / span).clamp(0.0, 1.0);
336 ((ratio * 1000.0).round() / 10.0, trim_float(value))
337}
338
339fn trim_float(v: f64) -> String {
340 if v.fract() == 0.0 && v.abs() < 1e15 { format!("{}", v as i64) } else { format!("{v}") }
341}
342
343impl View for Progress {
344 fn into_node(self) -> Node {
345 let (class, attrs, children) = self.extra.split();
346 let max = self.max.unwrap_or(100.0);
347 let size = format!("nr-progress-{}", self.size.suffix());
348 let classes = [
349 "nr-progress",
350 &size,
351 self.color.class(),
352 self.radius.map(Radius::class).unwrap_or(""),
353 if self.striped { "nr-progress-striped" } else { "" },
354 if self.value.is_none() { "nr-progress-indeterminate" } else { "" },
355 ];
356 let mut el = root("div", &classes, self.unstyled, class);
357 el.set_attr(attr("role", "progressbar"));
358 el.set_attr(attr("aria-valuemin", trim_float(self.min)));
359 el.set_attr(attr("aria-valuemax", trim_float(max)));
360 let mut pct = None;
361 if let Some(value) = self.value {
362 let (p, now) = percent(value, self.min, max);
363 el.set_attr(attr("aria-valuenow", now));
364 pct = Some(p);
365 }
366 let label_id = self.label.as_ref().map(|_| next_id("nr-progress"));
367 match (&self.label, &label_id) {
368 (Some(_), Some(id)) => el.set_attr(attr("aria-labelledby", id.clone())),
369 _ => el.set_attr(attr("aria-label", "Progress")),
370 }
371 apply(&mut el, attrs);
372 if self.label.is_some() || self.show_value || self.value_label.is_some() {
373 let mut line = Element::new("div").with(attr("class", "nr-progress-label"));
374 if let (Some(label), Some(id)) = (self.label, label_id) {
375 line.children.push(Element::new("span").with(attr("id", id)).with(label).into_node());
376 }
377 let shown = self.value_label.or_else(|| match (self.show_value, pct) {
378 (true, Some(p)) => Some(format!("{}%", trim_float(p))),
379 _ => None,
380 });
381 if let Some(text) = shown {
382 line.children
383 .push(Element::new("span").with(attr("class", "nr-progress-value")).with(text).into_node());
384 }
385 el.children.push(line.into_node());
386 }
387 let mut bar = Element::new("div").with(attr("class", "nr-progress-bar"));
388 if let Some(p) = pct {
389 bar.set_attr(attr("style", format!("width:{}%", trim_float(p))));
390 }
391 el.children.push(Element::new("div").with(attr("class", "nr-progress-track")).with(bar).into_node());
392 el.children.extend(children);
393 styled(el)
394 }
395}
396
397#[derive(Default)]
406pub struct CircularProgress {
407 value: Option<f64>,
408 max: Option<f64>,
409 color: Color,
410 size: Size,
411 label: Option<String>,
412 show_value: bool,
413 value_label: Option<String>,
414 unstyled: bool,
415 extra: Extra,
416}
417
418impl CircularProgress {
419 pub fn new() -> Self {
420 Self::default()
421 }
422 common_methods!();
423 with_parts!();
424
425 pub fn value(mut self, v: f64) -> Self {
427 self.value = Some(v);
428 self
429 }
430 pub fn max(mut self, v: f64) -> Self {
432 self.max = Some(v);
433 self
434 }
435 pub fn color(mut self, v: Color) -> Self {
436 self.color = v;
437 self
438 }
439 pub fn size(mut self, v: Size) -> Self {
440 self.size = v;
441 self
442 }
443 pub fn label(mut self, v: impl Into<String>) -> Self {
445 self.label = Some(v.into());
446 self
447 }
448 pub fn show_value(mut self, v: bool) -> Self {
450 self.show_value = v;
451 self
452 }
453 pub fn value_label(mut self, v: impl Into<String>) -> Self {
455 self.value_label = Some(v.into());
456 self
457 }
458}
459
460impl View for CircularProgress {
461 fn into_node(self) -> Node {
462 let (class, attrs, children) = self.extra.split();
463 let max = self.max.unwrap_or(100.0);
464 let size = format!("nr-ring-{}", self.size.suffix());
465 let classes =
466 ["nr-ring", &size, self.color.class(), if self.value.is_none() { "nr-ring-indeterminate" } else { "" }];
467 let mut el = root("div", &classes, self.unstyled, class);
468 el.set_attr(attr("role", "progressbar"));
469 el.set_attr(attr("aria-valuemin", "0"));
470 el.set_attr(attr("aria-valuemax", trim_float(max)));
471 let mut pct = None;
472 if let Some(value) = self.value {
473 let (p, now) = percent(value, 0.0, max);
474 el.set_attr(attr("aria-valuenow", now));
475 el.set_attr(attr("style", format!("--p:{}", trim_float(p))));
476 pct = Some(p);
477 }
478 match &self.label {
479 Some(label) => el.set_attr(attr("aria-label", label.clone())),
480 None => el.set_attr(attr("aria-label", "Progress")),
481 }
482 apply(&mut el, attrs);
483 let circle = |class: &'static str| {
485 Element::new("circle")
486 .with(attr("class", class))
487 .with(attr("cx", "18"))
488 .with(attr("cy", "18"))
489 .with(attr("r", "16"))
490 .with(attr("fill", "none"))
491 .with(attr("stroke-width", "3"))
492 .into_node()
493 };
494 let svg = Element::new("svg")
495 .with(attr("class", "nr-ring-svg"))
496 .with(attr("viewBox", "0 0 36 36"))
497 .with(attr("aria-hidden", "true"))
498 .with(circle("nr-ring-track"))
499 .with(circle("nr-ring-fill"));
500 let mut face = Element::new("div").with(attr("class", "nr-ring-face")).with(svg);
501 let shown = self.value_label.or_else(|| match (self.show_value, pct) {
502 (true, Some(p)) => Some(format!("{}%", trim_float(p))),
503 _ => None,
504 });
505 if let Some(text) = shown {
506 face.children.push(Element::new("span").with(attr("class", "nr-ring-value")).with(text).into_node());
507 }
508 el.children.push(face.into_node());
509 if let Some(label) = self.label {
510 el.children.push(Element::new("span").with(attr("class", "nr-ring-label")).with(label).into_node());
511 }
512 el.children.extend(children);
513 styled(el)
514 }
515}
516
517#[derive(Default)]
525pub struct Skeleton {
526 lines: u8,
527 radius: Option<Radius>,
528 unstyled: bool,
529 extra: Extra,
530}
531
532impl Skeleton {
533 pub fn new() -> Self {
534 Self::default()
535 }
536 common_methods!();
537 with_parts!();
538
539 pub fn lines(mut self, v: u8) -> Self {
541 self.lines = v;
542 self
543 }
544 pub fn radius(mut self, v: Radius) -> Self {
545 self.radius = Some(v);
546 self
547 }
548}
549
550impl View for Skeleton {
551 fn into_node(self) -> Node {
552 let (class, attrs, children) = self.extra.split();
553 let lines = self.lines > 0;
554 let classes =
555 ["nr-skeleton", if lines { "nr-skeleton-lines" } else { "" }, self.radius.map(Radius::class).unwrap_or("")];
556 let mut el = root("div", &classes, self.unstyled, class);
557 el.set_attr(attr("aria-hidden", "true"));
558 apply(&mut el, attrs);
559 for _ in 0..self.lines {
560 el.children.push(Element::new("span").with(attr("class", "nr-skeleton-line")).into_node());
561 }
562 el.children.extend(children);
563 styled(el)
564 }
565}
566
567#[derive(Debug, Clone, Copy, PartialEq, Eq)]
569pub enum Trend {
570 Up,
571 Down,
572 Flat,
573}
574
575#[derive(Default)]
584pub struct Stat {
585 label: Option<String>,
586 value: Option<String>,
587 delta: Option<String>,
588 trend: Option<Trend>,
589 description: Option<String>,
590 icon: Option<Node>,
591 color: Option<Color>,
592 unstyled: bool,
593 extra: Extra,
594}
595
596impl Stat {
597 pub fn new() -> Self {
598 Self::default()
599 }
600 common_methods!();
601 with_parts!();
602
603 pub fn label(mut self, v: impl Into<String>) -> Self {
604 self.label = Some(v.into());
605 self
606 }
607 pub fn value(mut self, v: impl Into<String>) -> Self {
608 self.value = Some(v.into());
609 self
610 }
611 pub fn delta(mut self, v: impl Into<String>) -> Self {
613 self.delta = Some(v.into());
614 self
615 }
616 pub fn trend(mut self, v: Trend) -> Self {
617 self.trend = Some(v);
618 self
619 }
620 pub fn description(mut self, v: impl Into<String>) -> Self {
622 self.description = Some(v.into());
623 self
624 }
625 pub fn icon(mut self, v: impl View) -> Self {
627 self.icon = Some(v.into_node());
628 self
629 }
630 pub fn color(mut self, v: Color) -> Self {
632 self.color = Some(v);
633 self
634 }
635}
636
637impl View for Stat {
638 fn into_node(self) -> Node {
639 let (class, attrs, children) = self.extra.split();
640 let mut el = root("div", &["nr-stat", self.color.map(Color::class).unwrap_or("")], self.unstyled, class);
641 apply(&mut el, attrs);
642 let mut body = Element::new("div").with(attr("class", "nr-stat-body"));
643 if let Some(label) = self.label {
644 body.children.push(Element::new("span").with(attr("class", "nr-stat-label")).with(label).into_node());
645 }
646 let mut line = Element::new("div").with(attr("class", "nr-stat-line"));
647 if let Some(value) = self.value {
648 line.children.push(Element::new("span").with(attr("class", "nr-stat-value")).with(value).into_node());
649 }
650 if let Some(delta) = self.delta {
651 let (trend_class, glyph) = match self.trend {
652 Some(Trend::Up) => ("nr-stat-up", Some(icons::trending_up())),
653 Some(Trend::Down) => ("nr-stat-down", Some(icons::trending_down())),
654 Some(Trend::Flat) => ("nr-stat-flat", Some(icons::minus())),
655 None => ("", None),
656 };
657 let mut d =
658 Element::new("span").with(attr("class", format!("nr-stat-delta {trend_class}").trim_end().to_owned()));
659 if let Some(g) = glyph {
660 d.children.push(icon(g));
661 }
662 line.children.push(d.with(delta).into_node());
663 }
664 body.children.push(line.into_node());
665 if let Some(description) = self.description {
666 body.children
667 .push(Element::new("span").with(attr("class", "nr-stat-description")).with(description).into_node());
668 }
669 body.children.extend(children);
670 el.children.push(body.into_node());
671 if let Some(glyph) = self.icon {
672 el.children.push(Element::new("span").with(attr("class", "nr-stat-icon")).with(glyph).into_node());
673 }
674 styled(el)
675 }
676}
677
678#[derive(Default)]
685pub struct Kbd {
686 unstyled: bool,
687 extra: Extra,
688}
689
690impl Kbd {
691 pub fn new() -> Self {
692 Self::default()
693 }
694 common_methods!();
695 with_parts!();
696}
697
698impl View for Kbd {
699 fn into_node(self) -> Node {
700 let (class, attrs, children) = self.extra.split();
701 let mut el = root("kbd", &["nr-kbd"], self.unstyled, class);
702 apply(&mut el, attrs);
703 for child in children {
704 el.children.push(Element::new("kbd").with(child).into_node());
705 }
706 styled(el)
707 }
708}
709
710#[derive(Default)]
719pub struct EmptyState {
720 icon: Option<Node>,
721 title: Option<String>,
722 description: Option<String>,
723 compact: bool,
724 unstyled: bool,
725 extra: Extra,
726}
727
728impl EmptyState {
729 pub fn new() -> Self {
730 Self::default()
731 }
732 common_methods!();
733 with_parts!();
734
735 pub fn icon(mut self, v: impl View) -> Self {
737 self.icon = Some(v.into_node());
738 self
739 }
740 pub fn title(mut self, v: impl Into<String>) -> Self {
741 self.title = Some(v.into());
742 self
743 }
744 pub fn description(mut self, v: impl Into<String>) -> Self {
745 self.description = Some(v.into());
746 self
747 }
748 pub fn compact(mut self, v: bool) -> Self {
750 self.compact = v;
751 self
752 }
753}
754
755impl View for EmptyState {
756 fn into_node(self) -> Node {
757 let (class, attrs, children) = self.extra.split();
758 let mut el =
759 root("div", &["nr-empty", if self.compact { "nr-empty-compact" } else { "" }], self.unstyled, class);
760 apply(&mut el, attrs);
761 let glyph = self.icon.unwrap_or_else(|| icon(icons::inbox()));
762 el.children.push(Element::new("span").with(attr("class", "nr-empty-icon")).with(glyph).into_node());
763 if let Some(title) = self.title {
764 el.children.push(Element::new("h3").with(attr("class", "nr-empty-title")).with(title).into_node());
765 }
766 if let Some(description) = self.description {
767 el.children
768 .push(Element::new("p").with(attr("class", "nr-empty-description")).with(description).into_node());
769 }
770 if !children.is_empty() {
771 el.children.push(Element::new("div").with(attr("class", "nr-empty-actions")).with(children).into_node());
772 }
773 styled(el)
774 }
775}
776
777fn first_element(nodes: &mut [Node]) -> Option<&mut Element> {
780 for node in nodes {
781 match node {
782 Node::Element(el) => return Some(el),
783 Node::Fragment(inner) => {
784 if let Some(el) = first_element(inner) {
785 return Some(el);
786 }
787 }
788 _ => {}
789 }
790 }
791 None
792}
793
794#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
796pub enum Side {
797 #[default]
798 Top,
799 Bottom,
800 Left,
801 Right,
802}
803
804impl Side {
805 fn class(self) -> &'static str {
806 match self {
807 Side::Top => "nr-tooltip-top",
808 Side::Bottom => "nr-tooltip-bottom",
809 Side::Left => "nr-tooltip-left",
810 Side::Right => "nr-tooltip-right",
811 }
812 }
813}
814
815#[derive(Default)]
826pub struct Tooltip {
827 content: Option<String>,
828 side: Side,
829 color: Option<Color>,
830 unstyled: bool,
831 extra: Extra,
832}
833
834impl Tooltip {
835 pub fn new() -> Self {
836 Self::default()
837 }
838 common_methods!();
839 with_parts!();
840
841 pub fn content(mut self, v: impl Into<String>) -> Self {
843 self.content = Some(v.into());
844 self
845 }
846 pub fn side(mut self, v: Side) -> Self {
847 self.side = v;
848 self
849 }
850 pub fn color(mut self, v: Color) -> Self {
852 self.color = Some(v);
853 self
854 }
855}
856
857impl View for Tooltip {
858 fn into_node(self) -> Node {
859 let (class, attrs, children) = self.extra.split();
860 let classes = ["nr-tooltip", self.side.class(), self.color.map(Color::class).unwrap_or("")];
861 let mut el = root("span", &["nr-tooltip-anchor"], self.unstyled, class);
862 apply(&mut el, attrs);
863 let id = next_id("nr-tip");
864 let mut children = children;
866 if let Some(anchor) = first_element(&mut children) {
867 anchor.set_attr(attr("aria-describedby", id.clone()));
868 anchor.set_attr(flag("data-nr-tooltip-anchor", true));
869 }
870 el.children = children;
871 el.children.push(
872 Element::new("span")
873 .with(attr("class", classes.iter().filter(|c| !c.is_empty()).copied().collect::<Vec<_>>().join(" ")))
874 .with(attr("role", "tooltip"))
875 .with(attr("id", id))
876 .with(self.content.unwrap_or_default())
877 .into_node(),
878 );
879 styled(el)
880 }
881}