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
//! Empty: the Carbon empty-state pattern (`.cu-empty*` in `custom.css`).
//!
//! - `.cu-empty { display: flex; flex-direction: column; align-items: flex-start;
//! gap: var(--spacing-06); max-width: 26rem; padding: var(--spacing-07) 0 }`:
//! left-aligned, 24px between the header and the actions.
//! - `EmptyHeader` stacks media, title and description 8px apart;
//! `EmptyMedia` is `icon-secondary` with 8px below, or with `variant = icon`
//! a 5rem `layer-01` square holding the icon in `icon-primary`;
//! `EmptyTitle` is `heading-03` (20/28) in `text-primary`; `EmptyDescription`
//! `body-01` in `text-secondary`; `EmptyContent` a wrapping row of actions 8px apart.
//! - **Behavior**: the title and description are selectable (Base's
//! `SelectableText`), so what an error state says can be copied. Kit's
//! `Empty` centers its content; Carbon's pattern is left-aligned, so it
//! is drawn here.
use super::parts::part;
use crate::{
theme::color,
tokens::{ColorRole, SPACING_03, SPACING_06, SPACING_07, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::base::SelectableText;
use gpui_kit::{
AnyElement, App, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
Window, div, rems,
};
/// `.cu-empty { max-width: 26rem }` in px.
pub const MAX_WIDTH: f32 = 416.;
/// `.cu-empty-media--icon { width: 5rem; height: 5rem }` in px.
pub const ICON_BOX: f32 = 80.;
part!(
/// `Empty`: the left-aligned state container.
Empty,
|children| div()
.flex()
.flex_col()
.items_start()
.gap(SPACING_06)
.max_w(rems(MAX_WIDTH / 16.))
.py(SPACING_07)
.children(children)
);
part!(
/// `EmptyHeader`: media, title and description, 8px apart.
EmptyHeader,
|children| div().flex().flex_col().gap(SPACING_03).children(children)
);
part!(
/// `EmptyContent`: the actions row (`.cu-empty-content { display: flex;
/// gap: var(--spacing-03); flex-wrap: wrap }`).
EmptyContent,
|children| div()
.flex()
.flex_row()
.flex_wrap()
.items_center()
.gap(SPACING_03)
.children(children)
);
/// `index.d.ts` `EmptyMedia.variant`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum EmptyMediaVariant {
/// The illustration or icon as is, in `icon-secondary`.
#[default]
Default,
/// A 5rem `layer-01` square with the icon in `icon-primary`.
Icon,
}
impl EmptyMediaVariant {
/// Both variants.
pub const ALL: [EmptyMediaVariant; 2] = [Self::Default, Self::Icon];
/// The `index.d.ts` name.
pub const fn name(self) -> &'static str {
match self {
Self::Default => "default",
Self::Icon => "icon",
}
}
/// The media color: `.cu-empty-media { color: var(--icon-secondary) }`,
/// `--icon { color: var(--icon-primary) }`.
pub fn icon_color(self) -> gpui_kit::Hsla {
color(match self {
Self::Default => ColorRole::IconSecondary,
Self::Icon => ColorRole::IconPrimary,
})
}
}
/// `EmptyMedia`. Give icons [`EmptyMedia::icon_color`] (the CSS sets `fill:
/// currentColor`, which GPUI icons do not inherit).
#[derive(IntoElement, Default)]
pub struct EmptyMedia {
variant: EmptyMediaVariant,
children: Vec<AnyElement>,
}
impl EmptyMedia {
/// Default media.
pub fn new() -> Self {
Self::default()
}
/// The variant.
pub fn variant(mut self, variant: EmptyMediaVariant) -> Self {
self.variant = variant;
self
}
/// The color the media's icon should take.
pub fn icon_color(&self) -> gpui_kit::Hsla {
self.variant.icon_color()
}
}
impl ParentElement for EmptyMedia {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for EmptyMedia {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
// `.cu-empty-media { margin-bottom: var(--spacing-03) }`.
let media = div().mb(SPACING_03).text_color(self.variant.icon_color());
match self.variant {
EmptyMediaVariant::Default => media.flex().children(self.children),
// `.cu-empty-media--icon { display: flex; width: 5rem; height: 5rem;
// align-items: center; justify-content: center; background: var(--layer-01) }`.
EmptyMediaVariant::Icon => media
.flex()
.items_center()
.justify_center()
.size(rems(ICON_BOX / 16.))
.bg(color(ColorRole::Layer01))
.children(self.children),
}
}
}
/// `EmptyTitle`: `.cu-empty-title { font-size: 1.25rem; line-height: 1.4;
/// font-weight: 400; color: var(--text-primary) }`.
#[derive(IntoElement)]
pub struct EmptyTitle(SharedString);
impl EmptyTitle {
/// A title.
pub fn new(text: impl Into<SharedString>) -> Self {
Self(text.into())
}
}
impl RenderOnce for EmptyTitle {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.type_style(TypeStyle::Heading03)
.text_color(color(ColorRole::TextPrimary))
.child(SelectableText::new(
ElementId::Name(format!("empty-title:{}", self.0).into()),
self.0,
))
}
}
/// `EmptyDescription`: `.cu-empty-desc { font-size: 0.875rem; line-height:
/// 1.42857; color: var(--text-secondary) }`.
#[derive(IntoElement)]
pub struct EmptyDescription(SharedString);
impl EmptyDescription {
/// A description.
pub fn new(text: impl Into<SharedString>) -> Self {
Self(text.into())
}
}
impl RenderOnce for EmptyDescription {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.type_style(TypeStyle::Body01)
.text_color(color(ColorRole::TextSecondary))
.child(SelectableText::new(
ElementId::Name(format!("empty-desc:{}", self.0).into()),
self.0,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn empty_follows_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!((MAX_WIDTH, ICON_BOX), (416., 80.));
assert_eq!((SPACING_06.px_value(), SPACING_07.px_value()), (24., 32.));
assert_eq!(
EmptyMediaVariant::Default.icon_color(),
color(ColorRole::IconSecondary)
);
assert_eq!(
EmptyMediaVariant::Icon.icon_color(),
color(ColorRole::IconPrimary)
);
}
}