1#![allow(non_snake_case)]
4
5use std::rc::Rc;
6use strata_dom::{Attribute, EventType, NodeBlueprint, Tag, View};
7use strata_icons::prelude::{Icon, IconName};
8use strata_macro::{component, rsx};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
12pub enum ButtonVariant {
13 #[default]
14 Primary,
15 Secondary,
16 Outline,
17 Ghost,
18 Destructive,
19 Subtle,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub enum ButtonSize {
25 Xs,
26 Sm,
27 #[default]
28 Md,
29 Lg,
30 Xl,
31}
32
33#[component]
35pub fn Button(
36 variant: Option<ButtonVariant>,
37 size: Option<ButtonSize>,
38 loading: Option<bool>,
39 disabled: Option<bool>,
40 class: Option<&'static str>,
41 onclick: Option<Rc<dyn Fn()>>,
42 prefix_icon: Option<IconName>,
43 suffix_icon: Option<IconName>,
44 children: Option<Vec<NodeBlueprint>>,
45) -> View {
46 let var = variant.unwrap_or_default();
47 let sz = size.unwrap_or_default();
48 let is_disabled = disabled.unwrap_or(false) || loading.unwrap_or(false);
49
50 let base = "inline-flex items-center justify-center font-medium rounded-lg transition-all duration-150 active:scale-95 disabled:opacity-50 disabled:pointer-events-none select-none cursor-pointer focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500";
51
52 let var_classes = match var {
53 ButtonVariant::Primary => "bg-indigo-600 hover:bg-indigo-500 text-white shadow-md shadow-indigo-600/20 border border-transparent",
54 ButtonVariant::Secondary => "bg-neutral-800 hover:bg-neutral-700 text-neutral-100 border border-neutral-700",
55 ButtonVariant::Outline => "border border-neutral-700 hover:border-neutral-500 text-neutral-200 hover:text-white bg-transparent hover:bg-neutral-800/40",
56 ButtonVariant::Ghost => "text-neutral-400 hover:text-white hover:bg-neutral-800/60 border border-transparent",
57 ButtonVariant::Destructive => "bg-rose-600 hover:bg-rose-500 text-white shadow-md shadow-rose-600/20 border border-transparent",
58 ButtonVariant::Subtle => "bg-indigo-500/10 hover:bg-indigo-500/20 text-indigo-400 border border-indigo-500/20",
59 };
60
61 let sz_classes = match sz {
62 ButtonSize::Xs => "h-7 px-2.5 text-xs gap-1",
63 ButtonSize::Sm => "h-8 px-3 text-xs gap-1.5",
64 ButtonSize::Md => "h-10 px-4 text-sm gap-2",
65 ButtonSize::Lg => "h-12 px-6 text-base gap-2.5",
66 ButtonSize::Xl => "h-14 px-8 text-lg gap-3",
67 };
68
69 let custom = class.unwrap_or("");
70 let combined = format!("{base} {var_classes} {sz_classes} {custom}");
71 let classes: Vec<String> = combined.split_whitespace().map(|s| s.to_string()).collect();
72
73 let mut attrs = Vec::new();
74 attrs.push(Attribute::property("role", "button"));
75 if is_disabled {
76 attrs.push(Attribute::property("disabled", "true"));
77 attrs.push(Attribute::property("aria-disabled", "true"));
78 }
79 if let Some(cb) = onclick {
80 if !is_disabled {
81 attrs.push(Attribute::event_listener(EventType::Click, move || cb()));
82 }
83 }
84
85 let mut btn_children = Vec::new();
86 if loading.unwrap_or(false) {
87 btn_children.push(
88 rsx! {
89 <span class="inline-block animate-spin mr-1.5 text-current">
90 {Icon(IconName::Loader2, Some("w-4 h-4 animate-spin")).root}
91 </span>
92 }
93 .root,
94 );
95 } else if let Some(icon) = prefix_icon {
96 btn_children.push(Icon(icon, Some("w-4 h-4 text-current")).root);
97 }
98
99 if let Some(ch) = children {
100 btn_children.extend(ch);
101 }
102
103 if let Some(icon) = suffix_icon {
104 if !loading.unwrap_or(false) {
105 btn_children.push(Icon(icon, Some("w-4 h-4 text-current")).root);
106 }
107 }
108
109 View::new(NodeBlueprint::element(
110 Tag::Button,
111 classes,
112 attrs,
113 btn_children,
114 ))
115}
116
117#[component]
119pub fn IconButton(
120 icon: IconName,
121 variant: Option<ButtonVariant>,
122 size: Option<ButtonSize>,
123 rounded: Option<bool>,
124 tooltip: Option<&'static str>,
125 disabled: Option<bool>,
126 class: Option<&'static str>,
127 onclick: Option<Rc<dyn Fn()>>,
128) -> View {
129 let var = variant.unwrap_or(ButtonVariant::Ghost);
130 let sz = size.unwrap_or(ButtonSize::Md);
131 let is_rounded = rounded.unwrap_or(false);
132
133 let shape = if is_rounded {
134 "rounded-full"
135 } else {
136 "rounded-lg"
137 };
138 let dim = match sz {
139 ButtonSize::Xs => "w-7 h-7 p-1",
140 ButtonSize::Sm => "w-8 h-8 p-1.5",
141 ButtonSize::Md => "w-10 h-10 p-2",
142 ButtonSize::Lg => "w-12 h-12 p-2.5",
143 ButtonSize::Xl => "w-14 h-14 p-3",
144 };
145
146 let title_attr = tooltip.map(|t| Attribute::property("title", t));
147
148 let mut btn = Button(
149 Some(var),
150 Some(sz),
151 None,
152 disabled,
153 Some("p-0"),
154 onclick,
155 None,
156 None,
157 Some(vec![Icon(icon, Some("w-full h-full text-current")).root]),
158 );
159
160 if let NodeBlueprint::Element {
161 ref mut classes,
162 ref mut attributes,
163 ..
164 } = btn.root
165 {
166 classes.push(shape.to_string());
167 classes.push(dim.to_string());
168 if let Some(custom) = class {
169 classes.extend(custom.split_whitespace().map(|s| s.to_string()));
170 }
171 if let Some(t) = title_attr {
172 attributes.push(t);
173 }
174 }
175
176 btn
177}
178
179#[component]
181pub fn ButtonGroup(children: Vec<NodeBlueprint>, class: Option<&'static str>) -> View {
182 let custom = class.unwrap_or("");
183 let base = "inline-flex items-center rounded-lg shadow-sm [&>button:not(:first-child)]:-ml-px [&>button:not(:first-child)]:rounded-l-none [&>button:not(:last-child)]:rounded-r-none";
184 let combined = format!("{base} {custom}");
185 let classes: Vec<String> = combined.split_whitespace().map(|s| s.to_string()).collect();
186
187 View::new(NodeBlueprint::element(
188 Tag::Div,
189 classes,
190 vec![Attribute::property("role", "group")],
191 children,
192 ))
193}
194
195#[component]
197pub fn SplitButton(
198 label: &'static str,
199 on_action: Rc<dyn Fn()>,
200 on_menu: Rc<dyn Fn()>,
201 variant: Option<ButtonVariant>,
202 size: Option<ButtonSize>,
203 class: Option<&'static str>,
204) -> View {
205 let var = variant.unwrap_or(ButtonVariant::Primary);
206 let sz = size.unwrap_or(ButtonSize::Md);
207
208 rsx! {
209 <div class={format!("inline-flex rounded-lg shadow-sm {}", class.unwrap_or(""))}>
210 <Button
211 variant={Some(var)}
212 size={Some(sz)}
213 onclick={on_action}
214 class={Some("rounded-r-none")}
215 >
216 {label}
217 </Button>
218 <Button
219 variant={Some(var)}
220 size={Some(sz)}
221 onclick={on_menu}
222 class={Some("rounded-l-none -ml-px px-2")}
223 prefix_icon={Some(IconName::ChevronDown)}
224 />
225 </div>
226 }
227}