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
//! Full Page components
use crate::prelude::{Button, ButtonType, ButtonVariant};
use std::rc::Rc;
use yew::prelude::*;

mod section;
mod sidebar;

pub use section::*;
pub use sidebar::*;

/// Properties for [`Page`]
#[derive(Clone, PartialEq, Properties)]
pub struct PageProperties {
    #[prop_or_default]
    pub children: Html,
    #[prop_or_default]
    pub sidebar: ChildrenWithProps<PageSidebar>,
    #[prop_or_default]
    pub tools: Html,

    /// The brand section.
    ///
    /// Expected to be a single [`MastheadBrand`] component.
    ///
    /// NOTE: Future versions might enforce the child requirement without prior deprecation.
    #[prop_or_default]
    pub brand: Html,

    #[prop_or_default]
    pub nav: Html,
    #[prop_or(true)]
    pub open: bool,
    #[prop_or_default]
    pub full_height: bool,

    #[prop_or_default]
    pub id: Option<AttrValue>,
}

/// A full page
///
/// > The page component is used to define the basic layout of a page with either vertical or horizontal navigation.
///
/// See: <https://www.patternfly.org/components/page>
///
/// ## Properties
///
/// Defined by [`PageProperties`].
///
/// ## Elements
///
/// * **Sidebar**: Contains a single [`PageSidebar`], hosting the main navigation.
/// * **Navigation**: The top header navigation section.
/// * **Tools**: Tools, shown in the header section of the page.
/// * **Brand**: A brand logo, shown in the navigation header section.
/// * **Children**: The actual page content, probably wrapped into [`PageSection`] components.
///
#[function_component(Page)]
pub fn page(props: &PageProperties) -> Html {
    let open = use_state_eq(|| true);

    let onclick = {
        let open = open.clone();
        Callback::from(move |_| {
            open.set(!(*open));
        })
    };

    html! (
        <div class="pf-v5-c-page" id={&props.id} role="main" tabindex="-1">
            <header class="pf-v5-c-masthead">
                <span class="pf-v5-c-masthead__toggle">
                    <Button
                        r#type={ButtonType::Button}
                        variant={ButtonVariant::Plain}
                        {onclick}
                    >
                        <i class="fas fa-bars" aria-hidden="true" />
                    </Button>
                </span>

                <div class="pf-v5-c-masthead__main">
                    { props.brand.clone() }
                </div>

                <div class="pf-v5-c-masthead__content"> // TODO: Should migrate props
                    { props.nav.clone() }
                    { props.tools.clone() }
                </div>

            </header>

            { for props.sidebar.iter().map(|mut s|{
                let props = Rc::make_mut(&mut s.props);
                props.open = *open;
                s
            }) }

            <main class="pf-v5-c-page__main" tabindex="-1">
                { props.children.clone() }
            </main>
        </div>
    )
}

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct MastheadBrandProperties {
    /// Expected to be a single [`crate::prelude::Brand`] component.
    ///
    /// NOTE: Future versions might enforce the child requirement without prior deprecation.
    #[prop_or_default]
    pub children: Html,

    /// Called when the user clicks on the brand logo.
    #[prop_or_default]
    pub onclick: Option<Callback<()>>,
}

/// Masthead brand component.
///
/// ## Properties
///
/// Defined by [`MastheadBrandProperties`].
///
/// ## Children
///
/// A single [`crate::prelude::Brand`] component. The children may be wrapped in an `a` element when the `onclick`
/// callback is set.
#[function_component(MastheadBrand)]
pub fn masthead_brand(props: &MastheadBrandProperties) -> Html {
    match &props.onclick {
        Some(onclick) => {
            let onclick = onclick.reform(|_| ());
            html!(
                <a class="pf-v5-c-masthead__brand" href="#" {onclick}>
                    { props.children.clone() }
                </a>
            )
        }
        None => {
            html!(
                <div class="pf-v5-c-masthead__brand">
                    { props.children.clone() }
                </div>
            )
        }
    }
}