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
//! Tooltip
use crate::prelude::{Orientation, Popper, PopperContent};

use yew::prelude::*;

use crate::integration::popperjs;

/// Properties for [`Tooltip`]
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TooltipProperties {
    pub children: Children,
    pub text: String,
}

/// Tooltip component
///
/// > A **tooltip** is in-app messaging used to identify elements on a page with short, clarifying text.
///
/// See: <https://www.patternfly.org/v4/components/tooltip>
///
/// ## Properties
///
/// Defined by [`TooltipProperties`].
pub struct Tooltip {
    node: NodeRef,
    active: bool,
}

#[doc(hidden)]
#[derive(Clone, Debug)]
pub enum TooltipMsg {
    Enter,
    Leave,
}

impl Component for Tooltip {
    type Message = TooltipMsg;
    type Properties = TooltipProperties;

    fn create(_: &Context<Self>) -> Self {
        Self {
            node: NodeRef::default(),
            active: false,
        }
    }

    fn update(&mut self, _: &Context<Self>, msg: Self::Message) -> bool {
        log::debug!("Update: {:?}", msg);

        match msg {
            TooltipMsg::Enter => {
                self.active = true;
                true
            }
            TooltipMsg::Leave => {
                self.active = false;
                true
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let enter = ctx.link().callback(|_| TooltipMsg::Enter);
        let leave = ctx.link().callback(|_| TooltipMsg::Leave);

        html! (
            <>
                <Popper<Tooltip> active={self.active} content={ctx.props().clone()}>
                    <span onmouseenter={enter.clone()} onmouseleave={leave.clone()} ref={self.node.clone()}>
                        { for ctx.props().children.iter() }
                    </span>
                </Popper<Tooltip>>
            </>
        )
    }
}

impl PopperContent for Tooltip {
    fn view(
        props: &TooltipProperties,
        _onclose: Callback<()>,
        r#ref: NodeRef,
        state: Option<popperjs::State>,
    ) -> Html {
        let styles = state
            .as_ref()
            .map(|s| s.styles.to_string())
            .unwrap_or_default();
        let orientation = state
            .as_ref()
            .map(|s| s.orientation)
            .unwrap_or(Orientation::Bottom);

        html! {
            <TooltipPopup
                r#ref={r#ref}
                styles={styles}
                hidden={state.is_none()}
                orientation={orientation}
                text={props.text.clone()}
            />
        }
    }
}

/// Properties for [`TooltipPopup`]
#[derive(Clone, PartialEq, Properties)]
pub struct TooltipPopupProperties {
    pub text: String,
    pub orientation: Orientation,
    #[prop_or_default]
    pub hidden: bool,
    #[prop_or_default]
    pub styles: String,
    #[prop_or_default]
    pub r#ref: NodeRef,
}

/// The content shown when the tooltip pops up.
///
/// ## Properties
///
/// Defined by [`TooltipPopupProperties`].
#[function_component(TooltipPopup)]
pub fn tooltip_popup(props: &TooltipPopupProperties) -> Html {
    let mut classes = Classes::from("pf-c-tooltip");

    classes.extend(props.orientation.as_classes());

    let style = if props.hidden {
        "display: none;"
    } else {
        &props.styles
    }
    .to_string();

    html! {
        <div ref={&props.r#ref} style={style} class={classes} role="tooltip">
            <div class="pf-c-tooltip__arrow"></div>
            <div class="pf-c-tooltip__content">
                { &props.text }
            </div>
        </div>
    }
}