Skip to main content

strata_ui/
composite.rs

1//! High-Level Composite Application Organisms for Strata UI.
2
3#![allow(non_snake_case)]
4
5use std::rc::Rc;
6use strata_dom::{NodeBlueprint, View};
7use strata_icons::prelude::{Icon, IconName};
8use strata_macro::{component, rsx};
9
10/// Form field container bundling labels, required indicators, and error validation states.
11#[component]
12pub fn FormField(
13    label: Option<&'static str>,
14    required: Option<bool>,
15    helper_text: Option<&'static str>,
16    error_message: Option<&'static str>,
17    children: Option<Vec<NodeBlueprint>>,
18    class: Option<&'static str>,
19) -> View {
20    let req = required.unwrap_or(false);
21    let custom = class.unwrap_or("");
22
23    rsx! {
24        <div class={format!("flex flex-col gap-1.5 w-full {}", custom)}>
25            {if let Some(lbl) = label {
26                rsx! {
27                    <label class="text-xs font-mono uppercase tracking-wider text-neutral-400 select-none flex items-center gap-1">
28                        <span>{lbl}</span>
29                        {if req {
30                            rsx! { <span class="text-rose-400 font-bold">{"*"}</span> }
31                        } else {
32                            rsx! { <></> }
33                        }}
34                    </label>
35                }
36            } else {
37                rsx! { <></> }
38            }}
39            {children.unwrap_or_default()}
40            {if let Some(err) = error_message {
41                rsx! { <span class="text-xs text-rose-400 font-medium">{err}</span> }
42            } else if let Some(help) = helper_text {
43                rsx! { <span class="text-xs text-neutral-500">{help}</span> }
44            } else {
45                rsx! { <></> }
46            }}
47        </div>
48    }
49}
50
51/// Drag-and-drop file upload target zone.
52#[component]
53pub fn MediaDropzone(
54    title: Option<&'static str>,
55    description: Option<&'static str>,
56    accept: Option<&'static str>,
57    class: Option<&'static str>,
58) -> View {
59    let custom = class.unwrap_or("");
60    let t = title.unwrap_or("Upload files");
61    let d = description.unwrap_or("Drag and drop files here, or click to browse");
62
63    rsx! {
64        <div class={format!("flex flex-col items-center justify-center p-8 rounded-2xl border-2 border-dashed border-neutral-800 hover:border-indigo-500/60 bg-neutral-900/30 hover:bg-neutral-900/60 transition-all duration-200 cursor-pointer text-center group {}", custom)}>
65            <div class="p-3.5 rounded-2xl bg-neutral-800/80 group-hover:bg-indigo-600/20 text-neutral-400 group-hover:text-indigo-400 transition-colors mb-3">
66                {Icon(IconName::Upload, Some("w-6 h-6 text-current")).root}
67            </div>
68            <h4 class="text-sm font-semibold text-white group-hover:text-indigo-300 transition-colors">{t}</h4>
69            <p class="text-xs text-neutral-400 mt-1 max-w-xs">{d}</p>
70            {if let Some(acc) = accept {
71                rsx! {
72                    <span class="mt-3 px-2 py-0.5 rounded-full border border-neutral-800 bg-neutral-900 text-[10px] font-mono text-neutral-500">
73                        {acc}
74                    </span>
75                }
76            } else {
77                rsx! { <></> }
78            }}
79        </div>
80    }
81}
82
83/// Column definition for DataTable composite.
84#[derive(Clone)]
85pub struct TableColumnDef<T> {
86    pub key: &'static str,
87    pub title: &'static str,
88    pub accessor: Rc<dyn Fn(&T) -> String>,
89}
90
91impl<T> TableColumnDef<T> {
92    pub fn new(
93        key: &'static str,
94        title: &'static str,
95        accessor: impl Fn(&T) -> String + 'static,
96    ) -> Self {
97        Self {
98            key,
99            title,
100            accessor: Rc::new(accessor),
101        }
102    }
103}
104
105/// Reusable generic DataTable component.
106pub fn DataTable<T: 'static>(
107    columns: Vec<TableColumnDef<T>>,
108    data: &[T],
109    class: Option<&'static str>,
110) -> View {
111    let custom = class.unwrap_or("");
112
113    rsx! {
114        <div class="w-full overflow-x-auto rounded-xl border border-neutral-800">
115            <table class={format!("w-full text-left text-sm text-neutral-300 {}", custom)}>
116                <thead class="bg-neutral-900/80 border-b border-neutral-800 text-xs font-mono uppercase tracking-wider text-neutral-400">
117                    <tr>
118                        {columns.iter().map(|col| rsx! {
119                            <th class="px-4 py-3 font-semibold text-neutral-300">
120                                {col.title}
121                            </th>
122                        })}
123                    </tr>
124                </thead>
125                <tbody class="divide-y divide-neutral-800/60 bg-neutral-950/40">
126                    {data.iter().map(|row_item| rsx! {
127                        <tr class="hover:bg-neutral-800/30 transition-colors">
128                            {columns.iter().map(|col| {
129                                let cell_val = (col.accessor)(row_item);
130                                rsx! {
131                                    <td class="px-4 py-3 text-neutral-300">
132                                        {cell_val}
133                                    </td>
134                                }
135                            })}
136                        </tr>
137                    })}
138                </tbody>
139            </table>
140        </div>
141    }
142}