Skip to main content

vizia_core/views/
sidebar.rs

1use crate::prelude::*;
2
3/// A sidebar container with optional header, scrollable content area, and optional footer.
4///
5/// The sidebar is composed as:
6/// - `sidebar-header`
7/// - `sidebar-content`
8/// - `sidebar-footer`
9pub struct Sidebar {
10    _width: Signal<Units>,
11}
12
13impl Sidebar {
14    /// Creates a new [Sidebar] with custom header, content and footer builders.
15    pub fn new<H, C, F>(cx: &mut Context, header: H, content: C, footer: F) -> Handle<Self>
16    where
17        H: 'static + Fn(&mut Context),
18        C: 'static + Fn(&mut Context),
19        F: 'static + Fn(&mut Context),
20    {
21        let _width: Signal<Units> = Signal::new(Pixels(200.0));
22        Self { _width }.build(cx, move |cx| {
23            Resizable::new(
24                cx,
25                _width,
26                ResizeStackDirection::Right,
27                move |_cx, new_size| _width.set(Pixels(new_size)),
28                move |cx| {
29                    VStack::new(cx, |cx| {
30                        (header)(cx);
31                    })
32                    .class("sidebar-header")
33                    .height(Auto);
34
35                    Divider::new(cx).class("sidebar-divider");
36                    ScrollView::new(cx, move |cx| {
37                        (content)(cx);
38                    })
39                    .class("sidebar-content")
40                    .height(Stretch(1.0));
41
42                    Divider::new(cx).class("sidebar-divider");
43
44                    VStack::new(cx, |cx| {
45                        (footer)(cx);
46                    })
47                    .class("sidebar-footer")
48                    .height(Auto);
49                },
50            );
51        })
52    }
53}
54
55impl View for Sidebar {
56    fn element(&self) -> Option<&'static str> {
57        Some("sidebar")
58    }
59}