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
//! Tabs: a segmented list of tab triggers. You render the active panel yourself,
//! the way a controlled React component works.
use std::rc::Rc;
use gpui::{div, prelude::*, App, ElementId, SharedString, StyleRefinement, Window};
use crate::sx::SxStyled;
use crate::{hooks::EventHandler, styles, styles::ApplyStyleOverrides};
/// ```no_run
/// # use rok_ui::prelude::*;
/// # fn example(window: &mut Window, cx: &mut App) {
/// # fn account_panel() -> AnyElement { div().into_any_element() }
/// # fn password_panel() -> AnyElement { div().into_any_element() }
/// let selected_tab = use_state(window, cx, || 0usize);
/// let selected = selected_tab.get(cx);
/// let settings = div()
/// .child(
/// Tabs::new("settings-tabs")
/// .tab("Account")
/// .tab("Password")
/// .selected_index(selected)
/// .on_change(move |index, _, cx| selected_tab.set(*index, cx)),
/// )
/// .child(if selected == 0 { account_panel() } else { password_panel() });
/// # }
/// ```
#[derive(IntoElement)]
pub struct Tabs {
id: ElementId,
tab_labels: Vec<SharedString>,
selected_index: usize,
on_change: Option<EventHandler<usize>>,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(Tabs);
impl Tabs {
/// An empty tab list.
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
tab_labels: Vec::new(),
selected_index: 0,
on_change: None,
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
/// Add a tab trigger.
#[must_use]
pub fn tab(mut self, label: impl Into<SharedString>) -> Self {
self.tab_labels.push(label.into());
self
}
/// The selected tab.
#[must_use]
pub fn selected_index(mut self, selected_index: usize) -> Self {
self.selected_index = selected_index;
self
}
/// Receives the index of the tab the user picked.
#[must_use]
pub fn on_change(mut self, handler: impl Fn(&usize, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Rc::new(handler));
self
}
}
styles! {
TABS = {
list: {
display: flex,
align: center,
height: 9,
padding: 0.75,
radius: lg,
background: muted,
color: muted_foreground,
},
trigger: {
display: flex,
flex: 1,
align: center,
justify: center,
height: full,
padding_x: 2,
radius: md,
border: 1,
text: sm,
font: medium,
whitespace: nowrap,
cursor: pointer,
focus: { shadow: ring },
},
selected: {
background: background,
border_color: border,
color: foreground,
shadow: xs,
},
unselected: {
border_color: transparent,
color: muted_foreground,
hover: { color: foreground },
},
}
}
impl RenderOnce for Tabs {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let selected_index = self.selected_index;
let on_change = self.on_change;
let triggers = self
.tab_labels
.into_iter()
.enumerate()
.map(|(tab_index, label)| {
let on_change = on_change.clone();
div()
.id(tab_index)
.tab_index(0)
.sx((
&TABS.trigger,
if tab_index == selected_index {
&TABS.selected
} else {
&TABS.unselected
},
))
.when_some(on_change, |trigger, handler| {
trigger.on_click(move |_, window, cx| handler(&tab_index, window, cx))
})
.child(crate::components::bidi_text::text(label))
});
div()
.id(self.id)
.sx((&TABS.list, &self.sx))
.children(triggers)
.apply_style_overrides(&self.style_overrides)
}
}