Skip to main content

strata_ui/
navigation.rs

1//! High-Level Accessible Navigation Components for Strata UI.
2
3#![allow(non_snake_case)]
4
5use std::rc::Rc;
6use strata_dom::{NodeBlueprint, Signal, View};
7use strata_icons::prelude::{Icon, IconName};
8use strata_macro::{component, rsx};
9
10/// Top navigation bar with slots for branding, center navigation links, and action buttons.
11#[component]
12pub fn Navbar(
13    brand: Option<Vec<NodeBlueprint>>,
14    links: Option<Vec<NodeBlueprint>>,
15    actions: Option<Vec<NodeBlueprint>>,
16    class: Option<&'static str>,
17) -> View {
18    let custom = class.unwrap_or("");
19    let base = "w-full h-16 border-b border-neutral-800 bg-neutral-950/80 backdrop-blur-md px-6 flex items-center justify-between z-40 sticky top-0";
20
21    rsx! {
22        <nav class={format!("{} {}", base, custom)}>
23            <div class="flex items-center gap-6">
24                {brand.unwrap_or_default()}
25                <div class="hidden md:flex items-center gap-4">
26                    {links.unwrap_or_default()}
27                </div>
28            </div>
29            <div class="flex items-center gap-3">
30                {actions.unwrap_or_default()}
31            </div>
32        </nav>
33    }
34}
35
36/// Navigation item descriptor for Sidebar.
37#[derive(Debug, Clone, PartialEq)]
38pub struct SidebarItemData {
39    pub label: &'static str,
40    pub icon: IconName,
41    pub href: Option<&'static str>,
42    pub badge: Option<&'static str>,
43    pub is_active: bool,
44}
45
46/// Collapsible vertical Sidebar navigation.
47#[component]
48pub fn Sidebar(
49    items: Vec<SidebarItemData>,
50    collapsed: Option<Signal<bool>>,
51    header: Option<Vec<NodeBlueprint>>,
52    footer: Option<Vec<NodeBlueprint>>,
53    class: Option<&'static str>,
54) -> View {
55    let is_coll = collapsed.as_ref().map(|s| s.get()).unwrap_or(false);
56    let width = if is_coll { "w-16" } else { "w-64" };
57    let custom = class.unwrap_or("");
58
59    rsx! {
60        <aside class={format!("h-full flex flex-col justify-between border-r border-neutral-800 bg-neutral-950 transition-all duration-200 select-none {} {}", width, custom)}>
61            <div class="flex flex-col">
62                {if let Some(h) = header {
63                    rsx! { <div class="p-4 border-b border-neutral-800/80">{h}</div> }
64                } else {
65                    rsx! { <></> }
66                }}
67                <div class="flex flex-col gap-1 p-2">
68                    {items.into_iter().map(|item| {
69                        let (bg, text) = if item.is_active {
70                            ("bg-indigo-600/10 text-indigo-400 font-semibold", "text-indigo-400")
71                        } else {
72                            ("bg-transparent text-neutral-400 hover:text-white hover:bg-neutral-800/40 font-medium", "text-neutral-400 group-hover:text-white")
73                        };
74                        rsx! {
75                            <a
76                                href={item.href.unwrap_or("#")}
77                                class={format!("group flex items-center gap-3 px-3 py-2.5 rounded-xl text-xs transition-colors cursor-pointer {}", bg)}
78                            >
79                                <span class={text}>
80                                    {Icon(item.icon, Some("w-4 h-4 text-current")).root}
81                                </span>
82                                {if !is_coll {
83                                    rsx! {
84                                        <span class="flex-1 truncate">{item.label}</span>
85                                    }
86                                } else {
87                                    rsx! { <></> }
88                                }}
89                                {if let (false, Some(badge_text)) = (is_coll, item.badge) {
90                                    rsx! {
91                                        <span class="px-1.5 py-0.5 rounded-full text-[10px] font-mono bg-neutral-800 text-neutral-300">
92                                            {badge_text}
93                                        </span>
94                                    }
95                                } else {
96                                    rsx! { <></> }
97                                }}
98                            </a>
99                        }
100                    })}
101                </div>
102            </div>
103            {if let Some(f) = footer {
104                rsx! { <div class="p-4 border-t border-neutral-800/80">{f}</div> }
105            } else {
106                rsx! { <></> }
107            }}
108        </aside>
109    }
110}
111
112/// Breadcrumb navigation item.
113#[derive(Debug, Clone, PartialEq)]
114pub struct BreadcrumbItemData {
115    pub label: &'static str,
116    pub href: Option<&'static str>,
117    pub is_current: bool,
118}
119
120/// Breadcrumb hierarchical navigation trail.
121#[component]
122pub fn Breadcrumb(items: Vec<BreadcrumbItemData>, class: Option<&'static str>) -> View {
123    let custom = class.unwrap_or("");
124    let total = items.len();
125
126    rsx! {
127        <nav aria-label="Breadcrumb" class={format!("flex items-center gap-1.5 text-xs font-mono {}", custom)}>
128            {items.into_iter().enumerate().map(|(idx, item)| {
129                let is_last = idx + 1 == total;
130                let text_color = if item.is_current || is_last {
131                    "text-white font-semibold"
132                } else {
133                    "text-neutral-400 hover:text-neutral-200"
134                };
135
136                rsx! {
137                    <div class="flex items-center gap-1.5">
138                        {if let Some(href) = item.href {
139                            rsx! { <a href={href} class={format!("transition-colors {}", text_color)}>{item.label}</a> }
140                        } else {
141                            rsx! { <span class={text_color}>{item.label}</span> }
142                        }}
143                        {if !is_last {
144                            rsx! {
145                                <span class="text-neutral-600">
146                                    {Icon(IconName::ChevronRight, Some("w-3 h-3 text-current")).root}
147                                </span>
148                            }
149                        } else {
150                            rsx! { <></> }
151                        }}
152                    </div>
153                }
154            })}
155        </nav>
156    }
157}
158
159/// Pagination controls with page navigation and previous/next buttons.
160#[component]
161pub fn Pagination(
162    total_pages: usize,
163    current_page: Signal<usize>,
164    class: Option<&'static str>,
165) -> View {
166    let curr = current_page.get();
167    let prev_sig = current_page.clone();
168    let next_sig = current_page.clone();
169    let custom = class.unwrap_or("");
170
171    rsx! {
172        <div class={format!("inline-flex items-center gap-1.5 select-none {}", custom)}>
173            <button
174                type="button"
175                class={format!("p-2 rounded-lg border border-neutral-800 bg-neutral-900 text-neutral-400 hover:text-white transition-colors cursor-pointer {}", if curr <= 1 { "opacity-40 pointer-events-none" } else { "" })}
176                onclick={move |_| {
177                    if curr > 1 {
178                        prev_sig.set(curr - 1);
179                    }
180                }}
181            >
182                {Icon(IconName::ChevronLeft, Some("w-4 h-4 text-current")).root}
183            </button>
184            <div class="px-3 py-1 text-xs font-mono text-neutral-300">
185                {format!("Page {curr} of {total_pages}")}
186            </div>
187            <button
188                type="button"
189                class={format!("p-2 rounded-lg border border-neutral-800 bg-neutral-900 text-neutral-400 hover:text-white transition-colors cursor-pointer {}", if curr >= total_pages { "opacity-40 pointer-events-none" } else { "" })}
190                onclick={move |_| {
191                    if curr < total_pages {
192                        next_sig.set(curr + 1);
193                    }
194                }}
195            >
196                {Icon(IconName::ChevronRight, Some("w-4 h-4 text-current")).root}
197            </button>
198        </div>
199    }
200}
201
202/// Command item descriptor for CommandPalette (⌘K).
203#[derive(Debug, Clone, PartialEq)]
204pub struct CommandItemData {
205    pub id: &'static str,
206    pub title: &'static str,
207    pub category: &'static str,
208    pub icon: Option<IconName>,
209    pub shortcut: Option<&'static str>,
210}
211
212/// Spotlight Search / Command Palette modal (⌘K).
213#[component]
214pub fn CommandPalette(
215    open: Signal<bool>,
216    query: Signal<String>,
217    items: Vec<CommandItemData>,
218    on_select: Option<Rc<dyn Fn(&'static str)>>,
219) -> View {
220    if !open.get() {
221        return rsx! { <></> };
222    }
223
224    let q = query.get().to_lowercase();
225    let filtered: Vec<CommandItemData> = items
226        .into_iter()
227        .filter(|item| {
228            q.is_empty()
229                || item.title.to_lowercase().contains(&q)
230                || item.category.to_lowercase().contains(&q)
231        })
232        .collect();
233
234    let op = open.clone();
235    let op_backdrop = open.clone();
236
237    rsx! {
238        <div class="fixed inset-0 z-50 flex items-start justify-center pt-24 bg-black/60 backdrop-blur-sm p-4">
239            <div
240                class="fixed inset-0"
241                onclick={move |_| op_backdrop.set(false)}
242            />
243            <div class="relative z-10 w-full max-w-xl rounded-2xl bg-neutral-900 border border-neutral-800 shadow-2xl overflow-hidden flex flex-col">
244                <div class="flex items-center px-4 border-b border-neutral-800">
245                    <span class="text-neutral-500 mr-3">
246                        {Icon(IconName::Search, Some("w-4 h-4 text-current")).root}
247                    </span>
248                    <input
249                        type="text"
250                        class="w-full h-14 bg-transparent text-sm text-white placeholder-neutral-500 focus:outline-none"
251                        placeholder="Type a command or search..."
252                        value={query.get()}
253                    />
254                    <button
255                        type="button"
256                        class="text-neutral-500 hover:text-white p-1"
257                        onclick={move |_| op.set(false)}
258                    >
259                        {Icon(IconName::X, Some("w-4 h-4 text-current")).root}
260                    </button>
261                </div>
262                <div class="max-h-80 overflow-y-auto p-2 flex flex-col gap-1">
263                    {if filtered.is_empty() {
264                        rsx! {
265                            <div class="py-8 text-center text-xs text-neutral-500 font-mono">
266                                {"No commands found."}
267                            </div>
268                        }
269                    } else {
270                        rsx! {
271                            <div>
272                                {filtered.into_iter().map(|item| {
273                                    let id = item.id;
274                                    let sel_cb = on_select.clone();
275                                    let close_sig = open.clone();
276                                    rsx! {
277                                        <div
278                                            class="flex items-center justify-between px-3 py-2.5 rounded-xl hover:bg-neutral-800/80 cursor-pointer select-none group transition-colors"
279                                            onclick={move |_| {
280                                                if let Some(cb) = &sel_cb {
281                                                    cb(id);
282                                                }
283                                                close_sig.set(false);
284                                            }}
285                                        >
286                                            <div class="flex items-center gap-3">
287                                                {if let Some(ic) = item.icon {
288                                                    rsx! {
289                                                        <span class="text-neutral-400 group-hover:text-indigo-400">
290                                                            {Icon(ic, Some("w-4 h-4 text-current")).root}
291                                                        </span>
292                                                    }
293                                                } else {
294                                                    rsx! { <></> }
295                                                }}
296                                                <span class="text-xs font-medium text-neutral-200 group-hover:text-white">{item.title}</span>
297                                            </div>
298                                            {if let Some(sc) = item.shortcut {
299                                                rsx! {
300                                                    <span class="px-1.5 py-0.5 rounded border border-neutral-700 bg-neutral-800 text-[10px] font-mono text-neutral-400">
301                                                        {sc}
302                                                    </span>
303                                                }
304                                            } else {
305                                                rsx! { <></> }
306                                            }}
307                                        </div>
308                                    }
309                                })}
310                            </div>
311                        }
312                    }}
313                </div>
314            </div>
315        </div>
316    }
317}