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
//! Title
use crate::ouia;
use crate::prelude::{ExtendClasses, OuiaComponentType, Size};
use crate::utils::{Ouia, OuiaSafe};
use yew::prelude::*;

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

/// Title level
#[derive(Clone, Default, PartialEq, Eq, Ord, PartialOrd, Copy, Debug)]
pub enum Level {
    #[default]
    H1,
    H2,
    H3,
    H4,
    H5,
    H6,
}

/// Properties for [`Title`]
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TitleProperties {
    #[prop_or_default]
    pub children: Html,
    #[prop_or_default]
    pub level: Level,
    #[prop_or_default]
    pub size: Option<Size>,

    /// 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,
}

/// Title component
///
/// > A **title** component applies top and bottom margins, font-weight, font-size, and line-height to titles. The most common usage for a title is to define headings within a page. For more information about the relationship between title component sizes and HTML heading levels, see the [Typography guidelines](https://www.patternfly.org/v4/guidelines/typography#customizing-heading-levels).
///
/// See: <https://www.patternfly.org/components/title>
///
/// ## Properties
///
/// Defined by [`TitleProperties`].
#[function_component(Title)]
pub fn title(props: &TitleProperties) -> Html {
    let ouia_id = use_memo(props.ouia_id.clone(), |id| {
        id.clone().unwrap_or(OUIA.generated_id())
    });
    let mut class = Classes::from("pf-v5-c-title");

    class.extend_from(&props.size.unwrap_or(match props.level {
        Level::H1 => Size::XXLarge,
        Level::H2 => Size::XLarge,
        Level::H3 => Size::Large,
        Level::H4 => Size::Medium,
        Level::H5 => Size::Medium,
        Level::H6 => Size::Medium,
    }));

    let element = match props.level {
        Level::H1 => "h1",
        Level::H2 => "h2",
        Level::H3 => "h3",
        Level::H4 => "h4",
        Level::H5 => "h5",
        Level::H6 => "h6",
    };

    html! {
        <@{element}
            {class}
            data-ouia-component-id={(*ouia_id).clone()}
            data-ouia-component-type={props.ouia_type}
            data-ouia-safe={props.ouia_safe}
        >
            { props.children.clone() }
        </@>
    }
}