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
use crate::prelude::*;
use component::{Component, ComponentScope, example_group_with_title, single_example};
#[derive(IntoElement, RegisterComponent)]
pub struct ListSubHeader {
label: SharedString,
start_slot: Option<IconName>,
end_slot: Option<AnyElement>,
inset: bool,
selected: bool,
}
impl ListSubHeader {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
start_slot: None,
end_slot: None,
inset: false,
selected: false,
}
}
pub fn left_icon(mut self, left_icon: Option<IconName>) -> Self {
self.start_slot = left_icon;
self
}
pub fn end_slot(mut self, end_slot: AnyElement) -> Self {
self.end_slot = Some(end_slot);
self
}
pub fn inset(mut self, inset: bool) -> Self {
self.inset = inset;
self
}
}
impl Toggleable for ListSubHeader {
fn toggle_state(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
}
impl RenderOnce for ListSubHeader {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
h_flex()
.flex_1()
.w_full()
.relative()
.pb(DynamicSpacing::Base04.rems(cx))
.px(DynamicSpacing::Base02.rems(cx))
.child(
div()
.h_5()
.when(self.inset, |this| this.px_2())
.when(self.selected, |this| this.bg(semantic::active_bg(cx)))
.flex()
.flex_1()
.w_full()
.gap_1()
.items_center()
.justify_between()
.child(
div()
.flex()
.gap_1()
.items_center()
.children(
self.start_slot.map(|i| {
Icon::new(i).color(Color::Muted).size(IconSize::Small)
}),
)
.child(
Label::new(self.label.clone())
.color(Color::Muted)
.size(LabelSize::Small),
),
)
.children(self.end_slot),
)
}
}
impl Component for ListSubHeader {
fn scope() -> ComponentScope {
ComponentScope::DataDisplay
}
fn description() -> Option<&'static str> {
Some(
"A sub-header component for organizing list content into subsections with optional icons and end slots.",
)
}
fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
Some(
v_flex()
.gap_6()
.children(vec![
example_group_with_title(
"Basic Sub-headers",
vec![
single_example(
"Simple",
ListSubHeader::new("Subsection").into_any_element(),
),
single_example(
"With Icon",
ListSubHeader::new("Documents")
.left_icon(Some(IconName::File))
.into_any_element(),
),
single_example(
"With End Slot",
ListSubHeader::new("Recent")
.end_slot(
Label::new("3").color(Color::Muted).into_any_element(),
)
.into_any_element(),
),
],
),
example_group_with_title(
"States",
vec![
single_example(
"Selected",
ListSubHeader::new("Selected")
.toggle_state(true)
.into_any_element(),
),
single_example(
"Inset",
ListSubHeader::new("Inset Sub-header")
.inset(true)
.into_any_element(),
),
],
),
])
.into_any_element(),
)
}
}