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
use crate::Popper;
use crate::{Orientation, PopperContent};
use yew::prelude::*;
use crate::integration::popperjs;
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TooltipProps {
pub children: Children,
pub text: String,
}
pub struct Tooltip {
node: NodeRef,
active: bool,
}
#[derive(Clone, Debug)]
pub enum TooltipMsg {
Enter,
Leave,
}
impl Component for Tooltip {
type Message = TooltipMsg;
type Properties = TooltipProps;
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);
return 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: &TooltipProps,
_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
ref={r#ref}
styles={styles}
hidden={state.is_none()}
orientation={orientation}
text={props.text.clone()}
/>}
}
}
#[derive(Clone, PartialEq, Properties)]
pub struct TooltipPopupProps {
pub text: String,
pub orientation: Orientation,
#[prop_or_default]
pub hidden: bool,
#[prop_or_default]
pub styles: String,
}
#[function_component(TooltipPopup)]
pub fn tooltip_popup(props: &TooltipPopupProps) -> 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 style={style} class={classes} role="tooltip">
<div class="pf-c-tooltip__arrow"></div>
<div class="pf-c-tooltip__content">
{ &props.text }
</div>
</div>
}
}