1#![allow(non_snake_case)]
4
5use strata_dom::{create_dropdown_portal, use_persistent_signal, AnchorPlacement, Signal, View};
6use strata_icons::prelude::{Icon, IconName};
7use strata_macro::{component, rsx};
8
9#[component]
11pub fn Input(
12 label: Option<&'static str>,
13 placeholder: Option<&'static str>,
14 value: Option<Signal<String>>,
15 prefix_icon: Option<IconName>,
16 suffix_icon: Option<IconName>,
17 helper_text: Option<&'static str>,
18 is_error: Option<bool>,
19 error_message: Option<&'static str>,
20 disabled: Option<bool>,
21 class: Option<&'static str>,
22) -> View {
23 let custom = class.unwrap_or("");
24 let err = is_error.unwrap_or(false);
25 let border_color = if err {
26 "border-rose-500 focus:border-rose-500 focus:ring-rose-500/20"
27 } else {
28 "border-neutral-800 focus:border-indigo-500 focus:ring-indigo-500/20"
29 };
30
31 let pl = if prefix_icon.is_some() {
32 "pl-10"
33 } else {
34 "pl-3.5"
35 };
36 let pr = if suffix_icon.is_some() {
37 "pr-10"
38 } else {
39 "pr-3.5"
40 };
41
42 let input_classes = format!(
43 "w-full h-10 bg-neutral-900 border rounded-lg text-sm text-white placeholder-neutral-500 focus:outline-none focus:ring-2 transition-all duration-150 disabled:opacity-50 disabled:pointer-events-none {border_color} {pl} {pr} {custom}"
44 );
45
46 let val_str = value.map(|s| s.get()).unwrap_or_default();
47
48 rsx! {
49 <div class="flex flex-col gap-1.5 w-full">
50 {if let Some(lbl) = label {
51 rsx! { <label class="text-xs font-mono uppercase tracking-wider text-neutral-400 select-none">{lbl}</label> }
52 } else {
53 rsx! { <></> }
54 }}
55 <div class="relative flex items-center w-full">
56 {if let Some(icon) = prefix_icon {
57 rsx! {
58 <div class="absolute left-3 flex items-center pointer-events-none text-neutral-500">
59 {Icon(icon, Some("w-4 h-4 text-neutral-400")).root}
60 </div>
61 }
62 } else {
63 rsx! { <></> }
64 }}
65 <input
66 type="text"
67 class={input_classes}
68 placeholder={placeholder.unwrap_or("")}
69 disabled={disabled.unwrap_or(false)}
70 value={val_str}
71 />
72 {if let Some(icon) = suffix_icon {
73 rsx! {
74 <div class="absolute right-3 flex items-center pointer-events-none text-neutral-500">
75 {Icon(icon, Some("w-4 h-4 text-neutral-400")).root}
76 </div>
77 }
78 } else {
79 rsx! { <></> }
80 }}
81 </div>
82 {if let (true, Some(err_msg)) = (err, error_message) {
83 rsx! { <span class="text-xs text-rose-400 font-medium">{err_msg}</span> }
84 } else if let Some(help) = helper_text {
85 rsx! { <span class="text-xs text-neutral-500">{help}</span> }
86 } else {
87 rsx! { <></> }
88 }}
89 </div>
90 }
91}
92
93#[component]
95pub fn PasswordInput(
96 label: Option<&'static str>,
97 placeholder: Option<&'static str>,
98 value: Option<Signal<String>>,
99 show_password: Option<Signal<bool>>,
100 disabled: Option<bool>,
101 class: Option<&'static str>,
102) -> View {
103 let show = show_password.as_ref().map(|s| s.get()).unwrap_or(false);
104 let input_type = if show { "text" } else { "password" };
105 let icon = if show {
106 IconName::EyeOff
107 } else {
108 IconName::Eye
109 };
110 let show_sig = show_password.clone();
111
112 rsx! {
113 <div class="flex flex-col gap-1.5 w-full">
114 {if let Some(lbl) = label {
115 rsx! { <label class="text-xs font-mono uppercase tracking-wider text-neutral-400 select-none">{lbl}</label> }
116 } else {
117 rsx! { <></> }
118 }}
119 <div class="relative flex items-center w-full">
120 <input
121 type={input_type}
122 class={format!("w-full h-10 bg-neutral-900 border border-neutral-800 rounded-lg pl-3.5 pr-10 text-sm text-white placeholder-neutral-500 focus:outline-none focus:border-indigo-500 transition-colors {}", class.unwrap_or(""))}
123 placeholder={placeholder.unwrap_or("••••••••")}
124 disabled={disabled.unwrap_or(false)}
125 value={value.map(|s| s.get()).unwrap_or_default()}
126 />
127 <button
128 type="button"
129 class="absolute right-3 text-neutral-400 hover:text-white cursor-pointer select-none"
130 onclick={move |_| {
131 if let Some(s) = &show_sig {
132 s.update(|v| *v = !*v);
133 }
134 }}
135 >
136 {Icon(icon, Some("w-4 h-4 text-neutral-400 hover:text-white")).root}
137 </button>
138 </div>
139 </div>
140 }
141}
142
143#[component]
145pub fn NumberInput(
146 label: Option<&'static str>,
147 value: Signal<i64>,
148 min: Option<i64>,
149 max: Option<i64>,
150 step: Option<i64>,
151 disabled: Option<bool>,
152 class: Option<&'static str>,
153) -> View {
154 let s = step.unwrap_or(1);
155 let mi = min.unwrap_or(i64::MIN);
156 let ma = max.unwrap_or(i64::MAX);
157
158 let val = value.get();
159 let val_dec = value.clone();
160 let val_inc = value.clone();
161
162 rsx! {
163 <div class="flex flex-col gap-1.5 w-full">
164 {if let Some(lbl) = label {
165 rsx! { <label class="text-xs font-mono uppercase tracking-wider text-neutral-400 select-none">{lbl}</label> }
166 } else {
167 rsx! { <></> }
168 }}
169 <div class="flex items-center rounded-lg border border-neutral-800 bg-neutral-900 overflow-hidden w-full h-10">
170 <button
171 type="button"
172 class="h-full px-3 text-neutral-400 hover:text-white hover:bg-neutral-800/60 cursor-pointer select-none transition-colors"
173 onclick={move |_| {
174 val_dec.update(|v| *v = (*v - s).max(mi));
175 }}
176 >
177 {"-"}
178 </button>
179 <div class="flex-1 text-center font-mono text-sm text-white select-none">
180 {val.to_string()}
181 </div>
182 <button
183 type="button"
184 class="h-full px-3 text-neutral-400 hover:text-white hover:bg-neutral-800/60 cursor-pointer select-none transition-colors"
185 onclick={move |_| {
186 val_inc.update(|v| *v = (*v + s).min(ma));
187 }}
188 >
189 {"+"}
190 </button>
191 </div>
192 </div>
193 }
194}
195
196#[component]
198pub fn SearchInput(
199 placeholder: Option<&'static str>,
200 value: Signal<String>,
201 class: Option<&'static str>,
202) -> View {
203 let val = value.get();
204 let has_content = !val.is_empty();
205 let val_clear = value.clone();
206
207 rsx! {
208 <div class="relative flex items-center w-full">
209 <div class="absolute left-3 flex items-center pointer-events-none text-neutral-500">
210 {Icon(IconName::Search, Some("w-4 h-4 text-neutral-400")).root}
211 </div>
212 <input
213 type="text"
214 class={format!("w-full h-10 bg-neutral-900 border border-neutral-800 rounded-lg pl-10 pr-10 text-sm text-white placeholder-neutral-500 focus:outline-none focus:border-indigo-500 transition-colors {}", class.unwrap_or(""))}
215 placeholder={placeholder.unwrap_or("Search...")}
216 value={val}
217 />
218 {if has_content {
219 rsx! {
220 <button
221 type="button"
222 class="absolute right-3 text-neutral-400 hover:text-white cursor-pointer select-none"
223 onclick={move |_| val_clear.set(String::new())}
224 >
225 {Icon(IconName::X, Some("w-4 h-4 text-neutral-400 hover:text-white")).root}
226 </button>
227 }
228 } else {
229 rsx! { <></> }
230 }}
231 </div>
232 }
233}
234
235#[component]
237pub fn Textarea(
238 label: Option<&'static str>,
239 placeholder: Option<&'static str>,
240 rows: Option<usize>,
241 value: Option<Signal<String>>,
242 disabled: Option<bool>,
243 class: Option<&'static str>,
244) -> View {
245 let r = rows.unwrap_or(4);
246 let custom = class.unwrap_or("");
247
248 rsx! {
249 <div class="flex flex-col gap-1.5 w-full">
250 {if let Some(lbl) = label {
251 rsx! { <label class="text-xs font-mono uppercase tracking-wider text-neutral-400 select-none">{lbl}</label> }
252 } else {
253 rsx! { <></> }
254 }}
255 <textarea
256 rows={r.to_string()}
257 class={format!("w-full p-3.5 bg-neutral-900 border border-neutral-800 rounded-lg text-sm text-white placeholder-neutral-500 focus:outline-none focus:border-indigo-500 transition-colors resize-y {}", custom)}
258 placeholder={placeholder.unwrap_or("")}
259 disabled={disabled.unwrap_or(false)}
260 >
261 {value.map(|s| s.get()).unwrap_or_default()}
262 </textarea>
263 </div>
264 }
265}
266
267#[component]
269pub fn Checkbox(
270 label: Option<&'static str>,
271 checked: Signal<bool>,
272 disabled: Option<bool>,
273 class: Option<&'static str>,
274) -> View {
275 let is_checked = checked.get();
276 let is_dis = disabled.unwrap_or(false);
277 let chk = checked.clone();
278
279 let (bg, border) = if is_checked {
280 ("bg-indigo-600", "border-indigo-600")
281 } else {
282 (
283 "bg-neutral-900",
284 "border-neutral-700 hover:border-neutral-500",
285 )
286 };
287
288 rsx! {
289 <label class={format!("inline-flex items-center gap-2.5 cursor-pointer select-none group {}", if is_dis { "opacity-50 pointer-events-none" } else { "" })}>
290 <div
291 class={format!("w-5 h-5 rounded-md border flex items-center justify-center transition-all duration-150 {} {}", bg, border)}
292 onclick={move |_| {
293 if !is_dis {
294 chk.update(|c| *c = !*c);
295 }
296 }}
297 >
298 {if is_checked {
299 Icon(IconName::Check, Some("w-3.5 h-3.5 text-white stroke-[2.5]"))
300 } else {
301 rsx! { <></> }
302 }}
303 </div>
304 {if let Some(lbl) = label {
305 rsx! { <span class="text-sm text-neutral-300 group-hover:text-white transition-colors">{lbl}</span> }
306 } else {
307 rsx! { <></> }
308 }}
309 </label>
310 }
311}
312
313#[component]
315pub fn Switch(
316 label: Option<&'static str>,
317 description: Option<&'static str>,
318 checked: Signal<bool>,
319 disabled: Option<bool>,
320) -> View {
321 let is_active = checked.get();
322 let is_dis = disabled.unwrap_or(false);
323 let chk = checked.clone();
324
325 let bg_color = if is_active {
326 "bg-indigo-600"
327 } else {
328 "bg-neutral-800"
329 };
330 let knob_pos = if is_active {
331 "translate-x-5"
332 } else {
333 "translate-x-0"
334 };
335
336 rsx! {
337 <div class={format!("flex items-center justify-between w-full py-1.5 {}", if is_dis { "opacity-50 pointer-events-none" } else { "" })}>
338 <div class="flex flex-col">
339 {if let Some(lbl) = label {
340 rsx! { <span class="text-sm font-medium text-white select-none">{lbl}</span> }
341 } else {
342 rsx! { <></> }
343 }}
344 {if let Some(desc) = description {
345 rsx! { <span class="text-xs text-neutral-400 select-none">{desc}</span> }
346 } else {
347 rsx! { <></> }
348 }}
349 </div>
350 <button
351 type="button"
352 role="switch"
353 aria-checked={if is_active { "true" } else { "false" }}
354 class={format!("w-11 h-6 rounded-full transition-colors duration-200 relative p-0.5 cursor-pointer focus:outline-none {}", bg_color)}
355 onclick={move |_| {
356 if !is_dis {
357 chk.update(|c| *c = !*c);
358 }
359 }}
360 >
361 <div class={format!("w-5 h-5 rounded-full bg-white shadow-md transition-transform duration-200 {}", knob_pos)} />
362 </button>
363 </div>
364 }
365}
366
367#[derive(Debug, Clone, PartialEq, Eq)]
369pub struct SelectOption {
370 pub value: String,
371 pub label: String,
372}
373
374impl SelectOption {
375 pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
376 Self {
377 value: value.into(),
378 label: label.into(),
379 }
380 }
381}
382
383impl From<&str> for SelectOption {
384 fn from(text: &str) -> Self {
385 Self::new(text, text)
386 }
387}
388
389impl From<String> for SelectOption {
390 fn from(text: String) -> Self {
391 Self::new(text.clone(), text)
392 }
393}
394
395pub fn select_step_index(len: usize, current: usize, delta: isize) -> usize {
397 if len == 0 {
398 return 0;
399 }
400 (current as isize + delta).clamp(0, len as isize - 1) as usize
401}
402
403#[component]
414pub fn Select(
415 id: Option<&'static str>,
416 label: Option<&'static str>,
417 options: Vec<SelectOption>,
418 value: Option<Signal<String>>,
419 on_change: Option<std::rc::Rc<dyn Fn(&str)>>,
420 disabled: Option<bool>,
421 class: Option<&'static str>,
422) -> View {
423 let custom = class.unwrap_or("");
424 let is_dis = disabled.unwrap_or(false);
425 let key = match id {
426 Some(id) => format!("select:{id}"),
427 None => format!(
428 "select:{}:{}",
429 label.unwrap_or(""),
430 options
431 .iter()
432 .map(|o| o.value.as_str())
433 .collect::<Vec<_>>()
434 .join("|")
435 ),
436 };
437 let trigger_id = format!("{key}-trigger");
438
439 let open = use_persistent_signal(&format!("{key}:open"), || false);
440 let first_value = options.first().map(|o| o.value.clone()).unwrap_or_default();
441 let value_sig =
442 value.unwrap_or_else(|| use_persistent_signal(&format!("{key}:value"), || first_value));
443 let current_value = value_sig.get();
444 let selected_idx = options.iter().position(|o| o.value == current_value);
445 let active = use_persistent_signal(&format!("{key}:active"), || 0usize);
446 let is_open = open.get() && !is_dis;
447 let active_idx = active.get().min(options.len().saturating_sub(1));
448
449 let selected_label = selected_idx
450 .map(|i| options[i].label.clone())
451 .unwrap_or_default();
452
453 let option_values: Vec<String> = options.iter().map(|o| o.value.clone()).collect();
454 let pick = {
455 let value_sig = value_sig.clone();
456 let open = open.clone();
457 let on_change = on_change.clone();
458 move |idx: usize| {
459 if let Some(v) = option_values.get(idx) {
460 value_sig.set(v.clone());
461 open.set(false);
462 if let Some(cb) = on_change.as_ref() {
463 cb(v);
464 }
465 }
466 }
467 };
468
469 let on_keydown = {
470 let open = open.clone();
471 let active = active.clone();
472 let pick = pick.clone();
473 let len = options.len();
474 move |e: strata_dom::Event| {
475 if is_dis {
476 return;
477 }
478 let is_open_now = open.get();
479 match e.key.as_str() {
480 "ArrowDown" | "ArrowUp" => {
481 let delta = if e.key == "ArrowDown" { 1 } else { -1 };
482 if is_open_now {
483 active.set(select_step_index(len, active.get(), delta));
484 } else {
485 open.set(true);
486 }
487 e.prevent_default();
488 }
489 "Home" if is_open_now => {
490 active.set(0);
491 e.prevent_default();
492 }
493 "End" if is_open_now => {
494 active.set(len.saturating_sub(1));
495 e.prevent_default();
496 }
497 "Enter" | " " => {
498 if is_open_now {
499 pick(active.get());
500 } else {
501 open.set(true);
502 }
503 e.prevent_default();
504 }
505 "Escape" if is_open_now => {
506 open.set(false);
507 e.prevent_default();
508 }
509 _ => {}
510 }
511 }
512 };
513
514 let base = "w-full h-10 flex items-center justify-between bg-neutral-900 border border-neutral-800 rounded-lg px-3.5 text-sm text-white focus:outline-none focus:border-indigo-500 transition-colors cursor-pointer";
515 let combined = format!("{base} {custom}");
516
517 let menu = if is_open {
518 let items: Vec<_> = options
519 .iter()
520 .enumerate()
521 .map(|(i, opt)| {
522 let chosen = Some(i) == selected_idx;
523 let highlighted = i == active_idx;
524 let pick = pick.clone();
525 let row_class = format!(
526 "px-3.5 py-2 text-sm cursor-pointer {}",
527 if highlighted {
528 "bg-neutral-800 text-white"
529 } else {
530 "text-neutral-300"
531 }
532 );
533 rsx! {
534 <div
535 role="option"
536 aria-selected={if chosen { "true" } else { "false" }}
537 class={row_class}
538 onclick={move |_| pick(i)}
539 >
540 {opt.label.clone()}
541 </div>
542 }
543 })
544 .collect();
545 let close = open.clone();
546 Some(create_dropdown_portal(
547 trigger_id.clone(),
548 AnchorPlacement::BottomStart,
549 4.0,
550 move |_| close.set(false),
551 rsx! {
552 <div
553 role="listbox"
554 class="min-w-40 max-h-64 overflow-y-auto rounded-lg bg-neutral-900 border border-neutral-800 shadow-xl py-1"
555 >
556 {items}
557 </div>
558 },
559 ))
560 } else {
561 None
562 };
563
564 rsx! {
565 <div class="flex flex-col gap-1.5 w-full">
566 {if let Some(lbl) = label {
567 rsx! { <label class="text-xs font-mono uppercase tracking-wider text-neutral-400 select-none">{lbl}</label> }
568 } else {
569 rsx! { <></> }
570 }}
571 <button
572 type="button"
573 id={trigger_id.clone()}
574 role="combobox"
575 aria-haspopup="listbox"
576 aria-expanded={if is_open { "true" } else { "false" }}
577 disabled={is_dis}
578 class={combined}
579 onclick={{
580 let open = open.clone();
581 move |_| {
582 if !is_dis {
583 open.update(|o| *o = !*o);
584 }
585 }
586 }}
587 onkeydown={on_keydown}
588 >
589 <span>{selected_label}</span>
590 {Icon(IconName::ChevronDown, Some("w-4 h-4 text-neutral-400")).root}
591 </button>
592 {if let Some(menu) = menu { menu } else { rsx! { <></> } }}
593 </div>
594 }
595}
596
597#[component]
599pub fn Slider(
600 min: f32,
601 max: f32,
602 value: Signal<f32>,
603 disabled: Option<bool>,
604 class: Option<&'static str>,
605) -> View {
606 let val = value.get();
607 let pct = ((val - min) / (max - min)).clamp(0.0, 1.0) * 100.0;
608 let custom = class.unwrap_or("");
609
610 rsx! {
611 <div class={format!("relative flex items-center w-full py-2 {}", custom)}>
612 <div class="w-full h-2 rounded-full bg-neutral-800 overflow-hidden">
613 <div
614 class="h-full bg-indigo-600 rounded-full transition-all duration-75"
615 style={format!("width: {pct:.1}%;")}
616 />
617 </div>
618 <div
619 class="absolute w-4 h-4 rounded-full bg-white shadow-md border-2 border-indigo-600 cursor-pointer -ml-2 top-1"
620 style={format!("left: {pct:.1}%;")}
621 />
622 </div>
623 }
624}
625
626#[component]
628pub fn PinInput(length: usize, value: Signal<String>, disabled: Option<bool>) -> View {
629 let len = length.max(1);
630 let val = value.get();
631
632 rsx! {
633 <div class="flex items-center gap-2">
634 {(0..len).map(|i| {
635 let char_at = val.chars().nth(i).map(|c| c.to_string()).unwrap_or_default();
636 rsx! {
637 <div class="w-12 h-14 rounded-xl border border-neutral-800 bg-neutral-900 flex items-center justify-center text-xl font-mono font-bold text-white shadow-inner">
638 {char_at}
639 </div>
640 }
641 })}
642 </div>
643 }
644}