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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! HoverCard: a preview of what sits behind a link, shown on hover → Carbon
//! `Popover` without a caret (`index.jsx` `HoverCard`).
//!
//! - **Behavior**: opens `open_delay` (300ms) after the pointer rests on the
//! trigger, or while keyboard focus is inside it; closes 150ms after the
//! pointer leaves both the trigger and the card (so the pointer can travel
//! onto the card); Escape closes it. Kit's `component::hover_card` renders
//! its own rounded surface, so the surface is drawn here and positioned by
//! Base's `Popup` via [`super::floating`].
//! - **Appearance**: the Popover's (`layer-01`, 2px radius, the popover drop shadow,
//! `.cu-popover-content` padding), aligned `bottom-start` by default with a
//! 4px gap (no caret). [`HoverCard::bare`] drops that surface for
//! content that is a popup surface itself (e.g. `ThreadHoverCard`,
//! `.t3-hovercard`), so only one surface is drawn.
//!
//! ```ignore
//! HoverCard::new("profile", Button::new("l", "@carbon-design").variant(ButtonVariant::Link))
//! .child(profile_summary)
//! ```
use super::floating::{self, Align};
use super::overlay::{self, rem};
use super::popover::{CONTENT_MAX_WIDTH, CONTENT_MIN_WIDTH, PopoverLook, RADIUS};
use crate::{tokens::TypeStyle, typography::TypeStyled as _};
use gpui_kit::{
AnyElement, App, ElementId, InteractiveElement, IntoElement, KeyDownEvent, ParentElement,
RenderOnce, StatefulInteractiveElement as _, Styled, Window, div, px,
};
use std::time::Duration;
/// `openDelay` default (`index.jsx`: `openDelay = 300`).
pub const OPEN_DELAY: Duration = Duration::from_millis(300);
/// The close delay (`setTimeout(() => set(defaultOpen), 150)`).
pub const CLOSE_DELAY: Duration = Duration::from_millis(150);
/// A hover card. See the module docs.
#[derive(IntoElement)]
pub struct HoverCard {
id: ElementId,
trigger: AnyElement,
children: Vec<AnyElement>,
align: Align,
default_open: bool,
open_delay: Duration,
bare: bool,
}
impl HoverCard {
/// A hover card with a stable `id` for `trigger`.
pub fn new(id: impl Into<ElementId>, trigger: impl IntoElement) -> Self {
Self {
id: id.into(),
trigger: trigger.into_any_element(),
children: Vec::new(),
align: Align::BottomLeft,
default_open: false,
open_delay: OPEN_DELAY,
bare: false,
}
}
/// One of Carbon's 12 alignments (default `bottom-start`).
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
/// Open initially, and again whenever the pointer leaves (`index.jsx`:
/// leaving runs `set(defaultOpen)` after 150ms).
pub fn default_open(mut self, open: bool) -> Self {
self.default_open = open;
self
}
/// Delay before opening on hover (default 300ms).
pub fn open_delay(mut self, delay: Duration) -> Self {
self.open_delay = delay;
self
}
/// Draw no surface of its own (no background, radius, shadow, padding or
/// width limits): for children that are a complete popup surface, such
/// as `ThreadHoverCard` (`.t3-hovercard`). Default `false`.
pub fn bare(mut self, bare: bool) -> Self {
self.bare = bare;
self
}
}
impl ParentElement for HoverCard {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for HoverCard {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let hover = floating::hover_intent(
(id.clone(), "hover"),
self.default_open,
self.default_open,
window,
cx,
);
let within = window
.use_keyed_state((id.clone(), "within"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let dismissed = window.use_keyed_state((id.clone(), "dismissed"), cx, |_, _| false);
let focused = within.contains_focused(window, cx);
if !focused && *dismissed.read(cx) && !hover.read(cx).open {
dismissed.update(cx, |d, _| *d = false);
}
let open = (hover.read(cx).open || focused) && !*dismissed.read(cx);
let open_delay = self.open_delay;
let on_hover = {
let hover = hover.clone();
move |hovered: &bool, _: &mut Window, cx: &mut App| {
floating::pointer(
&hover,
*hovered,
if *hovered { open_delay } else { CLOSE_DELAY },
cx,
)
}
};
let trigger = div()
.id((id.clone(), "trigger"))
.flex_shrink_0()
.track_focus(&within.clone().tab_stop(false))
.on_hover(on_hover.clone())
.on_key_down({
let dismissed = dismissed.clone();
let hover = hover.clone();
move |event: &KeyDownEvent, _, cx| {
if event.keystroke.key == "escape" {
dismissed.update(cx, |d, cx| {
*d = true;
cx.notify();
});
floating::schedule(&hover, false, Duration::ZERO, cx);
}
}
})
.child(self.trigger)
.child(floating::window_exit_guard(hover.clone(), CLOSE_DELAY))
.into_any_element();
let look = PopoverLook::current();
let bare = self.bare;
let surface = open.then(|| {
let surface = div().id((id.clone(), "surface")).on_hover(on_hover);
if bare {
return surface.children(self.children);
}
surface
.rounded(px(RADIUS))
.bg(look.background)
.text_color(look.text)
.shadow(overlay::popover_drop_shadow())
.min_w(rem(CONTENT_MIN_WIDTH))
.max_w(rem(CONTENT_MAX_WIDTH))
.p(rem(16.))
.type_style(TypeStyle::Body01)
.children(self.children)
});
floating::anchored(
(id, "popup"),
trigger,
surface,
self.align,
None,
0.,
window,
cx,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_follow_index_jsx() {
let card = HoverCard::new("h", div());
assert_eq!(card.align, Align::BottomLeft);
assert_eq!(card.open_delay.as_millis(), 300);
assert!(!card.bare, "the popover surface is drawn by default");
assert!(HoverCard::new("b", div()).bare(true).bare);
assert_eq!(CLOSE_DELAY.as_millis(), 150);
// No caret: 4px gap, start edge 1rem before the trigger center.
let placement = floating::Placement::of(card.align, false, 0.);
assert_eq!((placement.gap, placement.cross_px), (4., -16.));
}
}