1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! Flex

use crate::prelude::{
    AsClasses, ChildrenProperties, ExtendClasses, Raw, SpaceItems, Spacer, WithBreakpoints,
};
use std::{fmt::Debug, rc::Rc};
use yew::{
    html::ChildrenRenderer,
    prelude::*,
    virtual_dom::{VChild, VComp},
};

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FlexModifier {
    /// Can grow beyond required size
    Grow,
    /// Try to minimize size
    Shrink,
    /// Weight of 1
    Flex1,
    /// Weight of 2
    Flex2,
    /// Weight of 3
    Flex3,
    /// Weight of 4
    Flex4,
    /// Full width item
    FullWidth,
    /// Column ordering
    Column,
    /// Row ordering
    ///
    /// This is the opposite of [`Column`] and the default. It can be used for response
    /// modifiers in combination with [`WithBreakpoints`] to bring back the default for some
    /// breakpoints.
    Row,
    Justify(Justify),
    Align(Alignment),
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Justify {
    Start,
    End,
    SpaceBetween,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Alignment {
    Right,
    Left,
    Start,
    Center,
    End,
    Baseline,
    Stretch,
}

impl AsClasses for FlexModifier {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            FlexModifier::Grow => classes.push("pf-m-grow"),
            FlexModifier::Shrink => classes.push("pf-m-shrink"),
            FlexModifier::Flex1 => classes.push("pf-m-flex-1"),
            FlexModifier::Flex2 => classes.push("pf-m-flex-2"),
            FlexModifier::Flex3 => classes.push("pf-m-flex-3"),
            FlexModifier::Flex4 => classes.push("pf-m-flex-4"),
            FlexModifier::FullWidth => classes.push("pf-m-full-width"),
            FlexModifier::Column => classes.push("pf-m-column"),
            FlexModifier::Row => classes.push("pf-m-row"),
            FlexModifier::Justify(layout) => match layout {
                Justify::Start => classes.push("pf-m-justify-content-flex-start"),
                Justify::End => classes.push("pf-m-justify-content-flex-end"),
                Justify::SpaceBetween => classes.push("pf-m-justify-content-space-between"),
            },
            FlexModifier::Align(alignment) => match alignment {
                Alignment::Right => classes.push("pf-m-align-right"),
                Alignment::Left => classes.push("pf-m-align-left"),
                Alignment::Start => classes.push("pf-m-align-self-flex-start"),
                Alignment::Center => classes.push("pf-m-align-self-center"),
                Alignment::End => classes.push("pf-m-align-self-flex-end"),
                Alignment::Baseline => classes.push("pf-m-align-self-baseline"),
                Alignment::Stretch => classes.push("pf-m-align-self-stretch"),
            },
        }
    }
}

#[derive(Clone, PartialEq)]
pub enum FlexChild {
    Flex(Rc<<Flex as BaseComponent>::Properties>),
    FlexItem(Rc<<FlexItem as BaseComponent>::Properties>),
    Raw(Rc<<Raw as BaseComponent>::Properties>),
}

impl From<FlexProperties> for FlexChild {
    fn from(props: FlexProperties) -> Self {
        FlexChild::Flex(Rc::new(props))
    }
}

impl From<FlexItemProperties> for FlexChild {
    fn from(props: FlexItemProperties) -> Self {
        FlexChild::FlexItem(Rc::new(props))
    }
}

impl From<ChildrenProperties> for FlexChild {
    fn from(props: ChildrenProperties) -> Self {
        FlexChild::Raw(Rc::new(props))
    }
}

#[derive(PartialEq, Clone)]
pub struct FlexChildVariant {
    props: FlexChild,
}

impl<CHILD> From<VChild<CHILD>> for FlexChildVariant
where
    CHILD: BaseComponent,
    CHILD::Properties: Into<FlexChild> + Clone,
{
    fn from(vchild: VChild<CHILD>) -> Self {
        Self {
            props: (*vchild.props).clone().into(),
        }
    }
}

impl From<FlexChildVariant> for Html {
    fn from(value: FlexChildVariant) -> Self {
        match value.props {
            FlexChild::Flex(props) => VComp::new::<Flex>(props, None).into(),
            FlexChild::FlexItem(props) => VComp::new::<FlexItem>(props, None).into(),
            FlexChild::Raw(props) => VComp::new::<Raw>(props, None).into(),
        }
    }
}

pub trait ToFlexItems {
    fn into_flex_items(self) -> Vec<VChild<FlexItem>>;
}

impl ToFlexItems for Vec<Html> {
    fn into_flex_items(self) -> Vec<VChild<FlexItem>> {
        self.into_iter()
            .map(|html| html_nested! {<FlexItem> { html }</FlexItem>})
            .collect()
    }
}

#[derive(Clone, PartialEq, Properties)]
pub struct FlexProperties {
    #[prop_or_default]
    pub children: ChildrenRenderer<FlexChildVariant>,
    #[prop_or_default]
    pub modifiers: WithBreakpoints<FlexModifier>,
    /// Individual spacing for this item, in context of its siblings.
    #[prop_or_default]
    pub spacer: WithBreakpoints<Spacer>,
    /// Spacing for all child items.
    #[prop_or_default]
    pub space_items: WithBreakpoints<SpaceItems>,
}

/// Flex layout
///
/// > The Flex layout is a tool to build your own custom layout that builds-in the PatternFly spacer and breakpoint systems.
///
/// See: <https://www.patternfly.org/layouts/flex>
///
/// ## Properties
///
/// Defined by [`FlexProperties`].
///
/// ## Children
///
/// The Flex layout contains either [`FlexItem`] children, or nested [`Flex`] layouts.
#[function_component(Flex)]
pub fn flex(props: &FlexProperties) -> Html {
    let mut classes = Classes::from("pf-v5-l-flex");

    classes.extend_from(&props.modifiers);
    classes.extend_from(&props.space_items);
    classes.extend_from(&props.spacer);

    html! (
        <div class={classes}>
            { for props.children.iter() }
        </div>
    )
}

// flex item

#[derive(Clone, PartialEq, Properties)]
pub struct FlexItemProperties {
    #[prop_or_default]
    pub children: Html,
    #[prop_or_default]
    pub modifiers: WithBreakpoints<FlexModifier>,
    #[prop_or_default]
    pub spacer: WithBreakpoints<Spacer>,
}

/// An item of the [`Flex`] layout.
///
/// ## Properties
///
/// Defined by [`FlexItemProperties`].
#[function_component(FlexItem)]
pub fn flex_item(props: &FlexItemProperties) -> Html {
    let mut classes = Classes::from("pf-v5-l-flex__item");

    classes.extend_from(&props.modifiers);
    classes.extend_from(&props.spacer);

    html! (
        <div class={classes}>
            { props.children.clone() }
        </div>
    )
}