1use next_rust_view::{Element, Node, View, attrs::active_class, attrs::active_class_prefix};
4
5use crate::button::{common_methods, with_parts};
6use crate::icons::{self, icon};
7use crate::{Extra, apply, attr, root, styled};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum Width {
12 Sm,
14 Md,
16 Lg,
18 #[default]
20 Xl,
21 Xxl,
23 Full,
25}
26
27impl Width {
28 fn class(self) -> &'static str {
29 match self {
30 Width::Sm => "nr-container-sm",
31 Width::Md => "nr-container-md",
32 Width::Lg => "nr-container-lg",
33 Width::Xl => "nr-container-xl",
34 Width::Xxl => "nr-container-2xl",
35 Width::Full => "nr-container-full",
36 }
37 }
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum Align {
43 Start,
44 Center,
45 End,
46 Stretch,
47 Baseline,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum Justify {
53 Start,
54 Center,
55 End,
56 Between,
57 Around,
58 Evenly,
59}
60
61const GAPS: [u8; 11] = [0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 16];
63
64fn gap_class(n: u8) -> String {
65 let step = GAPS.iter().rev().find(|g| **g <= n).copied().unwrap_or(0);
66 format!("nr-gap-{step}")
67}
68
69#[derive(Default)]
71pub struct Container {
72 width: Width,
73 unstyled: bool,
74 extra: Extra,
75}
76
77impl Container {
78 pub fn new() -> Self {
79 Self::default()
80 }
81 common_methods!();
82 with_parts!();
83
84 pub fn width(mut self, v: Width) -> Self {
85 self.width = v;
86 self
87 }
88}
89
90impl View for Container {
91 fn into_node(self) -> Node {
92 let (class, attrs, children) = self.extra.split();
93 let mut el = root("div", &["nr-container", self.width.class()], self.unstyled, class);
94 apply(&mut el, attrs);
95 el.children = children;
96 styled(el)
97 }
98}
99
100#[derive(Default)]
108pub struct Stack {
109 row: bool,
110 wrap: bool,
111 gap: Option<u8>,
112 align: Option<Align>,
113 justify: Option<Justify>,
114 unstyled: bool,
115 extra: Extra,
116}
117
118impl Stack {
119 pub fn new() -> Self {
120 Self::default()
121 }
122 common_methods!();
123 with_parts!();
124
125 pub fn row(mut self, v: bool) -> Self {
127 self.row = v;
128 self
129 }
130 pub fn wrap(mut self, v: bool) -> Self {
132 self.wrap = v;
133 self
134 }
135 pub fn gap(mut self, v: u8) -> Self {
137 self.gap = Some(v);
138 self
139 }
140 pub fn align(mut self, v: Align) -> Self {
141 self.align = Some(v);
142 self
143 }
144 pub fn justify(mut self, v: Justify) -> Self {
145 self.justify = Some(v);
146 self
147 }
148}
149
150impl View for Stack {
151 fn into_node(self) -> Node {
152 let (class, attrs, children) = self.extra.split();
153 let gap = gap_class(self.gap.unwrap_or(4));
154 let align = match self.align {
155 None => "",
156 Some(Align::Start) => "nr-items-start",
157 Some(Align::Center) => "nr-items-center",
158 Some(Align::End) => "nr-items-end",
159 Some(Align::Stretch) => "nr-items-stretch",
160 Some(Align::Baseline) => "nr-items-baseline",
161 };
162 let justify = match self.justify {
163 None => "",
164 Some(Justify::Start) => "nr-justify-start",
165 Some(Justify::Center) => "nr-justify-center",
166 Some(Justify::End) => "nr-justify-end",
167 Some(Justify::Between) => "nr-justify-between",
168 Some(Justify::Around) => "nr-justify-around",
169 Some(Justify::Evenly) => "nr-justify-evenly",
170 };
171 let classes = [
172 "nr-stack",
173 if self.row { "nr-stack-row" } else { "" },
174 if self.wrap { "nr-stack-wrap" } else { "" },
175 &gap,
176 align,
177 justify,
178 ];
179 let mut el = root("div", &classes, self.unstyled, class);
180 apply(&mut el, attrs);
181 el.children = children;
182 styled(el)
183 }
184}
185
186#[derive(Default)]
189pub struct Grid {
190 cols: Option<u8>,
191 gap: Option<u8>,
192 min_width: Option<String>,
193 unstyled: bool,
194 extra: Extra,
195}
196
197impl Grid {
198 pub fn new() -> Self {
199 Self::default()
200 }
201 common_methods!();
202 with_parts!();
203
204 pub fn cols(mut self, v: u8) -> Self {
206 self.cols = Some(v.clamp(1, 12));
207 self
208 }
209 pub fn gap(mut self, v: u8) -> Self {
211 self.gap = Some(v);
212 self
213 }
214 pub fn min_width(mut self, v: impl Into<String>) -> Self {
216 self.min_width = Some(v.into());
217 self
218 }
219}
220
221impl View for Grid {
222 fn into_node(self) -> Node {
223 let (class, attrs, children) = self.extra.split();
224 let cols = format!("nr-cols-{}", self.cols.unwrap_or(3));
225 let gap = gap_class(self.gap.unwrap_or(4));
226 let mut el = if let Some(min) = &self.min_width {
227 let mut el = root("div", &["nr-grid", "nr-grid-fit", &gap], self.unstyled, class);
228 let min: String = min.chars().filter(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '%')).collect();
229 el.set_attr(attr("style", format!("--nr-grid-min:{min}")));
230 el
231 } else {
232 root("div", &["nr-grid", &cols, &gap], self.unstyled, class)
233 };
234 apply(&mut el, attrs);
235 el.children = children;
236 styled(el)
237 }
238}
239
240#[derive(Default)]
242pub struct Navbar {
243 brand: Option<Node>,
244 end_content: Option<Node>,
245 sticky: Option<bool>,
246 blurred: Option<bool>,
247 bordered: bool,
248 width: Width,
249 menu_toggle: bool,
250 unstyled: bool,
251 extra: Extra,
252}
253
254impl Navbar {
255 pub fn new() -> Self {
256 Self::default()
257 }
258 common_methods!();
259 with_parts!();
260
261 pub fn brand(mut self, v: impl View) -> Self {
263 self.brand = Some(v.into_node());
264 self
265 }
266 pub fn end_content(mut self, v: impl View) -> Self {
268 self.end_content = Some(v.into_node());
269 self
270 }
271 pub fn sticky(mut self, v: bool) -> Self {
273 self.sticky = Some(v);
274 self
275 }
276 pub fn blurred(mut self, v: bool) -> Self {
278 self.blurred = Some(v);
279 self
280 }
281 pub fn bordered(mut self, v: bool) -> Self {
283 self.bordered = v;
284 self
285 }
286 pub fn width(mut self, v: Width) -> Self {
288 self.width = v;
289 self
290 }
291 pub fn menu_toggle(mut self, v: bool) -> Self {
293 self.menu_toggle = v;
294 self
295 }
296}
297
298impl View for Navbar {
299 fn into_node(self) -> Node {
300 let (class, attrs, children) = self.extra.split();
301 let classes = [
302 "nr-navbar",
303 if self.sticky.unwrap_or(true) { "nr-navbar-sticky" } else { "" },
304 if self.blurred.unwrap_or(true) { "nr-navbar-blurred" } else { "" },
305 if self.bordered { "nr-navbar-bordered" } else { "" },
306 ];
307 let mut el = root("header", &classes, self.unstyled, class);
308 apply(&mut el, attrs);
309 let mut inner =
310 Element::new("div").with(attr("class", format!("nr-navbar-inner nr-container {}", self.width.class())));
311 if self.menu_toggle {
312 el.set_attr(attr("data-nr-ui", "shell"));
313 inner.children.push(
314 Element::new("button")
315 .with(attr("type", "button"))
316 .with(attr("class", "nr-navbar-menu"))
317 .with(attr("data-nr-shell-toggle", ""))
318 .with(attr("aria-label", "Menu"))
319 .with(attr("aria-expanded", "false"))
320 .with(icon(icons::menu()))
321 .into_node(),
322 );
323 }
324 if let Some(brand) = self.brand {
325 inner.children.push(Element::new("div").with(attr("class", "nr-navbar-brand")).with(brand).into_node());
326 }
327 if !children.is_empty() {
328 inner
329 .children
330 .push(Element::new("nav").with(attr("class", "nr-navbar-content")).with(children).into_node());
331 }
332 if let Some(end) = self.end_content {
333 inner.children.push(Element::new("div").with(attr("class", "nr-navbar-end")).with(end).into_node());
334 }
335 el.children.push(inner.into_node());
336 styled(el)
337 }
338}
339
340#[derive(Default)]
342pub struct NavbarItem {
343 href: Option<String>,
344 prefix: bool,
345 unstyled: bool,
346 extra: Extra,
347}
348
349impl NavbarItem {
350 pub fn new() -> Self {
351 Self::default()
352 }
353 common_methods!();
354 with_parts!();
355
356 pub fn href(mut self, v: impl Into<String>) -> Self {
357 self.href = Some(v.into());
358 self
359 }
360 pub fn prefix(mut self, v: bool) -> Self {
362 self.prefix = v;
363 self
364 }
365}
366
367impl View for NavbarItem {
368 fn into_node(self) -> Node {
369 nav_link("nr-navbar-link", self.href, self.prefix, None, self.unstyled, self.extra)
370 }
371}
372
373fn nav_link(
374 kind: &'static str,
375 href: Option<String>,
376 prefix: bool,
377 icon: Option<Node>,
378 unstyled: bool,
379 extra: Extra,
380) -> Node {
381 let (class, attrs, children) = extra.split();
382 let mut el = root("a", &[kind], unstyled, class);
383 el.set_attr(attr("href", href.unwrap_or_else(|| "#".into())));
384 let active = format!("{kind}-active");
385 el.set_attr(if prefix { active_class_prefix(active) } else { active_class(active) });
386 apply(&mut el, attrs);
387 if let Some(icon) = icon {
388 el.children.push(Element::new("span").with(attr("class", "nr-sidebar-icon")).with(icon).into_node());
389 }
390 el.children.push(Element::new("span").with(attr("class", "nr-nav-text")).with(children).into_node());
391 styled(el)
392}
393
394#[derive(Default)]
396pub struct Sidebar {
397 title: Option<String>,
398 unstyled: bool,
399 extra: Extra,
400}
401
402impl Sidebar {
403 pub fn new() -> Self {
404 Self::default()
405 }
406 common_methods!();
407 with_parts!();
408
409 pub fn title(mut self, v: impl Into<String>) -> Self {
411 self.title = Some(v.into());
412 self
413 }
414}
415
416impl View for Sidebar {
417 fn into_node(self) -> Node {
418 let (class, attrs, children) = self.extra.split();
419 let mut el = root("nav", &["nr-sidebar"], self.unstyled, class);
420 apply(&mut el, attrs);
421 if let Some(title) = self.title {
422 el.children.push(Element::new("p").with(attr("class", "nr-sidebar-title")).with(title).into_node());
423 }
424 el.children.extend(children);
425 styled(el)
426 }
427}
428
429#[derive(Default)]
431pub struct SidebarItem {
432 href: Option<String>,
433 icon: Option<Node>,
434 prefix: bool,
435 unstyled: bool,
436 extra: Extra,
437}
438
439impl SidebarItem {
440 pub fn new() -> Self {
441 Self::default()
442 }
443 common_methods!();
444 with_parts!();
445
446 pub fn href(mut self, v: impl Into<String>) -> Self {
447 self.href = Some(v.into());
448 self
449 }
450 pub fn icon(mut self, v: impl View) -> Self {
451 self.icon = Some(v.into_node());
452 self
453 }
454 pub fn prefix(mut self, v: bool) -> Self {
456 self.prefix = v;
457 self
458 }
459}
460
461impl View for SidebarItem {
462 fn into_node(self) -> Node {
463 nav_link("nr-sidebar-link", self.href, self.prefix, self.icon, self.unstyled, self.extra)
464 }
465}
466
467#[derive(Default)]
478pub struct AppShell {
479 navbar: Option<Node>,
480 sidebar: Option<Node>,
481 footer: Option<Node>,
482 unstyled: bool,
483 extra: Extra,
484}
485
486impl AppShell {
487 pub fn new() -> Self {
488 Self::default()
489 }
490 common_methods!();
491 with_parts!();
492
493 pub fn navbar(mut self, v: impl View) -> Self {
494 self.navbar = Some(v.into_node());
495 self
496 }
497 pub fn sidebar(mut self, v: impl View) -> Self {
498 self.sidebar = Some(v.into_node());
499 self
500 }
501 pub fn footer(mut self, v: impl View) -> Self {
502 self.footer = Some(v.into_node());
503 self
504 }
505}
506
507impl View for AppShell {
508 fn into_node(self) -> Node {
509 let (class, attrs, children) = self.extra.split();
510 let mut el = root(
511 "div",
512 &["nr-shell", if self.sidebar.is_some() { "nr-shell-with-sidebar" } else { "" }],
513 self.unstyled,
514 class,
515 );
516 apply(&mut el, attrs);
517 el.children.extend(self.navbar);
518 let mut body = Element::new("div").with(attr("class", "nr-shell-body"));
519 if let Some(sidebar) = self.sidebar {
520 body.children.push(Element::new("aside").with(attr("class", "nr-shell-sidebar")).with(sidebar).into_node());
521 body.children.push(
522 Element::new("div")
523 .with(attr("class", "nr-shell-scrim"))
524 .with(attr("data-nr-shell-toggle", ""))
525 .into_node(),
526 );
527 }
528 body.children.push(Element::new("main").with(attr("class", "nr-shell-main")).with(children).into_node());
529 el.children.push(body.into_node());
530 if let Some(footer) = self.footer {
531 el.children.push(Element::new("footer").with(attr("class", "nr-shell-footer")).with(footer).into_node());
532 }
533 styled(el)
534 }
535}