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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::core::{AsClasses, ExtendClasses, WithBreakpoints};
use yew::html::IntoPropValue;
use yew::prelude::*;

/// Properties for [`PageSection`]
#[derive(Copy, Clone, Default, Eq, PartialEq)]
pub enum PageSectionVariant {
    #[default]
    Default,
    Darker,
    Dark,
    Light,
}

impl AsClasses for PageSectionVariant {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            Self::Default => {}
            Self::Darker => classes.push("pf-m-dark-100"),
            Self::Dark => classes.push("pf-m-dark-200"),
            Self::Light => classes.push("pf-m-light"),
        }
    }
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub enum PageSectionType {
    #[default]
    Default,
    Breadcrumbs,
    Navigation,
    SubNavigation,
    Tabs,
    Wizard,
}

impl AsClasses for PageSectionType {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            Self::Default => classes.push("pf-v5-c-page__main-section"),
            Self::Breadcrumbs => classes.push("pf-v5-c-page__main-breadcrumb"),
            Self::Navigation => classes.push("pf-v5-c-page__main-nav"),
            Self::SubNavigation => classes.push("pf-v5-c-page__main-subnav"),
            Self::Tabs => classes.push("pf-v5-c-page__main-tabs"),
            Self::Wizard => classes.push("pf-v5-c-page__main-wizard"),
        }
    }
}

#[derive(Clone, PartialEq, Properties)]
pub struct PageSectionProperties {
    #[prop_or_default]
    pub children: Html,
    #[prop_or_default]
    pub r#type: PageSectionType,
    #[prop_or_default]
    pub variant: PageSectionVariant, // Should only be used on PageSectionType::Main
    #[prop_or_default]
    pub fill: PageSectionFill,
    #[prop_or_default]
    pub limit_width: bool,
    #[prop_or_default]
    pub shadow: PageSectionShadow,
    #[prop_or_default]
    pub align_center: bool,
    #[prop_or_default]
    pub overflow_scroll: bool,
    #[prop_or_default]
    pub sticky: WithBreakpoints<PageSectionSticky>,

    #[prop_or_default]
    pub id: Option<AttrValue>,
    #[prop_or_default]
    pub class: Classes,
    #[prop_or_default]
    pub style: Option<AttrValue>,

    #[prop_or_default]
    pub hidden: bool,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum PageSectionShadow {
    #[default]
    None,
    Top,
    Bottom,
    Both,
}

impl AsClasses for PageSectionShadow {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            Self::None => {}
            Self::Top => classes.push("pf-m-shadow-top"),
            Self::Bottom => classes.push("pf-m-shadow-bottom"),
            Self::Both => classes.push("pf-m-shadow-top pf-m-shadow-bottom"),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum PageSectionSticky {
    #[default]
    None,
    Top,
    Bottom,
    Both,
}

impl AsClasses for PageSectionSticky {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            Self::None => {}
            Self::Top => classes.push("pf-m-sticky-top"),
            Self::Bottom => classes.push("pf-m-sticky-bottom"),
            Self::Both => classes.push("pf-m-sticky-top pf-m-sticky-bottom"),
        }
    }
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum PageSectionFill {
    #[default]
    Default,
    Fill,
    NoFill,
}

impl AsClasses for PageSectionFill {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            Self::Default => {}
            Self::Fill => classes.push("pf-m-fill"),
            Self::NoFill => classes.push("pf-m-no-fill"),
        }
    }
}

impl IntoPropValue<PageSectionFill> for bool {
    fn into_prop_value(self) -> PageSectionFill {
        match self {
            true => PageSectionFill::Fill,
            false => PageSectionFill::NoFill,
        }
    }
}

/// A page section, a child of a [`Page`](crate::prelude::Page) component.
///
/// ## Properties
///
/// Defined by [`PageSectionProperties`].
///
/// ## Example
///
/// ```rust
/// use yew::prelude::*;
/// use patternfly_yew::prelude::*;
///
/// #[function_component(MyPage)]
/// fn my_page() -> Html {
///   html!(
///     <Page>
///       <PageSection>{"my content"}</PageSection>
///     </Page>
///   )
/// }
/// ```
#[function_component(PageSection)]
pub fn page_section(props: &PageSectionProperties) -> Html {
    // start with the main type

    let mut class = props.r#type.as_classes();

    // extend with options

    class.extend_from(&props.variant);
    class.extend_from(&props.fill);
    class.extend_from(&props.sticky);
    class.extend_from(&props.shadow);

    if props.limit_width {
        class.push("pf-m-limit-width");
    }

    if props.align_center {
        class.push("pf-m-align-center"); // Can only be used with limit_width
    }

    if props.overflow_scroll {
        class.push("pf-m-overflow-scroll"); // Can only be used with PageSectionType::Default
    }

    class.extend(&props.class);

    // render

    html! (
        <section {class} id={&props.id} hidden={props.hidden} style={props.style.clone()}>
            if props.limit_width {
                <div class="pf-v5-c-page__main-body">
                    { props.children.clone() }
                </div>
            } else {
                { props.children.clone() }
            }
         </section>
    )
}

/// Properties for [`PageSectionGroup`]
#[derive(PartialEq, Properties)]
pub struct PageSectionGroupProperties {
    pub children: Html,
    #[prop_or_default]
    pub shadow: PageSectionShadow,
    #[prop_or_default]
    pub sticky: WithBreakpoints<PageSectionSticky>,
    #[prop_or_default]
    pub overflow_scroll: bool,
}

#[function_component(PageSectionGroup)]
pub fn page_section_group(props: &PageSectionGroupProperties) -> Html {
    let mut class = Classes::from("pf-v5-c-page__main-group");

    class.extend_from(&props.shadow);
    class.extend_from(&props.sticky);

    if props.overflow_scroll {
        class.push("pf-m-overflow-scroll");
    }

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