yew_layout/
spacer.rs

1use std::any::TypeId;
2
3use crate::*;
4use css_style::{prelude::*, AlignSelf};
5use yew::prelude::*;
6
7// ======================== Column Component ========================
8
9/// Spacer properties.
10///
11/// Here you can find all properties that can be used with
12/// [Spacer](crate::spacer::Spacer) component.
13#[derive(Properties, Clone, PartialEq)]
14pub struct Props {
15    /// Minimum length for this spacer.
16    ///
17    /// By default the spacer will take the available space, this property can
18    /// only control minimum length for the spacer.
19    pub min: Option<Length>,
20}
21
22pub struct Spacer {
23    parent_ty: ParentType,
24}
25
26impl Component for Spacer {
27    type Message = ();
28    type Properties = Props;
29
30    fn create(ctx: &Context<Self>) -> Self {
31        Spacer {
32            // NOTE: not sure if this should be in the view fn instead.
33            parent_ty: ParentType::from(ctx.link().get_parent().map(|p| p.get_type_id().clone())),
34        }
35    }
36
37    fn view(&self, ctx: &Context<Self>) -> Html {
38        let props = ctx.props();
39        let style = style()
40            .align_self(AlignSelf::Stretch)
41            .flex_grow(1.0)
42            .flex_shrink(1.0)
43            .and_size(|s| match self.parent_ty {
44                ParentType::Column => s.max_height(1.0).try_min_height(props.min.clone()),
45                ParentType::Row => s.max_width(1.0).try_min_width(props.min.clone()),
46                ParentType::Unknow | ParentType::Nothing => s
47                    .max_width(1.0)
48                    .max_height(1.0)
49                    .try_min_height(props.min.clone())
50                    .try_min_width(props.min.clone()),
51            })
52            .to_string();
53        html! {
54            <div style={style}></div>
55        }
56    }
57}
58
59// ======================
60
61#[derive(Debug)]
62enum ParentType {
63    Row,
64    Column,
65    Unknow,
66    Nothing,
67}
68
69impl From<Option<TypeId>> for ParentType {
70    fn from(source: Option<TypeId>) -> Self {
71        let row = TypeId::of::<Row>();
72        let column = TypeId::of::<Column>();
73
74        match source {
75            Some(val) if val == row => ParentType::Row,
76            Some(val) if val == column => ParentType::Column,
77            Some(_) => ParentType::Unknow,
78            None => ParentType::Nothing,
79        }
80    }
81}