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
//! Tooltip: shadcn/ui's dark pill that appears on hover.
use gpui::{div, prelude::*, AnyView, App, Context, SharedString, Window};
use crate::styles;
use crate::sx::SxStyled;
styles! {
TOOLTIP = {
// Tooltips sit a little below and right of the pointer.
offset: { padding_left: 2, padding_top: 2.5 },
pill: {
padding_x: 3,
padding_y: 1.5,
radius: md,
background: foreground,
color: background,
font_family: sans,
text: xs,
},
}
}
/// A text tooltip. Attach it to any interactive element:
///
/// ```no_run
/// # use rok_ui::prelude::*;
/// # fn example(window: &mut Window, cx: &mut App) {
/// let help = div().id("help").tooltip(Tooltip::text("Opens the docs"));
/// let save = Button::new("save").tooltip("Save"); // buttons have a shortcut
/// # }
/// ```
pub struct Tooltip {
text: SharedString,
}
impl Tooltip {
/// A tooltip builder for `.tooltip(..)` that shows `text`.
pub fn text(
text: impl Into<SharedString>,
) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static {
let text = text.into();
move |_, cx| {
let text = text.clone();
cx.new(|_| Tooltip { text }).into()
}
}
}
impl Render for Tooltip {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().sx(&TOOLTIP.offset).child(
div()
.sx(&TOOLTIP.pill)
.child(crate::components::bidi_text::text(self.text.clone())),
)
}
}