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
//! Code block
use yew::prelude::*;

/// Properties for [`CodeBlock`]
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct CodeBlockProperties {
    #[prop_or_default]
    pub expandable: bool,
    #[prop_or_default]
    pub children: Html,
    #[prop_or_default]
    pub actions: ChildrenWithProps<CodeBlockAction>,
}

/// Code Block component
///
/// > A **code block** is a component that contains 2 or more lines of read-only code. The code in a code block can be copied to the clipboard.
///
/// See: <https://www.patternfly.org/components/code-block>
///
/// ## Properties
///
/// Defined by [`CodeBlockProperties`].
///
/// ## Children
///
/// A code block can contain any children, but is expected to contain a [`CodeBlockCode`] component.
///
/// It may also be wrapped with a detached [`crate::prelude::ExpandableSection`] component. The
/// [`crate::prelude::ExpandableSectionToggle`] would then be a child of this component, but stay outside the nested
/// code component.
///
/// ## Example
///
/// A simple example would be:
///
/// ```rust
/// use yew::prelude::*;
/// use patternfly_yew::prelude::*;
///
/// #[function_component(Example)]
/// fn example() -> Html {
///   html!(
///     <CodeBlock>
///       <CodeBlockCode>{r#"some code"#}</CodeBlockCode>
///     </CodeBlock>
///   )
/// }
/// ```
#[function_component(CodeBlock)]
pub fn code_block(props: &CodeBlockProperties) -> Html {
    html!(
        <div class="pf-v5-c-code-block">
            if !props.actions.is_empty() {
                <div class="pf-v5-c-code-block__header">
                    <div class="pf-v5-c-code-block__actions">
                        { for props.actions.iter() }
                    </div>
                </div>
            }

            <div class="pf-v5-c-code-block__content">
                { props.children.clone() }
            </div>
        </div>
    )
}

/// Properties for [`CodeBlockCode`]
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct CodeBlockCodeProperties {
    #[prop_or_default]
    pub children: Html,
}

/// The actual code component of the Code Block component.
///
/// ## Properties
///
/// Defined by [`CodeBlockCodeProperties`].
#[function_component(CodeBlockCode)]
pub fn code_block_code(props: &CodeBlockCodeProperties) -> Html {
    html!(
        <pre class="pf-v5-c-code-block__pre">
            <code class="pf-v5-c-code-block__code">{ props.children.clone() }</code>
        </pre>
    )
}

/// Properties for [`CodeBlockAction`]
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct CodeBlockActionProperties {
    #[prop_or_default]
    pub children: Html,
}

/// An action of a [`CodeBlock`]
#[function_component(CodeBlockAction)]
pub fn code_block_action(props: &CodeBlockActionProperties) -> Html {
    html!(
        <div class="pf-v5-c-code-block__actions-item">
            { props.children.clone() }
        </div>
    )
}