Skip to main content

perspective_viewer/components/containers/
sidebar.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use perspective_client::clone;
14use web_sys::Element;
15use yew::{
16    Callback, Children, Html, Properties, function_component, html, use_effect_with, use_node_ref,
17    use_state_eq,
18};
19
20use crate::components::containers::sidebar_close_button::SidebarCloseButton;
21use crate::components::editable_header::{EditableHeader, EditableHeaderProps};
22
23#[derive(PartialEq, Clone, Properties)]
24pub struct SidebarProps {
25    /// The component's children.
26    pub children: Children,
27
28    /// When this callback is called, the sidebar will close
29    pub on_close: Callback<()>,
30    pub id_prefix: String,
31    pub width_override: Option<i32>,
32    pub selected_tab: Option<usize>,
33    pub header_props: EditableHeaderProps,
34}
35
36/// Sidebars are designed to live in a
37/// [`super::split_panel::SplitPanel`]
38#[function_component]
39pub fn Sidebar(p: &SidebarProps) -> Html {
40    let id = &p.id_prefix;
41    let noderef = use_node_ref();
42    let auto_width = use_state_eq(|| 0f64);
43
44    // this gets the last calculated width and ensures that
45    // the auto-width element is at least that big.
46    // this ensures the panel grows but does not shrink.
47    use_effect_with(p.clone(), {
48        clone!(noderef, auto_width);
49        move |p| {
50            if p.width_override.is_none() {
51                let updated_width = noderef
52                    .cast::<Element>()
53                    .map(|el| el.get_bounding_client_rect().width())
54                    .unwrap_or_default();
55                let new_auto_width = (*auto_width).max(updated_width);
56                auto_width.set(new_auto_width);
57            } else {
58                auto_width.set(0f64);
59            }
60        }
61    });
62
63    let width_style = format!("min-width: 200px; width: {}px", *auto_width);
64    html! {
65        <div class="sidebar_column" id={format!("{id}_sidebar")} ref={noderef}>
66            <SidebarCloseButton id={format!("{id}_close_button")} on_close_sidebar={&p.on_close} />
67            <div class="sidebar_header"><EditableHeader ..p.header_props.clone() /></div>
68            <div class="sidebar_border" id={format!("{id}_border")} />
69            { p.children.iter().collect::<Html>() }
70            <div class="sidebar-auto-width" style={width_style} />
71        </div>
72    }
73}