use std::rc::Rc; use wasm_bindgen::JsCast; use web_sys::{Element, PointerEvent}; use yew::{function_component, html, use_state_eq, Callback, Html, Properties, UseStateHandle}; #[derive(Clone, Eq, PartialEq)] pub enum PanelDirection { Horizontal, Vertical, } #[derive(PartialEq, Properties)] pub struct SplitPanelProps { pub direction: PanelDirection, pub first: Html, pub second: Html, } #[function_component(SplitPanel)] pub fn split_panel(props: &SplitPanelProps) -> Html { let is_dragging: Rc>> = use_state_eq(|| None).into(); let dragged_space: Rc>> = use_state_eq(|| None).into(); let wrapper_class = match props.direction { PanelDirection::Horizontal => "hpanelwrapper", PanelDirection::Vertical => "vpanelwrapper", }; let wrapper_class = if (**is_dragging).is_some() { format!("{} dragging", wrapper_class) } else { wrapper_class.to_owned() }; let panel_class = match props.direction { PanelDirection::Horizontal => "hpanel", PanelDirection::Vertical => "vpanel", }; let divider_class = match props.direction { PanelDirection::Horizontal => "hdivider", PanelDirection::Vertical => "vdivider", }; let row_or_col = match props.direction { PanelDirection::Horizontal => "columns", PanelDirection::Vertical => "rows", }; let layout = match **dragged_space { None => format!("grid-template-{}: 1fr 4px 1fr", row_or_col), Some(custom) => format!("grid-template-{}: {}px 4px 1fr", row_or_col, custom), }; let is_dragging_forup = is_dragging.clone(); let dragged_space_formove = dragged_space.clone(); let dir = props.direction.clone(); match **is_dragging { None => { html! {
{props.first.clone()}
{}, Some(t) => { let el: Element = JsCast::unchecked_into::( JsCast::unchecked_into::(t) .parent_node().expect("Parent exists")); let top = match dir { PanelDirection::Horizontal => el.get_bounding_client_rect().x(), PanelDirection::Vertical => el.get_bounding_client_rect().y() }; is_dragging.set(Some(top)); } } })} />
{props.second.clone()}
} } Some(start) => { html! {
e.client_x() as f64, PanelDirection::Vertical => e.client_y() as f64 } - start - 2.0; (*dragged_space_formove).set(Some(distance)); })} >
{props.first.clone()}
{props.second.clone()}
} } } }