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
use crate::components::button::{BtnColor, BtnState, BtnVariant};
use crate::components::{Align, Tooltip};
use crate::components::{Button, button::ButtonIcon};
use leptos::callback::Callback;
use leptos::prelude::*;
use phosphor_leptos::{ARROW_LINE_LEFT, ARROW_LINE_RIGHT, SIDEBAR};
use tw_merge::tw_merge;
#[derive(Debug, Clone)]
pub struct NavLink {
pub name: String,
pub icon: ButtonIcon,
pub icon_hover: Option<ButtonIcon>,
// pub background_color: crate::colors::Color,
pub url: String,
}
#[derive(Debug, Clone)]
pub enum NavItem {
Link(NavLink),
Divider,
Gap,
}
impl NavItem {
pub fn view_list(links: Vec<NavItem>) -> impl IntoView {
view! { <NavItemList links=links /> }.into_any()
}
}
#[component]
pub fn NavItemList(links: Vec<NavItem>) -> impl IntoView {
let location = leptos_router::hooks::use_location();
let pathname = move || location.pathname.get();
let nav_items = StoredValue::new(links);
view! {
<For
each=move || {
let current_path = pathname();
nav_items
.get_value()
.into_iter()
.map(move |item| { (item, current_path.clone()) })
.collect::<Vec<_>>()
}
key=|(item, path)| format!("{:#?}-{}", item, path)
children=move |(item, current_path)| {
match item {
NavItem::Link(item) => {
{
let href = item.url.clone();
let href2 = item.url.clone();
let is_active = current_path == href;
view! {
<Button
variant=BtnVariant::Tab
icon=item.icon.clone()
// state=if is_active {
// BtnState::Active
// } else {
// BtnState::Default
// }
color=if is_active {
BtnColor::Neutral
} else {
BtnColor::Default
}
on:click=move |ev: web_sys::MouseEvent| {
ev.prevent_default();
window().location().set_href(&item.url).unwrap();
}
href=href2.clone()
class="pr-3"
>
{item.name}
</Button>
}
}
.into_any()
}
NavItem::Divider => view! { <div /> }.into_any(),
NavItem::Gap => view! { <div class="h-2 flex-1" /> }.into_any(),
}
}
/>
}
}
#[component]
pub fn SideBar(links: Vec<NavItem>) -> impl IntoView {
let location = leptos_router::hooks::use_location();
let pathname = move || location.pathname.get();
let nav_items = StoredValue::new(links);
let (is_wide, set_is_wide) = signal(false);
let (is_mobile_open, set_is_mobile_open) = signal(false);
view! {
<>
// Mobile menu toggle button - fixed at top left
<div class="md:hidden fixed top-2 left-2 z-50">
<Button
icon=ButtonIcon::Icon(SIDEBAR)
variant=BtnVariant::Square
on_click=Callback::new(move |_| set_is_mobile_open.set(!is_mobile_open.get()))
/>
</div>
// Mobile overlay backdrop
<Show when=move || is_mobile_open.get()>
<div
class="md:hidden fixed inset-0 bg-black bg-opacity-50 z-40"
on:click=move |_| set_is_mobile_open.set(false)
/>
</Show>
// Sidebar content
<div class=move || {
tw_merge!(
"flex flex-col h-screen justify-start p-2 gap-2 bg-white dark:bg-black z-50",
// Mobile styles
"fixed md:relative left-0 top-0 z-50 md:z-0",
// Mobile visibility
if is_mobile_open.get() { "translate-x-0" } else { "-translate-x-full md:translate-x-0" },
// Desktop width
if is_wide.get() { "w-44" } else { "w-14" }
)
}>
<nav class=move || {
tw_merge!(
"flex flex-col gap-2 justify-start items-start flex-1",
if is_wide.get() { "w-40" } else { "w-14" }
)
}>
<Show
when=move || is_wide.get()
fallback=move || {
view! {
<For
each=move || {
let current_path = pathname();
nav_items
.get_value()
.into_iter()
.map(move |item| { (item, current_path.clone()) })
.collect::<Vec<_>>()
}
key=|(item, path)| format!("{:#?}-{}", item, path)
children=move |(item, current_path)| {
let is_active = match item.clone() {
NavItem::Link(link) => current_path == link.url,
_ => false,
};
match item {
NavItem::Link(link) => {
view! {
<Tooltip label=link.name.clone() align=Align::Right>
<Button
icon=link.icon.clone()
state=MaybeProp::from(
if is_active { BtnState::Active } else { BtnState::Default },
)
href=link.url.clone()
variant=BtnVariant::Square
on_click=Callback::new(move |_| {
set_is_mobile_open.set(false)
})
/>
</Tooltip>
}
.into_any()
}
NavItem::Divider => view! { <div /> }.into_any(),
NavItem::Gap => {
view! { <div class="flex-1 h-full" /> }.into_any()
}
}
}
/>
}
}
>
<For
each=move || {
let current_path = pathname();
nav_items
.get_value()
.into_iter()
.map(move |item| { (item, current_path.clone()) })
.collect::<Vec<_>>()
}
key=|(item, path)| format!("{:#?}-{}", item, path)
children=move |(item, current_path)| {
let is_active = match item.clone() {
NavItem::Link(link) => current_path == link.url,
_ => false,
};
match item {
NavItem::Link(link) => {
view! {
<Button
icon=link.icon.clone()
state=if is_active {
BtnState::Active
} else {
BtnState::Default
}
href=link.url.clone()
variant=BtnVariant::Default
class="w-full"
on_click=Callback::new(move |_| {
set_is_mobile_open.set(false)
})
>
{link.name.clone()}
</Button>
}
.into_any()
}
NavItem::Divider => {
view! { <div /> }
.into_any()
}
NavItem::Gap => view! { <div class="h-2 flex-1" /> }.into_any(),
}
}
/>
</Show>
<div class="hidden md:block">
<Tooltip label="toggle sidebar".to_string() align=Align::Right>
<Show
when=move || !is_wide.get()
fallback=move || {
view! {
<Button
icon=ButtonIcon::Icon(SIDEBAR)
icon_hover=ButtonIcon::Icon(ARROW_LINE_LEFT)
variant=BtnVariant::Square
on_click=Callback::new(move |_| {
set_is_wide.set(!is_wide.get())
})
/>
}
}
>
<Button
icon=ButtonIcon::Icon(SIDEBAR)
icon_hover=ButtonIcon::Icon(ARROW_LINE_RIGHT)
variant=BtnVariant::Square
on_click=Callback::new(move |_| set_is_wide.set(!is_wide.get()))
/>
</Show>
</Tooltip>
</div>
</nav>
</div>
</>
}
}