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
//! Modal
use crate::ouia;
use crate::prelude::use_backdrop;
use crate::utils::{Ouia, OuiaComponentType, OuiaSafe};
use yew::prelude::*;
use yew_hooks::{use_click_away, use_event_with_window};

const OUIA: Ouia = ouia!("ModalContent");

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum ModalVariant {
    #[default]
    None,
    Small,
    Medium,
    Large,
}

impl ModalVariant {
    pub fn as_classes(&self) -> Classes {
        match self {
            ModalVariant::None => classes!(),
            ModalVariant::Small => classes!("pf-m-sm"),
            ModalVariant::Medium => classes!("pf-m-md"),
            ModalVariant::Large => classes!("pf-m-lg"),
        }
    }
}

/// Properties for [`Modal`]
#[derive(Clone, PartialEq, Properties)]
pub struct ModalProperties {
    #[prop_or_default]
    pub title: String,
    #[prop_or_default]
    pub description: String,
    #[prop_or_default]
    pub variant: ModalVariant,
    #[prop_or_default]
    pub children: Children,
    #[prop_or_default]
    pub footer: Option<Html>,

    #[prop_or_default]
    pub onclose: Option<Callback<()>>,

    /// Disable close button
    #[prop_or(true)]
    pub show_close: bool,

    /// Disable closing the modal when the escape key is pressed
    #[prop_or_default]
    pub disable_close_escape: bool,
    /// Disable closing the modal when the user clicks outside the modal
    #[prop_or_default]
    pub disable_close_click_outside: bool,

    /// OUIA Component id
    #[prop_or_default]
    pub ouia_id: Option<String>,
    /// OUIA Component Type
    #[prop_or(OUIA.component_type())]
    pub ouia_type: OuiaComponentType,
    /// OUIA Component Safe
    #[prop_or(OuiaSafe::TRUE)]
    pub ouia_safe: OuiaSafe,
}

/// Modal component
///
/// > A **modal** displays important information to a user without requiring them to navigate to a new page.
///
/// See: <https://www.patternfly.org/components/modal>
///
/// ## Properties
///
/// Defined by [`ModalProperties`].
///
/// ## Contexts
///
/// If the modal dialog is wrapped by a [`crate::prelude::BackdropViewer`] component and no
/// `onclose` callback is set, then it will automatically close the backdrop when the modal dialog
/// gets closed.
///
#[function_component(Modal)]
pub fn modal(props: &ModalProperties) -> Html {
    let ouia_id = use_memo(props.ouia_id.clone(), |id| {
        id.clone().unwrap_or(OUIA.generated_id())
    });
    let mut classes = props.variant.as_classes();
    classes.push("pf-v5-c-modal-box");

    let backdrop = use_backdrop();

    let onclose = use_memo((props.onclose.clone(), backdrop), |(onclose, backdrop)| {
        let onclose = onclose.clone();
        let backdrop = backdrop.clone();
        Callback::from(move |()| {
            if let Some(onclose) = &onclose {
                onclose.emit(());
            } else if let Some(backdrop) = &backdrop {
                backdrop.close();
            }
        })
    });

    // escape key
    {
        let disabled = props.disable_close_escape;
        let onclose = onclose.clone();
        use_event_with_window("keydown", move |e: KeyboardEvent| {
            if !disabled && e.key() == "Escape" {
                onclose.emit(());
            }
        });
    }

    // outside click

    let node_ref = use_node_ref();

    {
        let disabled = props.disable_close_click_outside;
        let onclose = onclose.clone();
        use_click_away(node_ref.clone(), move |_: Event| {
            if !disabled {
                onclose.emit(());
            }
        });
    }

    html! (
        <div
            class={classes}
            role="dialog"
            aria-modal="true"
            aria-labelledby="modal-title"
            aria-describedby="modal-description"
            ref={node_ref}
            data-ouia-component-id={(*ouia_id).clone()}
            data-ouia-component-type={props.ouia_type}
            data-ouia-safe={props.ouia_safe}
        >
            if props.show_close {
                <div class="pf-v5-c-modal-box__close">
                    <button
                        class="pf-v5-c-button pf-m-plain"
                        type="button"
                        aria-label="Close dialog"
                        onclick={onclose.reform(|_|())}
                    >
                        <i class="fas fa-times" aria-hidden="true"></i>
                    </button>
                </div>
            }

            <header class="pf-v5-c-modal-box__header">
                <h1
                    class="pf-v5-c-modal-box__title"
                    id="modal-title-modal-with-form"
                >{ &props.title }</h1>
            </header>


            if !&props.description.is_empty() {
                <div class="pf-v5-c-modal-box__body">
                    <p>{ &props.description }</p>
                </div>
            }

            { for props.children.iter().map(|c|{
               { html! (
                    <div class="pf-v5-c-modal-box__body" id="modal-description">{c}</div>
               ) }
            }) }

            if let Some(footer) = &props.footer {
              <footer class="pf-v5-c-modal-box__footer">
                  { footer.clone() }
              </footer>
            }
        </div>
    )
}