Skip to main content

strata_ui/
data_display.rs

1//! High-Level Accessible Data Display 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/// Card container with subtle glassmorphic backdrop and border.
11#[component]
12pub fn Card(children: Option<Vec<NodeBlueprint>>, class: Option<&'static str>) -> View {
13    let custom = class.unwrap_or("");
14    let base = "rounded-2xl bg-neutral-900/60 border border-neutral-800 shadow-xl overflow-hidden backdrop-blur-sm";
15    let combined = format!("{base} {custom}");
16    let classes: Vec<String> = combined.split_whitespace().map(|s| s.to_string()).collect();
17
18    View::new(NodeBlueprint::element(
19        strata_dom::Tag::Div,
20        classes,
21        Vec::new(),
22        children.unwrap_or_default(),
23    ))
24}
25
26/// Header slot for Card containers.
27#[component]
28pub fn CardHeader(
29    title: Option<&'static str>,
30    description: Option<&'static str>,
31    children: Option<Vec<NodeBlueprint>>,
32    class: Option<&'static str>,
33) -> View {
34    let custom = class.unwrap_or("");
35    rsx! {
36        <div class={format!("p-6 border-b border-neutral-800/80 flex flex-col gap-1.5 {}", custom)}>
37            {if let Some(t) = title {
38                rsx! { <h3 class="text-base font-semibold text-white tracking-tight">{t}</h3> }
39            } else {
40                rsx! { <></> }
41            }}
42            {if let Some(d) = description {
43                rsx! { <p class="text-xs text-neutral-400">{d}</p> }
44            } else {
45                rsx! { <></> }
46            }}
47            {children.unwrap_or_default()}
48        </div>
49    }
50}
51
52/// Body content slot for Card containers.
53#[component]
54pub fn CardContent(children: Option<Vec<NodeBlueprint>>, class: Option<&'static str>) -> View {
55    let custom = class.unwrap_or("");
56    rsx! {
57        <div class={format!("p-6 {}", custom)}>
58            {children.unwrap_or_default()}
59        </div>
60    }
61}
62
63/// Footer slot for Card containers with top border divider.
64#[component]
65pub fn CardFooter(children: Option<Vec<NodeBlueprint>>, class: Option<&'static str>) -> View {
66    let custom = class.unwrap_or("");
67    rsx! {
68        <div class={format!("px-6 py-4 border-t border-neutral-800/80 flex items-center justify-between {}", custom)}>
69            {children.unwrap_or_default()}
70        </div>
71    }
72}
73
74/// Visual status badge variants.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
76pub enum BadgeVariant {
77    #[default]
78    Default,
79    Success,
80    Warning,
81    Destructive,
82    Outline,
83    Indigo,
84}
85
86/// Visual status Badge pill.
87#[component]
88pub fn Badge(
89    variant: Option<BadgeVariant>,
90    children: Option<Vec<NodeBlueprint>>,
91    class: Option<&'static str>,
92) -> View {
93    let var = variant.unwrap_or_default();
94    let var_classes = match var {
95        BadgeVariant::Default => "bg-neutral-800 text-neutral-300 border-neutral-700",
96        BadgeVariant::Success => "bg-emerald-500/10 text-emerald-400 border-emerald-500/20",
97        BadgeVariant::Warning => "bg-amber-500/10 text-amber-400 border-amber-500/20",
98        BadgeVariant::Destructive => "bg-rose-500/10 text-rose-400 border-rose-500/20",
99        BadgeVariant::Outline => "bg-transparent text-white border-neutral-700",
100        BadgeVariant::Indigo => "bg-indigo-500/10 text-indigo-400 border-indigo-500/20",
101    };
102
103    let custom = class.unwrap_or("");
104    let combined = format!("inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-mono font-medium border select-none {var_classes} {custom}");
105
106    rsx! {
107        <span class={combined}>
108            {children.unwrap_or_default()}
109        </span>
110    }
111}
112
113/// Interactive dismissable Tag / Chip with close button.
114#[component]
115pub fn Tag(
116    label: &'static str,
117    onclose: Option<Rc<dyn Fn()>>,
118    class: Option<&'static str>,
119) -> View {
120    let custom = class.unwrap_or("");
121    let cb = onclose.clone();
122
123    rsx! {
124        <span class={format!("inline-flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-xs font-medium bg-neutral-800 border border-neutral-700 text-neutral-200 select-none {}", custom)}>
125            <span>{label}</span>
126            {if let Some(on_c) = cb {
127                rsx! {
128                    <button
129                        type="button"
130                        class="text-neutral-400 hover:text-white cursor-pointer -mr-0.5"
131                        onclick={move |_| on_c()}
132                    >
133                        {Icon(IconName::X, Some("w-3 h-3 text-current")).root}
134                    </button>
135                }
136            } else {
137                rsx! { <></> }
138            }}
139        </span>
140    }
141}
142
143/// User profile Avatar with initials fallback and status dot.
144#[component]
145pub fn Avatar(
146    fallback: &'static str,
147    status: Option<&'static str>,
148    class: Option<&'static str>,
149) -> View {
150    let custom = class.unwrap_or("");
151    let base = "w-9 h-9 rounded-full bg-gradient-to-tr from-indigo-500 to-purple-600 flex items-center justify-center text-xs font-bold text-white ring-2 ring-neutral-800 select-none relative";
152    let combined = format!("{base} {custom}");
153
154    rsx! {
155        <div class={combined}>
156            {fallback}
157            {if let Some(st) = status {
158                let dot_color = match st {
159                    "online" => "bg-emerald-500",
160                    "busy" => "bg-rose-500",
161                    "away" => "bg-amber-500",
162                    _ => "bg-neutral-500",
163                };
164                rsx! {
165                    <span class={format!("absolute bottom-0 right-0 w-2.5 h-2.5 rounded-full ring-2 ring-neutral-900 {}", dot_color)} />
166                }
167            } else {
168                rsx! { <></> }
169            }}
170        </div>
171    }
172}
173
174/// KPI Metric / Statistic block with value, label, and trend pill.
175#[component]
176pub fn Stat(
177    label: &'static str,
178    value: &'static str,
179    trend: Option<f32>,
180    subtitle: Option<&'static str>,
181    class: Option<&'static str>,
182) -> View {
183    let custom = class.unwrap_or("");
184
185    rsx! {
186        <div class={format!("p-5 rounded-2xl bg-neutral-900/60 border border-neutral-800 shadow-lg flex flex-col justify-between {}", custom)}>
187            <div class="flex items-center justify-between text-neutral-400 text-xs font-mono uppercase">
188                <span>{label}</span>
189                {if let Some(t) = trend {
190                    let (color, icon) = if t >= 0.0 {
191                        ("text-emerald-400 bg-emerald-500/10 border-emerald-500/20", IconName::ArrowUpRight)
192                    } else {
193                        ("text-rose-400 bg-rose-500/10 border-rose-500/20", IconName::ArrowDownLeft)
194                    };
195                    rsx! {
196                        <span class={format!("inline-flex items-center gap-1 px-1.5 py-0.5 rounded border text-xs font-mono font-semibold {}", color)}>
197                            {Icon(icon, Some("w-3 h-3 text-current")).root}
198                            {format!("{t:+.1}%")}
199                        </span>
200                    }
201                } else {
202                    rsx! { <></> }
203                }}
204            </div>
205            <div class="mt-3 text-2xl font-bold font-mono text-white tracking-tight">
206                {value}
207            </div>
208            {if let Some(sub) = subtitle {
209                rsx! { <div class="mt-1 text-xs text-neutral-500">{sub}</div> }
210            } else {
211                rsx! { <></> }
212            }}
213        </div>
214    }
215}
216
217/// Segmented tab switcher and tab trigger button.
218#[component]
219pub fn Tabs(
220    items: Vec<&'static str>,
221    active_index: Signal<usize>,
222    class: Option<&'static str>,
223) -> View {
224    let active = active_index.get();
225    let custom = class.unwrap_or("");
226
227    rsx! {
228        <div class={format!("inline-flex items-center p-1 rounded-xl bg-neutral-900 border border-neutral-800 gap-1 {}", custom)}>
229            {items.into_iter().enumerate().map(|(idx, title)| {
230                let is_sel = idx == active;
231                let active_sig = active_index.clone();
232                let (bg, text) = if is_sel {
233                    ("bg-neutral-800 text-white shadow-sm", "font-semibold")
234                } else {
235                    ("bg-transparent text-neutral-400 hover:text-neutral-200", "font-normal")
236                };
237                rsx! {
238                    <button
239                        type="button"
240                        class={format!("px-3.5 py-1.5 rounded-lg text-xs transition-all duration-150 cursor-pointer select-none {} {}", bg, text)}
241                        onclick={move |_| active_sig.set(idx)}
242                    >
243                        {title}
244                    </button>
245                }
246            })}
247        </div>
248    }
249}
250
251/// Collapsible Accordion section with toggleable expansion state.
252#[component]
253pub fn AccordionItem(
254    title: &'static str,
255    open: Signal<bool>,
256    children: Option<Vec<NodeBlueprint>>,
257    class: Option<&'static str>,
258) -> View {
259    let is_open = open.get();
260    let op = open.clone();
261    let custom = class.unwrap_or("");
262    let chevron_rot = if is_open { "rotate-180" } else { "rotate-0" };
263
264    rsx! {
265        <div class={format!("border-b border-neutral-800 py-3 w-full {}", custom)}>
266            <button
267                type="button"
268                class="flex items-center justify-between w-full text-left font-medium text-sm text-neutral-200 hover:text-white cursor-pointer select-none"
269                onclick={move |_| op.update(|o| *o = !*o)}
270            >
271                <span>{title}</span>
272                <span class={format!("transition-transform duration-200 text-neutral-400 {}", chevron_rot)}>
273                    {Icon(IconName::ChevronDown, Some("w-4 h-4 text-current")).root}
274                </span>
275            </button>
276            {if is_open {
277                rsx! {
278                    <div class="pt-3 text-xs text-neutral-400 leading-relaxed">
279                        {children.unwrap_or_default()}
280                    </div>
281                }
282            } else {
283                rsx! { <></> }
284            }}
285        </div>
286    }
287}
288
289/// Semantic data Table container.
290#[component]
291pub fn Table(children: Option<Vec<NodeBlueprint>>, class: Option<&'static str>) -> View {
292    let custom = class.unwrap_or("");
293    rsx! {
294        <div class="w-full overflow-x-auto rounded-xl border border-neutral-800">
295            <table class={format!("w-full text-left text-sm text-neutral-300 {}", custom)}>
296                {children.unwrap_or_default()}
297            </table>
298        </div>
299    }
300}
301
302/// Table head row container.
303#[component]
304pub fn TableHeader(children: Option<Vec<NodeBlueprint>>) -> View {
305    rsx! {
306        <thead class="bg-neutral-900/80 border-b border-neutral-800 text-xs font-mono uppercase tracking-wider text-neutral-400">
307            {children.unwrap_or_default()}
308        </thead>
309    }
310}
311
312/// Table body container.
313#[component]
314pub fn TableBody(children: Option<Vec<NodeBlueprint>>) -> View {
315    rsx! {
316        <tbody class="divide-y divide-neutral-800/60 bg-neutral-950/40">
317            {children.unwrap_or_default()}
318        </tbody>
319    }
320}
321
322/// Table row.
323#[component]
324pub fn TableRow(children: Option<Vec<NodeBlueprint>>, class: Option<&'static str>) -> View {
325    let custom = class.unwrap_or("");
326    rsx! {
327        <tr class={format!("hover:bg-neutral-800/30 transition-colors {}", custom)}>
328            {children.unwrap_or_default()}
329        </tr>
330    }
331}
332
333/// Table header cell.
334#[component]
335pub fn TableHead(children: Option<Vec<NodeBlueprint>>, class: Option<&'static str>) -> View {
336    let custom = class.unwrap_or("");
337    rsx! {
338        <th class={format!("px-4 py-3 font-semibold text-neutral-300 {}", custom)}>
339            {children.unwrap_or_default()}
340        </th>
341    }
342}
343
344/// Table data cell.
345#[component]
346pub fn TableCell(children: Option<Vec<NodeBlueprint>>, class: Option<&'static str>) -> View {
347    let custom = class.unwrap_or("");
348    rsx! {
349        <td class={format!("px-4 py-3 text-neutral-300 {}", custom)}>
350            {children.unwrap_or_default()}
351        </td>
352    }
353}
354
355/// Vertical timeline item for activity feeds.
356#[component]
357pub fn TimelineItem(
358    title: &'static str,
359    timestamp: &'static str,
360    description: Option<&'static str>,
361    is_last: Option<bool>,
362) -> View {
363    let last = is_last.unwrap_or(false);
364
365    rsx! {
366        <div class="relative pl-6 pb-6 group">
367            {if !last {
368                rsx! { <div class="absolute left-2.5 top-3 bottom-0 w-px bg-neutral-800" /> }
369            } else {
370                rsx! { <></> }
371            }}
372            <div class="absolute left-1 top-1.5 w-3.5 h-3.5 rounded-full border-2 border-indigo-600 bg-neutral-950" />
373            <div class="flex items-center justify-between text-xs font-mono text-neutral-400">
374                <span class="font-semibold text-neutral-200">{title}</span>
375                <span>{timestamp}</span>
376            </div>
377            {if let Some(desc) = description {
378                rsx! { <p class="mt-1 text-xs text-neutral-400 leading-relaxed">{desc}</p> }
379            } else {
380                rsx! { <></> }
381            }}
382        </div>
383    }
384}
385
386/// Keyboard shortcut key badge (e.g. ⌘K, Shift+Enter).
387#[component]
388pub fn Kbd(keys: &'static str, class: Option<&'static str>) -> View {
389    let custom = class.unwrap_or("");
390    rsx! {
391        <kbd class={format!("inline-flex items-center px-1.5 py-0.5 rounded border border-neutral-700 bg-neutral-800 font-mono text-[10px] font-semibold text-neutral-300 shadow-sm select-none {}", custom)}>
392            {keys}
393        </kbd>
394    }
395}
396
397/// Visual horizontal or vertical separator divider.
398#[component]
399pub fn Separator(vertical: Option<bool>, class: Option<&'static str>) -> View {
400    let is_vert = vertical.unwrap_or(false);
401    let custom = class.unwrap_or("");
402    let dim = if is_vert {
403        "w-px h-full"
404    } else {
405        "h-px w-full"
406    };
407
408    rsx! {
409        <div class={format!("bg-neutral-800 {} {}", dim, custom)} />
410    }
411}