Skip to main content

next_rust_ui/
navigation.rs

1//! Tabs, breadcrumbs, steps and accordions.
2
3use next_rust_view::{Attr, Element, Node, View};
4
5use crate::button::{common_methods, with_parts};
6use crate::icons::{self, icon};
7use crate::{Color, Extra, Radius, Size, apply, attr, flag, next_id, root, styled};
8
9// ---------------------------------------------------------------------------
10// Tabs
11// ---------------------------------------------------------------------------
12
13/// Look of a [`Tabs`] strip.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub enum TabsVariant {
16    /// A segmented control: a soft track with the selected tab raised.
17    #[default]
18    Solid,
19    /// Text with a line under the selected tab.
20    Underlined,
21    /// An outlined track.
22    Bordered,
23    /// Text only, the selected tab tinted.
24    Light,
25}
26
27impl TabsVariant {
28    fn class(self) -> &'static str {
29        match self {
30            TabsVariant::Solid => "nr-tabs-solid",
31            TabsVariant::Underlined => "nr-tabs-underlined",
32            TabsVariant::Bordered => "nr-tabs-bordered",
33            TabsVariant::Light => "nr-tabs-light",
34        }
35    }
36}
37
38/// One tab. With `href` it is a link (the page decides which is selected,
39/// by `selected` or by matching the current path with the client runtime);
40/// with children it is a panel the component script switches to.
41///
42/// `Tab![key = "grades", title = "Grades", p!["…"]]`
43#[derive(Default)]
44pub struct Tab {
45    key: Option<String>,
46    title: Option<String>,
47    href: Option<String>,
48    selected: bool,
49    disabled: bool,
50    start_content: Option<Node>,
51    end_content: Option<Node>,
52    extra: Extra,
53}
54
55impl Tab {
56    pub fn new() -> Self {
57        Self::default()
58    }
59    with_parts!();
60
61    /// Identifies the tab (`Tabs![selected = key]`); defaults to its index.
62    pub fn key(mut self, v: impl Into<String>) -> Self {
63        self.key = Some(v.into());
64        self
65    }
66    /// The text of the tab.
67    pub fn title(mut self, v: impl Into<String>) -> Self {
68        self.title = Some(v.into());
69        self
70    }
71    /// Make the tab a link.
72    pub fn href(mut self, v: impl Into<String>) -> Self {
73        self.href = Some(v.into());
74        self
75    }
76    pub fn selected(mut self, v: bool) -> Self {
77        self.selected = v;
78        self
79    }
80    pub fn disabled(mut self, v: bool) -> Self {
81        self.disabled = v;
82        self
83    }
84    /// An icon before the title.
85    pub fn start_content(mut self, v: impl View) -> Self {
86        self.start_content = Some(v.into_node());
87        self
88    }
89    /// A chip or count after the title.
90    pub fn end_content(mut self, v: impl View) -> Self {
91        self.end_content = Some(v.into_node());
92        self
93    }
94}
95
96/// Values accepted inside `Tabs![..]`.
97pub trait TabsPart {
98    fn add_to(self, tabs: &mut Tabs);
99}
100impl TabsPart for Tab {
101    fn add_to(self, tabs: &mut Tabs) {
102        tabs.tabs.push(self);
103    }
104}
105impl TabsPart for Vec<Tab> {
106    fn add_to(self, tabs: &mut Tabs) {
107        tabs.tabs.extend(self);
108    }
109}
110impl TabsPart for Attr {
111    fn add_to(self, tabs: &mut Tabs) {
112        tabs.extra.push(self);
113    }
114}
115
116/// A strip of tabs: links between pages, or panels switched in place.
117///
118/// ```
119/// # use next_rust_ui::*; use next_rust_view::*;
120/// let t = Tabs![selected = "b", Tab![key = "a", title = "A", p!["one"]], Tab![key = "b", title = "B", p!["two"]]];
121/// let html = render_static(t);
122/// assert!(html.contains(r#"role="tablist""#) && html.contains(r#"aria-selected="true""#));
123/// assert!(html.contains(r#"tabindex="0" hidden>"#), "the unselected panel is hidden");
124/// ```
125#[derive(Default)]
126pub struct Tabs {
127    tabs: Vec<Tab>,
128    variant: TabsVariant,
129    color: Color,
130    size: Size,
131    radius: Option<Radius>,
132    selected: Option<String>,
133    full_width: bool,
134    label: Option<String>,
135    unstyled: bool,
136    extra: Extra,
137}
138
139impl Tabs {
140    pub fn new() -> Self {
141        Self { color: Color::Default, ..Self::default() }
142    }
143    common_methods!();
144
145    /// Add tabs or attributes for the strip.
146    pub fn with(mut self, part: impl TabsPart) -> Self {
147        part.add_to(&mut self);
148        self
149    }
150    pub fn tabs(mut self, v: impl IntoIterator<Item = Tab>) -> Self {
151        self.tabs.extend(v);
152        self
153    }
154    pub fn variant(mut self, v: TabsVariant) -> Self {
155        self.variant = v;
156        self
157    }
158    pub fn color(mut self, v: Color) -> Self {
159        self.color = v;
160        self
161    }
162    pub fn size(mut self, v: Size) -> Self {
163        self.size = v;
164        self
165    }
166    pub fn radius(mut self, v: Radius) -> Self {
167        self.radius = Some(v);
168        self
169    }
170    /// The key of the selected tab (otherwise the first, or the one marked).
171    pub fn selected(mut self, v: impl Into<String>) -> Self {
172        self.selected = Some(v.into());
173        self
174    }
175    /// Tabs share the width of the strip.
176    pub fn full_width(mut self, v: bool) -> Self {
177        self.full_width = v;
178        self
179    }
180    /// Accessible name of the strip.
181    pub fn label(mut self, v: impl Into<String>) -> Self {
182        self.label = Some(v.into());
183        self
184    }
185}
186
187impl View for Tabs {
188    fn into_node(self) -> Node {
189        let (class, attrs, children) = self.extra.split();
190        let id = next_id("nr-tabs");
191        let keys: Vec<String> =
192            self.tabs.iter().enumerate().map(|(i, t)| t.key.clone().unwrap_or_else(|| i.to_string())).collect();
193        let selected = self
194            .selected
195            .as_ref()
196            .and_then(|s| keys.iter().position(|k| k == s))
197            .or_else(|| self.tabs.iter().position(|t| t.selected))
198            .or_else(|| self.tabs.iter().position(|t| !t.disabled));
199        let has_panels = self.tabs.iter().any(|t| t.extra.has_children());
200
201        let size = format!("nr-tabs-{}", self.size.suffix());
202        let classes = [
203            "nr-tabs",
204            self.variant.class(),
205            self.color.class(),
206            &size,
207            self.radius.map(Radius::class).unwrap_or(""),
208            if self.full_width { "nr-tabs-full" } else { "" },
209        ];
210        let mut el = root("div", &classes, self.unstyled, class);
211        if has_panels {
212            el.set_attr(attr("data-nr-ui", "tabs"));
213        }
214        apply(&mut el, attrs);
215
216        let mut list = Element::new("div").with(attr("class", "nr-tabs-list")).with(attr("role", "tablist"));
217        if let Some(label) = self.label {
218            list.set_attr(attr("aria-label", label));
219        }
220        let mut panels = Vec::new();
221        for (i, tab) in self.tabs.into_iter().enumerate() {
222            let key = &keys[i];
223            let is_selected = selected == Some(i);
224            let (tab_class, tab_attrs, content) = tab.extra.split();
225            let tag = if tab.href.is_some() { "a" } else { "button" };
226            let mut item = Element::new(tag);
227            item.set_attr(attr("class", if is_selected { "nr-tab nr-tab-selected" } else { "nr-tab" }));
228            if let Some(c) = tab_class {
229                item.set_attr(c);
230            }
231            item.set_attr(attr("data-key", key.clone()));
232            match &tab.href {
233                Some(href) => {
234                    item.set_attr(attr("href", href.clone()));
235                    item.set_attr(attr("data-nr-link", ""));
236                    if is_selected {
237                        item.set_attr(attr("aria-current", "page"));
238                    }
239                    if tab.disabled {
240                        item.set_attr(attr("aria-disabled", "true"));
241                        item.set_attr(attr("tabindex", "-1"));
242                    }
243                }
244                None => {
245                    item.set_attr(attr("type", "button"));
246                    item.set_attr(attr("role", "tab"));
247                    item.set_attr(attr("id", format!("{id}-tab-{key}")));
248                    item.set_attr(attr("aria-selected", if is_selected { "true" } else { "false" }));
249                    item.set_attr(attr("tabindex", if is_selected { "0" } else { "-1" }));
250                    item.set_attr(flag("disabled", tab.disabled));
251                    if has_panels {
252                        item.set_attr(attr("aria-controls", format!("{id}-panel-{key}")));
253                    }
254                }
255            }
256            apply(&mut item, tab_attrs);
257            item.children.extend(tab.start_content);
258            if let Some(title) = tab.title {
259                item.children.push(Element::new("span").with(attr("class", "nr-tab-title")).with(title).into_node());
260            }
261            item.children.extend(tab.end_content);
262            list.children.push(item.into_node());
263
264            if has_panels {
265                let mut panel = Element::new("div")
266                    .with(attr("class", "nr-tab-panel"))
267                    .with(attr("role", "tabpanel"))
268                    .with(attr("id", format!("{id}-panel-{key}")))
269                    .with(attr("aria-labelledby", format!("{id}-tab-{key}")))
270                    .with(attr("tabindex", "0"));
271                panel.set_attr(flag("hidden", !is_selected));
272                panel.children = content;
273                panels.push(panel.into_node());
274            }
275        }
276        el.children.push(list.into_node());
277        el.children.extend(panels);
278        el.children.extend(children);
279        styled(el)
280    }
281}
282
283// ---------------------------------------------------------------------------
284// Breadcrumbs
285// ---------------------------------------------------------------------------
286
287/// One crumb; the last one is the current page.
288///
289/// `BreadcrumbItem![href = "/courses", "Courses"]`
290#[derive(Default)]
291pub struct BreadcrumbItem {
292    href: Option<String>,
293    start_content: Option<Node>,
294    disabled: bool,
295    extra: Extra,
296}
297
298impl BreadcrumbItem {
299    pub fn new() -> Self {
300        Self::default()
301    }
302    with_parts!();
303
304    pub fn href(mut self, v: impl Into<String>) -> Self {
305        self.href = Some(v.into());
306        self
307    }
308    pub fn start_content(mut self, v: impl View) -> Self {
309        self.start_content = Some(v.into_node());
310        self
311    }
312    pub fn disabled(mut self, v: bool) -> Self {
313        self.disabled = v;
314        self
315    }
316}
317
318/// Values accepted inside `Breadcrumbs![..]`.
319pub trait BreadcrumbsPart {
320    fn add_to(self, crumbs: &mut Breadcrumbs);
321}
322impl BreadcrumbsPart for BreadcrumbItem {
323    fn add_to(self, crumbs: &mut Breadcrumbs) {
324        crumbs.items.push(self);
325    }
326}
327impl BreadcrumbsPart for Vec<BreadcrumbItem> {
328    fn add_to(self, crumbs: &mut Breadcrumbs) {
329        crumbs.items.extend(self);
330    }
331}
332impl BreadcrumbsPart for Attr {
333    fn add_to(self, crumbs: &mut Breadcrumbs) {
334        crumbs.extra.push(self);
335    }
336}
337
338/// The path to the current page.
339///
340/// ```
341/// # use next_rust_ui::*; use next_rust_view::*;
342/// let b = Breadcrumbs![BreadcrumbItem![href = "/", "Home"], BreadcrumbItem!["Settings"]];
343/// let html = render_static(b);
344/// assert!(html.contains(r#"<nav class="nr-breadcrumbs nr-breadcrumbs-md" aria-label="Breadcrumb">"#));
345/// assert!(html.contains(r#"aria-current="page""#));
346/// ```
347#[derive(Default)]
348pub struct Breadcrumbs {
349    items: Vec<BreadcrumbItem>,
350    size: Size,
351    separator: Option<String>,
352    unstyled: bool,
353    extra: Extra,
354}
355
356impl Breadcrumbs {
357    pub fn new() -> Self {
358        Self::default()
359    }
360    common_methods!();
361
362    /// Add crumbs or attributes for the `<nav>`.
363    pub fn with(mut self, part: impl BreadcrumbsPart) -> Self {
364        part.add_to(&mut self);
365        self
366    }
367    pub fn items(mut self, v: impl IntoIterator<Item = BreadcrumbItem>) -> Self {
368        self.items.extend(v);
369        self
370    }
371    pub fn size(mut self, v: Size) -> Self {
372        self.size = v;
373        self
374    }
375    /// Text between crumbs instead of the chevron ("/").
376    pub fn separator(mut self, v: impl Into<String>) -> Self {
377        self.separator = Some(v.into());
378        self
379    }
380}
381
382impl View for Breadcrumbs {
383    fn into_node(self) -> Node {
384        let (class, attrs, children) = self.extra.split();
385        let size = format!("nr-breadcrumbs-{}", self.size.suffix());
386        let mut el = root("nav", &["nr-breadcrumbs", &size], self.unstyled, class);
387        el.set_attr(attr("aria-label", "Breadcrumb"));
388        apply(&mut el, attrs);
389        let mut list = Element::new("ol").with(attr("class", "nr-breadcrumbs-list"));
390        let last = self.items.len().saturating_sub(1);
391        for (i, item) in self.items.into_iter().enumerate() {
392            let (item_class, item_attrs, content) = item.extra.split();
393            let current = i == last;
394            let mut li = Element::new("li").with(attr("class", "nr-breadcrumb"));
395            if i > 0 {
396                let sep =
397                    Element::new("span").with(attr("class", "nr-breadcrumb-sep")).with(attr("aria-hidden", "true"));
398                li.children.push(
399                    match &self.separator {
400                        Some(text) => sep.with(text.clone()),
401                        None => sep.with(icon(icons::chevron_right())),
402                    }
403                    .into_node(),
404                );
405            }
406            let mut link = match (&item.href, current || item.disabled) {
407                (Some(href), false) => {
408                    Element::new("a").with(attr("href", href.clone())).with(attr("data-nr-link", ""))
409                }
410                _ => Element::new("span"),
411            };
412            link.set_attr(attr(
413                "class",
414                if current { "nr-breadcrumb-link nr-breadcrumb-current" } else { "nr-breadcrumb-link" },
415            ));
416            if let Some(c) = item_class {
417                link.set_attr(c);
418            }
419            if current {
420                link.set_attr(attr("aria-current", "page"));
421            }
422            if item.disabled {
423                link.set_attr(attr("aria-disabled", "true"));
424            }
425            apply(&mut link, item_attrs);
426            link.children.extend(item.start_content);
427            link.children.extend(content);
428            li.children.push(link.into_node());
429            list.children.push(li.into_node());
430        }
431        el.children.push(list.into_node());
432        el.children.extend(children);
433        styled(el)
434    }
435}
436
437// ---------------------------------------------------------------------------
438// Steps
439// ---------------------------------------------------------------------------
440
441/// State of a [`Step`].
442#[derive(Debug, Clone, Copy, PartialEq, Eq)]
443pub enum StepStatus {
444    Done,
445    Current,
446    Upcoming,
447    /// Something went wrong at this step.
448    Error,
449}
450
451/// One step of a process. Children are extra content under the description.
452///
453/// `Step![title = "Payment", description = "Card or bank transfer"]`
454#[derive(Default)]
455pub struct Step {
456    title: Option<String>,
457    description: Option<String>,
458    href: Option<String>,
459    status: Option<StepStatus>,
460    icon: Option<Node>,
461    extra: Extra,
462}
463
464impl Step {
465    pub fn new() -> Self {
466        Self::default()
467    }
468    with_parts!();
469
470    pub fn title(mut self, v: impl Into<String>) -> Self {
471        self.title = Some(v.into());
472        self
473    }
474    pub fn description(mut self, v: impl Into<String>) -> Self {
475        self.description = Some(v.into());
476        self
477    }
478    /// Make the step a link (done steps usually are).
479    pub fn href(mut self, v: impl Into<String>) -> Self {
480        self.href = Some(v.into());
481        self
482    }
483    /// Override the status `Steps![current = ..]` implies.
484    pub fn status(mut self, v: StepStatus) -> Self {
485        self.status = Some(v);
486        self
487    }
488    /// Your own marker instead of the number or check mark.
489    pub fn icon(mut self, v: impl View) -> Self {
490        self.icon = Some(v.into_node());
491        self
492    }
493}
494
495/// Values accepted inside `Steps![..]`.
496pub trait StepsPart {
497    fn add_to(self, steps: &mut Steps);
498}
499impl StepsPart for Step {
500    fn add_to(self, steps: &mut Steps) {
501        steps.steps.push(self);
502    }
503}
504impl StepsPart for Vec<Step> {
505    fn add_to(self, steps: &mut Steps) {
506        steps.steps.extend(self);
507    }
508}
509impl StepsPart for Attr {
510    fn add_to(self, steps: &mut Steps) {
511        steps.extra.push(self);
512    }
513}
514
515/// Where someone is in a process.
516///
517/// ```
518/// # use next_rust_ui::*; use next_rust_view::*;
519/// let s = Steps![current = 1, Step![title = "Account"], Step![title = "Plan"], Step![title = "Done"]];
520/// let html = render_static(s);
521/// assert!(html.contains("nr-step-done") && html.contains(r#"aria-current="step""#) && html.contains("nr-step-upcoming"));
522/// ```
523#[derive(Default)]
524pub struct Steps {
525    steps: Vec<Step>,
526    current: usize,
527    color: Color,
528    size: Size,
529    vertical: bool,
530    unstyled: bool,
531    extra: Extra,
532}
533
534impl Steps {
535    pub fn new() -> Self {
536        Self::default()
537    }
538    common_methods!();
539
540    /// Add steps or attributes for the list.
541    pub fn with(mut self, part: impl StepsPart) -> Self {
542        part.add_to(&mut self);
543        self
544    }
545    pub fn steps(mut self, v: impl IntoIterator<Item = Step>) -> Self {
546        self.steps.extend(v);
547        self
548    }
549    /// Index of the current step (0-based); earlier ones are done.
550    pub fn current(mut self, v: usize) -> Self {
551        self.current = v;
552        self
553    }
554    pub fn color(mut self, v: Color) -> Self {
555        self.color = v;
556        self
557    }
558    pub fn size(mut self, v: Size) -> Self {
559        self.size = v;
560        self
561    }
562    pub fn vertical(mut self, v: bool) -> Self {
563        self.vertical = v;
564        self
565    }
566}
567
568impl View for Steps {
569    fn into_node(self) -> Node {
570        let (class, attrs, children) = self.extra.split();
571        let size = format!("nr-steps-{}", self.size.suffix());
572        let classes = ["nr-steps", &size, self.color.class(), if self.vertical { "nr-steps-vertical" } else { "" }];
573        let mut el = root("ol", &classes, self.unstyled, class);
574        apply(&mut el, attrs);
575        for (i, step) in self.steps.into_iter().enumerate() {
576            let status = step.status.unwrap_or(if i < self.current {
577                StepStatus::Done
578            } else if i == self.current {
579                StepStatus::Current
580            } else {
581                StepStatus::Upcoming
582            });
583            let status_class = match status {
584                StepStatus::Done => "nr-step-done",
585                StepStatus::Current => "nr-step-current",
586                StepStatus::Upcoming => "nr-step-upcoming",
587                StepStatus::Error => "nr-step-error",
588            };
589            let (step_class, step_attrs, content) = step.extra.split();
590            let mut li = Element::new("li").with(attr("class", format!("nr-step {status_class}")));
591            if let Some(c) = step_class {
592                li.set_attr(c);
593            }
594            if status == StepStatus::Current {
595                li.set_attr(attr("aria-current", "step"));
596            }
597            apply(&mut li, step_attrs);
598            let marker = Element::new("span").with(attr("class", "nr-step-marker")).with(match (step.icon, status) {
599                (Some(glyph), _) => glyph,
600                (None, StepStatus::Done) => icon(icons::check()),
601                (None, StepStatus::Error) => icon(icons::close()),
602                (None, _) => Node::Text((i + 1).to_string().into()),
603            });
604            let mut text = Element::new("span").with(attr("class", "nr-step-text"));
605            if let Some(title) = step.title {
606                text.children.push(Element::new("span").with(attr("class", "nr-step-title")).with(title).into_node());
607            }
608            if let Some(description) = step.description {
609                text.children.push(
610                    Element::new("span").with(attr("class", "nr-step-description")).with(description).into_node(),
611                );
612            }
613            text.children.extend(content);
614            let mut inner = match &step.href {
615                Some(href) => Element::new("a")
616                    .with(attr("class", "nr-step-inner"))
617                    .with(attr("href", href.clone()))
618                    .with(attr("data-nr-link", "")),
619                None => Element::new("span").with(attr("class", "nr-step-inner")),
620            };
621            inner.children.push(marker.into_node());
622            inner.children.push(text.into_node());
623            li.children.push(inner.into_node());
624            el.children.push(li.into_node());
625        }
626        el.children.extend(children);
627        styled(el)
628    }
629}
630
631// ---------------------------------------------------------------------------
632// Accordion
633// ---------------------------------------------------------------------------
634
635/// Look of an [`Accordion`].
636#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
637pub enum AccordionVariant {
638    /// Items separated by lines.
639    #[default]
640    Light,
641    /// One outlined box.
642    Bordered,
643    /// Each item its own card.
644    Splitted,
645    /// One raised box.
646    Shadow,
647}
648
649impl AccordionVariant {
650    fn class(self) -> &'static str {
651        match self {
652            AccordionVariant::Light => "nr-accordion-light",
653            AccordionVariant::Bordered => "nr-accordion-bordered",
654            AccordionVariant::Splitted => "nr-accordion-splitted",
655            AccordionVariant::Shadow => "nr-accordion-shadow",
656        }
657    }
658}
659
660/// One section of an [`Accordion`]: a `<details>` element, so it opens
661/// and closes without any script.
662///
663/// `AccordionItem![title = "Shipping", subtitle = "2–4 days", p!["…"]]`
664#[derive(Default)]
665pub struct AccordionItem {
666    title: Option<String>,
667    subtitle: Option<String>,
668    start_content: Option<Node>,
669    expanded: bool,
670    disabled: bool,
671    extra: Extra,
672}
673
674impl AccordionItem {
675    pub fn new() -> Self {
676        Self::default()
677    }
678    with_parts!();
679
680    pub fn title(mut self, v: impl Into<String>) -> Self {
681        self.title = Some(v.into());
682        self
683    }
684    pub fn subtitle(mut self, v: impl Into<String>) -> Self {
685        self.subtitle = Some(v.into());
686        self
687    }
688    /// An icon before the title.
689    pub fn start_content(mut self, v: impl View) -> Self {
690        self.start_content = Some(v.into_node());
691        self
692    }
693    /// Open at first.
694    pub fn expanded(mut self, v: bool) -> Self {
695        self.expanded = v;
696        self
697    }
698    pub fn disabled(mut self, v: bool) -> Self {
699        self.disabled = v;
700        self
701    }
702}
703
704/// Values accepted inside `Accordion![..]`.
705pub trait AccordionPart {
706    fn add_to(self, accordion: &mut Accordion);
707}
708impl AccordionPart for AccordionItem {
709    fn add_to(self, accordion: &mut Accordion) {
710        accordion.items.push(self);
711    }
712}
713impl AccordionPart for Vec<AccordionItem> {
714    fn add_to(self, accordion: &mut Accordion) {
715        accordion.items.extend(self);
716    }
717}
718impl AccordionPart for Attr {
719    fn add_to(self, accordion: &mut Accordion) {
720        accordion.extra.push(self);
721    }
722}
723
724/// Sections that expand one at a time (or several, with `multiple`).
725///
726/// ```
727/// # use next_rust_ui::*; use next_rust_view::*;
728/// let a = Accordion![AccordionItem![title = "One", expanded = true, p!["1"]], AccordionItem![title = "Two", p!["2"]]];
729/// let html = render_static(a);
730/// assert!(html.contains(r#"<details class="nr-accordion-item" name="nr-accordion-"#) && html.contains(" open>"));
731/// ```
732#[derive(Default)]
733pub struct Accordion {
734    items: Vec<AccordionItem>,
735    variant: AccordionVariant,
736    multiple: bool,
737    compact: bool,
738    hide_indicator: bool,
739    unstyled: bool,
740    extra: Extra,
741}
742
743impl Accordion {
744    pub fn new() -> Self {
745        Self::default()
746    }
747    common_methods!();
748
749    /// Add items or attributes for the outer element.
750    pub fn with(mut self, part: impl AccordionPart) -> Self {
751        part.add_to(&mut self);
752        self
753    }
754    pub fn items(mut self, v: impl IntoIterator<Item = AccordionItem>) -> Self {
755        self.items.extend(v);
756        self
757    }
758    pub fn variant(mut self, v: AccordionVariant) -> Self {
759        self.variant = v;
760        self
761    }
762    /// Let several items be open at once.
763    pub fn multiple(mut self, v: bool) -> Self {
764        self.multiple = v;
765        self
766    }
767    pub fn compact(mut self, v: bool) -> Self {
768        self.compact = v;
769        self
770    }
771    /// No chevron.
772    pub fn hide_indicator(mut self, v: bool) -> Self {
773        self.hide_indicator = v;
774        self
775    }
776}
777
778impl View for Accordion {
779    fn into_node(self) -> Node {
780        let (class, attrs, children) = self.extra.split();
781        let classes = ["nr-accordion", self.variant.class(), if self.compact { "nr-accordion-compact" } else { "" }];
782        let mut el = root("div", &classes, self.unstyled, class);
783        apply(&mut el, attrs);
784        let group = next_id("nr-accordion");
785        for item in self.items {
786            let (item_class, item_attrs, content) = item.extra.split();
787            let mut details = Element::new("details").with(attr("class", "nr-accordion-item"));
788            if let Some(c) = item_class {
789                details.set_attr(c);
790            }
791            if !self.multiple {
792                details.set_attr(attr("name", group.clone()));
793            }
794            details.set_attr(flag("open", item.expanded));
795            details.set_attr(flag("data-disabled", item.disabled));
796            apply(&mut details, item_attrs);
797            let mut summary = Element::new("summary").with(attr("class", "nr-accordion-trigger"));
798            if item.disabled {
799                summary.set_attr(attr("tabindex", "-1"));
800                summary.set_attr(attr("aria-disabled", "true"));
801            }
802            summary.children.extend(item.start_content);
803            let mut heading = Element::new("span").with(attr("class", "nr-accordion-heading"));
804            if let Some(title) = item.title {
805                heading
806                    .children
807                    .push(Element::new("span").with(attr("class", "nr-accordion-title")).with(title).into_node());
808            }
809            if let Some(subtitle) = item.subtitle {
810                heading
811                    .children
812                    .push(Element::new("span").with(attr("class", "nr-accordion-subtitle")).with(subtitle).into_node());
813            }
814            summary.children.push(heading.into_node());
815            if !self.hide_indicator {
816                summary.children.push(
817                    Element::new("span")
818                        .with(attr("class", "nr-accordion-indicator"))
819                        .with(icon(icons::chevron_down()))
820                        .into_node(),
821                );
822            }
823            details.children.push(summary.into_node());
824            details
825                .children
826                .push(Element::new("div").with(attr("class", "nr-accordion-content")).with(content).into_node());
827            el.children.push(details.into_node());
828        }
829        el.children.extend(children);
830        styled(el)
831    }
832}