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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Collapsible: one disclosure with a single trigger (`.cu-collapsible*` in
//! `custom.css`; Carbon has no direct equivalent).
//!
//! - **Behavior**: the trigger is a Base `Button` (focus, Enter/Space,
//! pointer) that toggles the content, controlled or uncontrolled.
//! - **Appearance**: the trigger is an inline `size-md` row (0.5rem start,
//! 1rem end padding, 0.5rem gap) in `text-primary`, `background-hover` on
//! hover, a 2px inset `focus` outline on keyboard focus only
//! (`.cu-collapsible-trigger:focus-visible`); a 16px `ChevronRight` in
//! `icon-primary` before the label; content padded `0.5rem 0 0.5rem 2rem`.
//! - **Motion**: the chevron turns 90° on open in 110ms
//! (`.cu-collapsible-chev { transition: transform 110ms cubic-bezier(0.2, 0, 0.38, 0.9) }`;
//! a cut under reduced motion). The content mounts and unmounts at once,
//! as the reference's `{open && <div className="cu-collapsible-content">}`.
use super::overlay::{self, OpenChange, rem};
use crate::{
icons::{CarbonIcon, Icon, IconSize},
motion::{self, Motion},
theme::color,
tokens::{ColorRole, SIZE_MD, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, CursorStyle, ElementId, Hsla, IntoElement, ParentElement, RenderOnce,
SharedString, StatefulInteractiveElement as _, Styled, Window, div,
prelude::FluentBuilder as _,
};
use std::rc::Rc;
/// `.cu-collapsible--open .cu-collapsible-chev { transform: rotate(90deg) }`.
pub const OPEN_ROTATION: f32 = std::f32::consts::FRAC_PI_2;
/// A collapsible region. See the module docs.
#[derive(IntoElement)]
pub struct Collapsible {
id: ElementId,
trigger: SharedString,
open: Option<bool>,
default_open: bool,
on_open_change: Option<OpenChange>,
trace: bool,
children: Vec<AnyElement>,
}
impl Collapsible {
/// A collapsible with a stable `id` and a trigger label
/// (`CollapsibleTrigger` children).
pub fn new(id: impl Into<ElementId>, trigger: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
trigger: trigger.into(),
open: None,
default_open: false,
on_open_change: None,
trace: false,
children: Vec::new(),
}
}
/// Controlled open state.
pub fn open(mut self, open: bool) -> Self {
self.open = Some(open);
self
}
/// Initial open state when uncontrolled.
pub fn default_open(mut self, open: bool) -> Self {
self.default_open = open;
self
}
/// Called when the trigger toggles.
pub fn on_open_change(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_open_change = Some(Rc::new(f));
self
}
}
impl Collapsible {
/// Restyle as a Trace disclosure (`.cu-trace .cu-collapsible-trigger
/// { min-height: 0; padding: 0; gap: spacing-02; font-size: 0.75rem; color:
/// link-primary }`, `.cu-trace .cu-collapsible-chev { fill: link-primary }`,
/// `.cu-trace .cu-collapsible-content { padding: spacing-03 0 0 }`). Used by
/// `TraceCollapsible`.
pub fn trace(mut self, trace: bool) -> Self {
self.trace = trace;
self
}
}
impl ParentElement for Collapsible {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Collapsible {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let openness = overlay::openness(
&id,
self.open,
self.default_open,
self.on_open_change.clone(),
window,
cx,
);
let open = openness.open;
let set_open = openness.setter();
let rotation = motion::animate(
(id.clone(), "chevron"),
if open { OPEN_ROTATION } else { 0. },
Motion::TOGGLE,
window,
cx,
);
let hover = window.use_keyed_state((id.clone(), "hover"), cx, |_, _| false);
let bg = motion::animate_color(
(id.clone(), "bg"),
if *hover.read(cx) {
color(ColorRole::BackgroundHover)
} else {
Hsla::transparent_black()
},
Motion::HOVER,
window,
cx,
);
let focus = window
.use_keyed_state((id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
// `:focus-visible`: keyboard focus only.
let focused = focus.is_focused(window) && crate::focus::focus_visible_mode(window);
let trace = self.trace;
let (text, chevron) = if trace {
(color(ColorRole::LinkPrimary), color(ColorRole::LinkPrimary))
} else {
(color(ColorRole::TextPrimary), color(ColorRole::IconPrimary))
};
let trigger = gpui_kit::base::Button::new((id.clone(), "trigger"))
.track_focus(&focus)
.accessibility_label(self.trigger.clone())
.relative()
.flex()
.flex_row()
.items_center()
.map(|this| {
if trace {
this.gap(rem(4.)).type_style(TypeStyle::Label01)
} else {
this.gap(rem(8.))
.min_h(SIZE_MD.rems())
.pl(rem(8.))
.pr(rem(16.))
.type_style(TypeStyle::BodyCompact01)
}
})
.bg(bg)
.text_color(text)
.cursor(CursorStyle::PointingHand)
.on_hover(move |hovered, _, cx| {
hover.update(cx, |h, cx| {
*h = *hovered;
cx.notify();
})
})
.on_click(move |_, window, cx| set_open(!open, window, cx))
.child(
Icon::carbon(CarbonIcon::ChevronRight)
.size(IconSize::S16)
.color(chevron)
.rotate(gpui_kit::radians(rotation)),
)
.child(self.trigger.clone())
.when(focused, |this| {
this.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border_2()
.border_color(color(ColorRole::Focus)),
)
});
let content = open.then(|| {
// `.cu-collapsible-content { padding: spacing-03 0 spacing-03 spacing-07 }`.
div()
.w_full()
.flex()
.flex_col()
.pt(rem(8.))
.when(!trace, |this| this.pb(rem(8.)).pl(rem(32.)))
.children(self.children)
});
div()
.flex()
.flex_col()
.items_start()
.child(trigger)
.when_some(content, |this, content| {
this.child(div().w_full().child(content))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chevron_turns_a_quarter() {
assert_eq!(OPEN_ROTATION, std::f32::consts::FRAC_PI_2);
assert_eq!(Motion::TOGGLE.speed.millis(), 110);
assert_eq!(Motion::TOGGLE.curve.points(), (0.2, 0., 0.38, 0.9));
assert_eq!(SIZE_MD.px_value(), 40.);
}
}