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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Card — port of `@heroui/card`.
use gpui::{
prelude::*, px, AnyElement, App, IntoElement, ParentElement, Pixels, RenderOnce, Window,
};
use herogpui_theme::ActiveTheme;
/// Card prominence level. Every fill level paints its surface shade and
/// carries the surface shadow; `transparent` paints nothing.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CardVariant {
/// No background — for cards with custom painting.
Transparent,
/// `bg-surface`
#[default]
Default,
/// `bg-surface-secondary`
Secondary,
/// `bg-surface-tertiary`
Tertiary,
}
impl CardVariant {
/// Every card variant, in declaration order.
pub const ALL: [CardVariant; 4] = [
CardVariant::Transparent,
CardVariant::Default,
CardVariant::Secondary,
CardVariant::Tertiary,
];
/// A human-readable label for this variant.
pub fn label(self) -> &'static str {
match self {
CardVariant::Transparent => "Transparent",
CardVariant::Default => "Default",
CardVariant::Secondary => "Secondary",
CardVariant::Tertiary => "Tertiary",
}
}
}
/// HeroUI Card container.
#[must_use = "a component does nothing until it is rendered: add it as a child or return it from `render`"]
#[derive(IntoElement)]
pub struct Card {
variant: CardVariant,
width: Option<Pixels>,
children: Vec<AnyElement>,
/// The corner radius, in place of `min(32px, --radius-3xl)`.
radius: Option<Pixels>,
/// The `sx` slot, refined over the root style at the end of render.
sx: Option<Box<gpui::StyleRefinement>>,
}
impl Card {
/// Creates an empty card.
pub fn new() -> Self {
Self {
variant: CardVariant::Default,
width: None,
children: Vec::new(),
radius: None,
sx: None,
}
}
/// Sets the card variant.
pub fn variant(mut self, variant: CardVariant) -> Self {
self.variant = variant;
self
}
/// Fixed card width.
pub fn w(mut self, v: impl Into<Pixels>) -> Self {
self.width = Some(v.into());
self
}
/// The corner radius, in place of `min(32px, --radius-3xl)`. Not a v3
/// prop; the removed v2 `radius` prop is prohibited and this is a
/// per-component repository extension.
pub fn radius(mut self, radius: impl Into<Pixels>) -> Self {
self.radius = Some(radius.into());
self
}
/// The one slot for caller-owned low-level styling: GPUI's styling methods
/// (`bg`, `text_color`, `w`, `h`, `p`, `rounded`, `border_color`, …)
/// applied to the card's root element after every value the variant and
/// the active theme chose, so they win.
pub fn sx(mut self, style: impl FnOnce(gpui::Div) -> gpui::Div) -> Self {
crate::util::refine_sx(&mut self.sx, style);
self
}
}
impl Default for Card {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for Card {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Card {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let colors = cx.colors();
let layout = cx.layout();
// `.card` is `flex flex-col gap-3 p-4`: the card is the padded box and
// its parts (`__header`, `__content`, `__footer`) carry none of their
// own, which is why this used to double the inset on every section.
let mut el = gpui::div()
.flex()
.flex_col()
.gap(px(12.))
.p(px(16.))
.rounded(
self.radius
.unwrap_or_else(|| crate::util::container_radius(cx)),
)
.children(self.children);
// Upstream `.card` is `overflow-visible`: no clipping call here.
if let Some(w) = self.width {
el = el.w(w);
}
// `card--default`/`--secondary`/`--tertiary` set the background;
// `card--transparent` is `border-none bg-transparent shadow-none`, so
// it paints nothing and keeps the full content box for its parts.
el = match self.variant {
CardVariant::Transparent => el,
CardVariant::Default => el.bg(colors.surface.background),
CardVariant::Secondary => el.bg(colors.surface_secondary),
CardVariant::Tertiary => el.bg(colors.surface_tertiary),
};
if self.variant != CardVariant::Transparent && !layout.surface_shadow.is_empty() {
el = el.shadow(layout.surface_shadow.clone());
}
el = crate::util::apply_sx(el, &self.sx);
el
}
}
/// Card header section (`CardHeader`).
#[must_use = "a component does nothing until it is rendered: add it as a child or return it from `render`"]
#[derive(IntoElement)]
pub struct CardHeader {
children: Vec<AnyElement>,
/// The `sx` slot, refined over the root style at the end of render.
sx: Option<Box<gpui::StyleRefinement>>,
}
impl CardHeader {
/// Creates an empty instance.
pub fn new() -> Self {
Self {
children: Vec::new(),
sx: None,
}
}
/// The one slot for caller-owned low-level styling: GPUI's styling methods
/// (`bg`, `text_color`, `w`, `h`, `p`, `rounded`, `border_color`, …)
/// applied to the header's root element after every value the card and the
/// active theme chose, so they win.
pub fn sx(mut self, style: impl FnOnce(gpui::Div) -> gpui::Div) -> Self {
crate::util::refine_sx(&mut self.sx, style);
self
}
}
impl Default for CardHeader {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for CardHeader {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for CardHeader {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
// `.card__header` is `flex flex-col` and nothing else: the title's
// text style belongs to `CardTitle` and the description's to
// `CardDescription`.
let el = gpui::div().flex().flex_col().children(self.children);
crate::util::apply_sx(el, &self.sx)
}
}
/// Card title (`CardTitle`, upstream `.card__title`).
#[must_use = "a component does nothing until it is rendered: add it as a child or return it from `render`"]
#[derive(IntoElement)]
pub struct CardTitle {
children: Vec<AnyElement>,
/// The `sx` slot, refined over the root style at the end of render.
sx: Option<Box<gpui::StyleRefinement>>,
}
impl CardTitle {
/// Creates an empty instance.
pub fn new() -> Self {
Self {
children: Vec::new(),
sx: None,
}
}
/// The one slot for caller-owned low-level styling: GPUI's styling methods
/// (`bg`, `text_color`, `w`, `h`, `p`, `rounded`, `border_color`, …)
/// applied to the title's root element after every value the card and the
/// active theme chose, so they win.
pub fn sx(mut self, style: impl FnOnce(gpui::Div) -> gpui::Div) -> Self {
crate::util::refine_sx(&mut self.sx, style);
self
}
}
impl Default for CardTitle {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for CardTitle {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for CardTitle {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
// `.card__title` is `text-sm leading-6 font-medium text-foreground`.
let colors = cx.colors();
let el = gpui::div()
.text_size(px(14.))
.line_height(px(24.))
.font_weight(gpui::FontWeight::MEDIUM)
.text_color(colors.foreground)
.children(self.children);
crate::util::apply_sx(el, &self.sx)
}
}
/// Card description (`CardDescription`, upstream `.card__description`).
#[must_use = "a component does nothing until it is rendered: add it as a child or return it from `render`"]
#[derive(IntoElement)]
pub struct CardDescription {
children: Vec<AnyElement>,
/// The `sx` slot, refined over the root style at the end of render.
sx: Option<Box<gpui::StyleRefinement>>,
}
impl CardDescription {
/// Creates an empty instance.
pub fn new() -> Self {
Self {
children: Vec::new(),
sx: None,
}
}
/// The one slot for caller-owned low-level styling: GPUI's styling methods
/// (`bg`, `text_color`, `w`, `h`, `p`, `rounded`, `border_color`, …)
/// applied to the description's root element after every value the card and
/// the active theme chose, so they win.
pub fn sx(mut self, style: impl FnOnce(gpui::Div) -> gpui::Div) -> Self {
crate::util::refine_sx(&mut self.sx, style);
self
}
}
impl Default for CardDescription {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for CardDescription {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for CardDescription {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
// `.card__description` is `text-sm leading-5 text-muted`.
let colors = cx.colors();
let el = gpui::div()
.text_size(px(14.))
.line_height(px(20.))
.text_color(colors.muted)
.children(self.children);
crate::util::apply_sx(el, &self.sx)
}
}
/// Content section (`CardContent`, upstream `.card__content`).
#[must_use = "a component does nothing until it is rendered: add it as a child or return it from `render`"]
#[derive(IntoElement)]
pub struct CardContent {
children: Vec<AnyElement>,
/// The `sx` slot, refined over the root style at the end of render.
sx: Option<Box<gpui::StyleRefinement>>,
}
impl CardContent {
/// Creates an empty instance.
pub fn new() -> Self {
Self {
children: Vec::new(),
sx: None,
}
}
/// The one slot for caller-owned low-level styling: GPUI's styling methods
/// (`bg`, `text_color`, `w`, `h`, `p`, `rounded`, `border_color`, …)
/// applied to the content's root element after every value the card and the
/// active theme chose, so they win.
pub fn sx(mut self, style: impl FnOnce(gpui::Div) -> gpui::Div) -> Self {
crate::util::refine_sx(&mut self.sx, style);
self
}
}
impl Default for CardContent {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for CardContent {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for CardContent {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
// `.card__content` is `flex flex-1 flex-col gap-1`. The upstream
// `flex-1` is dropped: the pinned-geometry test in `tests/card_deep.rs`
// measures the card as an auto-height column hugging its parts, which
// flex-1 regresses.
let el = gpui::div()
.flex()
.flex_col()
.gap(px(4.))
.children(self.children);
crate::util::apply_sx(el, &self.sx)
}
}
/// Card footer section (`CardFooter`).
#[must_use = "a component does nothing until it is rendered: add it as a child or return it from `render`"]
#[derive(IntoElement)]
pub struct CardFooter {
children: Vec<AnyElement>,
/// The `sx` slot, refined over the root style at the end of render.
sx: Option<Box<gpui::StyleRefinement>>,
}
impl CardFooter {
/// Creates an empty instance.
pub fn new() -> Self {
Self {
children: Vec::new(),
sx: None,
}
}
/// The one slot for caller-owned low-level styling: GPUI's styling methods
/// (`bg`, `text_color`, `w`, `h`, `p`, `rounded`, `border_color`, …)
/// applied to the footer's root element after every value the card and the
/// active theme chose, so they win.
pub fn sx(mut self, style: impl FnOnce(gpui::Div) -> gpui::Div) -> Self {
crate::util::refine_sx(&mut self.sx, style);
self
}
}
impl Default for CardFooter {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for CardFooter {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for CardFooter {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
// `.card__footer` is `flex flex-row items-center` -- no padding, gap,
// or text size of its own; the card's gap separates the parts and the
// caller composes the row's contents.
let el = gpui::div().flex().items_center().children(self.children);
crate::util::apply_sx(el, &self.sx)
}
}
crate::util::impl_component_styled!(
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
CardFooter
);