1use next_rust_view::{Node, View};
4
5use crate::button::{common_methods, with_parts};
6use crate::{Extra, Press, Radius, apply, attr, root, styled};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum Shadow {
11 None,
12 Sm,
13 #[default]
14 Md,
15 Lg,
16}
17
18#[derive(Default)]
26pub struct Card {
27 shadow: Shadow,
28 radius: Option<Radius>,
29 bordered: bool,
30 blurred: bool,
31 hoverable: bool,
32 href: Option<String>,
33 press: Option<Press>,
34 unstyled: bool,
35 extra: Extra,
36}
37
38impl Card {
39 pub fn new() -> Self {
40 Self::default()
41 }
42 common_methods!();
43 with_parts!();
44
45 pub fn shadow(mut self, v: Shadow) -> Self {
46 self.shadow = v;
47 self
48 }
49 pub fn radius(mut self, v: Radius) -> Self {
50 self.radius = Some(v);
51 self
52 }
53 pub fn bordered(mut self, v: bool) -> Self {
55 self.bordered = v;
56 self
57 }
58 pub fn blurred(mut self, v: bool) -> Self {
60 self.blurred = v;
61 self
62 }
63 pub fn hoverable(mut self, v: bool) -> Self {
65 self.hoverable = v;
66 self
67 }
68 pub fn href(mut self, v: impl Into<String>) -> Self {
70 self.href = Some(v.into());
71 self
72 }
73 pub fn on_press(mut self, v: impl Into<Press>) -> Self {
75 self.press = Some(v.into());
76 self
77 }
78}
79
80impl View for Card {
81 fn into_node(self) -> Node {
82 let (class, attrs, children) = self.extra.split();
83 let shadow = match self.shadow {
84 Shadow::None => "",
85 Shadow::Sm => "nr-card-shadow-sm",
86 Shadow::Md => "nr-card-shadow-md",
87 Shadow::Lg => "nr-card-shadow-lg",
88 };
89 let pressable = self.href.is_some() || self.press.is_some();
90 let classes = [
91 "nr-card",
92 shadow,
93 self.radius.map(Radius::class).unwrap_or(""),
94 if self.bordered { "nr-card-bordered" } else { "" },
95 if self.blurred { "nr-card-blurred" } else { "" },
96 if self.hoverable || pressable { "nr-card-hoverable" } else { "" },
97 if pressable { "nr-card-pressable" } else { "" },
98 ];
99 let tag = if self.href.is_some() { "a" } else { "div" };
100 let mut el = root(tag, &classes, self.unstyled, class);
101 if let Some(href) = self.href {
102 el.set_attr(attr("href", href));
103 el.set_attr(attr("data-nr-link", ""));
104 }
105 if let Some(press) = &self.press {
106 el.set_attr(attr("role", "button"));
107 el.set_attr(attr("tabindex", "0"));
108 apply(&mut el, press.attrs());
109 }
110 apply(&mut el, attrs);
111 el.children = children;
112 styled(el)
113 }
114}
115
116macro_rules! card_part {
117 ($name:ident, $tag:literal, $class:literal, $doc:literal) => {
118 #[doc = $doc]
119 #[derive(Default)]
120 pub struct $name {
121 unstyled: bool,
122 extra: Extra,
123 }
124
125 impl $name {
126 pub fn new() -> Self {
127 Self::default()
128 }
129 common_methods!();
130 with_parts!();
131 }
132
133 impl View for $name {
134 fn into_node(self) -> Node {
135 let (class, attrs, children) = self.extra.split();
136 let mut el = root($tag, &[$class], self.unstyled, class);
137 apply(&mut el, attrs);
138 el.children = children;
139 styled(el)
140 }
141 }
142 };
143}
144
145card_part!(CardHeader, "div", "nr-card-header", "The top of a [`Card`].");
146card_part!(CardBody, "div", "nr-card-body", "The main content of a [`Card`].");
147card_part!(CardFooter, "div", "nr-card-footer", "The bottom of a [`Card`], typically actions.");