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    AttrValue, Callback, Children, Html, Properties, function_component, html, use_effect_with,
17    use_node_ref, use_state_eq,
18};
19
20use crate::components::editable_header::{EditableHeader, EditableHeaderProps};
21
22#[derive(PartialEq, Clone, Properties)]
23pub struct SidebarProps {
24    /// The component's children.
25    pub children: Children,
26
27    /// When this callback is called, the sidebar will close
28    pub on_close: Callback<()>,
29    pub id_prefix: String,
30    pub width_override: Option<i32>,
31    pub selected_tab: Option<usize>,
32    pub header_props: EditableHeaderProps,
33}
34
35/// Sidebars are designed to live in a
36/// [`super::split_panel::SplitPanel`]
37#[function_component]
38pub fn Sidebar(p: &SidebarProps) -> Html {
39    let id = &p.id_prefix;
40    let noderef = use_node_ref();
41    let auto_width = use_state_eq(|| 0f64);
42
43    // this gets the last calculated width and ensures that
44    // the auto-width element is at least that big.
45    // this ensures the panel grows but does not shrink.
46    use_effect_with(p.clone(), {
47        clone!(noderef, auto_width);
48        move |p| {
49            if p.width_override.is_none() {
50                let updated_width = noderef
51                    .cast::<Element>()
52                    .map(|el| el.get_bounding_client_rect().width())
53                    .unwrap_or_default();
54                let new_auto_width = (*auto_width).max(updated_width);
55                auto_width.set(new_auto_width);
56            } else {
57                auto_width.set(0f64);
58            }
59        }
60    });
61
62    let width_style = format!("min-width: 200px; width: {}px", *auto_width);
63    html! {
64        <div class="sidebar_column" id={format!("{id}_sidebar")} ref={noderef}>
65            <SidebarCloseButton id={format!("{id}_close_button")} on_close_sidebar={&p.on_close} />
66            <div class="sidebar_header"><EditableHeader ..p.header_props.clone() /></div>
67            <div class="sidebar_border" id={format!("{id}_border")} />
68            { p.children.iter().collect::<Html>() }
69            <div class="sidebar-auto-width" style={width_style} />
70        </div>
71    }
72}
73
74#[derive(PartialEq, Clone, Properties)]
75pub struct SidebarCloseButtonProps {
76    pub on_close_sidebar: Callback<()>,
77    pub id: AttrValue,
78}
79
80#[function_component]
81pub fn SidebarCloseButton(p: &SidebarCloseButtonProps) -> Html {
82    let onclick = yew::use_callback(p.on_close_sidebar.clone(), |_, cb| cb.emit(()));
83    let id = &p.id;
84    html! {
85        <div {onclick} {id} class="sidebar_close_button">
86            <div class="sidebar_close_button_inner" />
87        </div>
88    }
89}