maud-ui 0.2.1

64 headless, accessible UI components for Rust web apps — shadcn Base UI API parity. Plus block templates, a live theme customiser, and shell hooks for 15 third-party widgets (Monaco, xyflow, Excalidraw, Three.js, AG Grid, Leaflet, FullCalendar, SortableJS, and more). Built on maud + htmx, styled like shadcn/ui.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Combobox component — maud-ui
//! Extends the Select pattern with text filtering.
use maud::{html, Markup};

#[derive(Clone, Debug)]
pub struct ComboboxOption {
    pub value: String,
    pub label: String,
}

#[derive(Clone, Debug, Default)]
pub struct ComboboxGroup {
    pub label: String,
    pub options: Vec<ComboboxOption>,
}

#[derive(Clone, Debug)]
pub struct Props {
    pub id: String,
    pub name: String,
    pub options: Vec<ComboboxOption>,
    pub selected: Option<String>,
    pub placeholder: String,
    pub search_placeholder: String,
    pub empty_text: String,
    pub disabled: bool,
    /// Multi-select mode. When true, selected values render as chips in the trigger.
    /// TODO: full JS chip interaction is stubbed — struct + rendered attr only.
    pub multiple: bool,
    /// When true, the first filtered option gets `aria-selected="true"` and
    /// `data-highlighted` so keyboard Enter can commit it immediately.
    pub auto_highlight: bool,
    /// Render a small X button inside the trigger to clear the selection.
    pub show_clear: bool,
    /// Sets `aria-invalid="true"` on the trigger for form-validation styling.
    pub aria_invalid: bool,
    /// Grouped options. When non-empty, takes precedence over `options` and
    /// renders group labels as `.mui-combobox__group-label`.
    pub groups: Vec<ComboboxGroup>,
    /// Selected values for multi-select mode (rendered as chips).
    pub selected_values: Vec<String>,
}

impl Default for Props {
    fn default() -> Self {
        Self {
            id: "combobox".to_string(),
            name: "combobox".to_string(),
            options: vec![],
            selected: None,
            placeholder: "Select\u{2026}".to_string(),
            search_placeholder: "Search\u{2026}".to_string(),
            empty_text: "No results found.".to_string(),
            disabled: false,
            multiple: false,
            auto_highlight: false,
            show_clear: false,
            aria_invalid: false,
            groups: vec![],
            selected_values: vec![],
        }
    }
}

fn lookup_label<'a>(opts: &'a [ComboboxOption], value: &str) -> Option<&'a str> {
    opts.iter()
        .find(|o| o.value == value)
        .map(|o| o.label.as_str())
}

fn flatten_options(props: &Props) -> Vec<ComboboxOption> {
    if !props.groups.is_empty() {
        let mut out = Vec::new();
        for g in &props.groups {
            for o in &g.options {
                out.push(o.clone());
            }
        }
        out
    } else {
        props.options.clone()
    }
}

fn render_option(opt: &ComboboxOption, selected: bool, highlighted: bool) -> Markup {
    let mut class = String::from("mui-combobox__option");
    if selected {
        class.push_str(" mui-combobox__option--selected");
    }
    if highlighted {
        class.push_str(" mui-combobox__option--highlighted");
    }
    html! {
        @if highlighted {
            div class=(class)
                role="option"
                data-value=(opt.value.clone())
                data-highlighted
                aria-selected=(selected.to_string()) {
                span class="mui-combobox__check" aria-hidden="true" { "\u{2713}" }
                span class="mui-combobox__option-label" { (opt.label.clone()) }
            }
        } @else {
            div class=(class)
                role="option"
                data-value=(opt.value.clone())
                aria-selected=(selected.to_string()) {
                span class="mui-combobox__check" aria-hidden="true" { "\u{2713}" }
                span class="mui-combobox__option-label" { (opt.label.clone()) }
            }
        }
    }
}

pub fn render(props: Props) -> Markup {
    let all_options = flatten_options(&props);

    let selected_label = props
        .selected
        .as_ref()
        .and_then(|sel| lookup_label(&all_options, sel).map(|s| s.to_string()))
        .unwrap_or_else(|| props.placeholder.clone());

    let hidden_value = props.selected.clone().unwrap_or_default();

    // Determine the first-option value to auto-highlight.
    let auto_highlight_value: Option<String> = if props.auto_highlight {
        if !props.groups.is_empty() {
            props
                .groups
                .iter()
                .flat_map(|g| g.options.iter())
                .next()
                .map(|o| o.value.clone())
        } else {
            props.options.first().map(|o| o.value.clone())
        }
    } else {
        None
    };

    let is_selected = |v: &str| -> bool {
        if props.multiple {
            props.selected_values.iter().any(|s| s == v)
        } else {
            props.selected.as_deref() == Some(v)
        }
    };

    html! {
        div class="mui-combobox"
            data-mui="combobox"
            data-multiple[props.multiple]
            data-auto-highlight[props.auto_highlight] {
            @if props.disabled {
                button type="button" class="mui-combobox__trigger"
                        role="combobox" aria-expanded="false"
                        aria-haspopup="listbox"
                        aria-controls=(format!("{}-dropdown", props.id))
                        aria-label=(selected_label.clone())
                        aria-invalid=[props.aria_invalid.then_some("true")]
                        id=(props.id.clone())
                        disabled {
                    @if props.multiple {
                        span class="mui-combobox__chips" {
                            @for v in &props.selected_values {
                                span class="mui-combobox__chip" data-value=(v.clone()) {
                                    span class="mui-combobox__chip-label" {
                                        (lookup_label(&all_options, v).unwrap_or(v.as_str()))
                                    }
                                    span class="mui-combobox__chip-remove" aria-label="Remove" { "\u{00d7}" }
                                }
                            }
                            @if props.selected_values.is_empty() {
                                span class="mui-combobox__value" { (selected_label.clone()) }
                            }
                        }
                    } @else {
                        span class="mui-combobox__value" { (selected_label) }
                    }
                    @if props.show_clear && props.selected.is_some() {
                        span class="mui-combobox__clear" aria-label="Clear" aria-hidden="true" { "\u{00d7}" }
                    }
                    span class="mui-combobox__chevron" aria-hidden="true" { "\u{25be}" }
                }
            } @else {
                button type="button" class="mui-combobox__trigger"
                        role="combobox" aria-expanded="false"
                        aria-haspopup="listbox"
                        aria-controls=(format!("{}-dropdown", props.id))
                        aria-label=(selected_label.clone())
                        aria-invalid=[props.aria_invalid.then_some("true")]
                        id=(props.id.clone()) {
                    @if props.multiple {
                        span class="mui-combobox__chips" {
                            @for v in &props.selected_values {
                                span class="mui-combobox__chip" data-value=(v.clone()) {
                                    span class="mui-combobox__chip-label" {
                                        (lookup_label(&all_options, v).unwrap_or(v.as_str()))
                                    }
                                    button type="button" class="mui-combobox__chip-remove"
                                            aria-label="Remove"
                                            data-value=(v.clone()) { "\u{00d7}" }
                                }
                            }
                            @if props.selected_values.is_empty() {
                                span class="mui-combobox__value" { (selected_label.clone()) }
                            }
                        }
                    } @else {
                        span class="mui-combobox__value" { (selected_label) }
                    }
                    @if props.show_clear && props.selected.is_some() {
                        button type="button" class="mui-combobox__clear" aria-label="Clear" { "\u{00d7}" }
                    }
                    span class="mui-combobox__chevron" aria-hidden="true" { "\u{25be}" }
                }
            }

            div class="mui-combobox__dropdown" id=(format!("{}-dropdown", props.id)) hidden {
                div class="mui-combobox__search-wrap" {
                    span class="mui-combobox__search-icon" aria-hidden="true" { "\u{1f50d}" }
                    input type="text" class="mui-combobox__search"
                          placeholder=(props.search_placeholder)
                          aria-label="Search options"
                          autocomplete="off";
                }
                div class="mui-combobox__list" role="listbox" {
                    @if !props.groups.is_empty() {
                        @for group in &props.groups {
                            div class="mui-combobox__group" role="group" aria-label=(group.label.clone()) {
                                div class="mui-combobox__group-label" { (group.label.clone()) }
                                @for opt in &group.options {
                                    (render_option(
                                        opt,
                                        is_selected(&opt.value),
                                        auto_highlight_value.as_deref() == Some(opt.value.as_str()),
                                    ))
                                }
                            }
                        }
                    } @else {
                        @for opt in &props.options {
                            (render_option(
                                opt,
                                is_selected(&opt.value),
                                auto_highlight_value.as_deref() == Some(opt.value.as_str()),
                            ))
                        }
                    }
                }
                div class="mui-combobox__empty" hidden { (props.empty_text) }
            }

            @if props.multiple {
                @for v in &props.selected_values {
                    input type="hidden" name=(format!("{}[]", props.name)) value=(v.clone()) class="mui-combobox__hidden";
                }
            } @else {
                input type="hidden" name=(props.name.clone()) value=(hidden_value) class="mui-combobox__hidden";
            }
        }
    }
}

pub fn showcase() -> Markup {
    let frameworks = vec![
        ComboboxOption {
            value: "react".to_string(),
            label: "React".to_string(),
        },
        ComboboxOption {
            value: "vue".to_string(),
            label: "Vue".to_string(),
        },
        ComboboxOption {
            value: "angular".to_string(),
            label: "Angular".to_string(),
        },
        ComboboxOption {
            value: "svelte".to_string(),
            label: "Svelte".to_string(),
        },
        ComboboxOption {
            value: "nextjs".to_string(),
            label: "Next.js".to_string(),
        },
        ComboboxOption {
            value: "nuxt".to_string(),
            label: "Nuxt".to_string(),
        },
        ComboboxOption {
            value: "sveltekit".to_string(),
            label: "SvelteKit".to_string(),
        },
        ComboboxOption {
            value: "remix".to_string(),
            label: "Remix".to_string(),
        },
        ComboboxOption {
            value: "astro".to_string(),
            label: "Astro".to_string(),
        },
        ComboboxOption {
            value: "solid".to_string(),
            label: "Solid".to_string(),
        },
    ];

    let team_members = vec![
        ComboboxOption {
            value: "alice".to_string(),
            label: "Alice Johnson".to_string(),
        },
        ComboboxOption {
            value: "bob".to_string(),
            label: "Bob Smith".to_string(),
        },
        ComboboxOption {
            value: "carol".to_string(),
            label: "Carol Williams".to_string(),
        },
        ComboboxOption {
            value: "david".to_string(),
            label: "David Brown".to_string(),
        },
        ComboboxOption {
            value: "elena".to_string(),
            label: "Elena Garcia".to_string(),
        },
        ComboboxOption {
            value: "frank".to_string(),
            label: "Frank Miller".to_string(),
        },
        ComboboxOption {
            value: "grace".to_string(),
            label: "Grace Lee".to_string(),
        },
        ComboboxOption {
            value: "henry".to_string(),
            label: "Henry Chen".to_string(),
        },
    ];

    let grouped = vec![
        ComboboxGroup {
            label: "Frontend".to_string(),
            options: vec![
                ComboboxOption {
                    value: "react".to_string(),
                    label: "React".to_string(),
                },
                ComboboxOption {
                    value: "vue".to_string(),
                    label: "Vue".to_string(),
                },
                ComboboxOption {
                    value: "svelte".to_string(),
                    label: "Svelte".to_string(),
                },
            ],
        },
        ComboboxGroup {
            label: "Backend".to_string(),
            options: vec![
                ComboboxOption {
                    value: "rust".to_string(),
                    label: "Rust".to_string(),
                },
                ComboboxOption {
                    value: "go".to_string(),
                    label: "Go".to_string(),
                },
                ComboboxOption {
                    value: "python".to_string(),
                    label: "Python".to_string(),
                },
            ],
        },
    ];

    html! {
        div.mui-showcase__grid {
            // Primary demo: framework picker
            section {
                h2 { "Framework picker" }
                p.mui-showcase__caption { "Search and select from a list of frontend frameworks." }
                div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(14rem,1fr));gap:1.5rem;" {
                    div {
                        h3 style="font-size:0.875rem;margin-bottom:0.5rem;" { "Pre-selected" }
                        (render(Props {
                            id: "fw-1".to_string(),
                            name: "framework-1".to_string(),
                            options: frameworks.clone(),
                            selected: Some("vue".to_string()),
                            placeholder: "Select framework\u{2026}".to_string(),
                            search_placeholder: "Search\u{2026}".to_string(),
                            empty_text: "No framework found.".to_string(),
                            disabled: false,
                            ..Default::default()
                        }))
                    }
                    div {
                        h3 style="font-size:0.875rem;margin-bottom:0.5rem;" { "Placeholder" }
                        (render(Props {
                            id: "fw-2".to_string(),
                            name: "framework-2".to_string(),
                            options: frameworks.clone(),
                            selected: None,
                            placeholder: "Select framework\u{2026}".to_string(),
                            search_placeholder: "Search frameworks\u{2026}".to_string(),
                            empty_text: "No framework found.".to_string(),
                            disabled: false,
                            ..Default::default()
                        }))
                    }
                    div {
                        h3 style="font-size:0.875rem;margin-bottom:0.5rem;" { "Disabled" }
                        (render(Props {
                            id: "fw-3".to_string(),
                            name: "framework-3".to_string(),
                            options: frameworks.clone(),
                            selected: Some("react".to_string()),
                            placeholder: "Select framework\u{2026}".to_string(),
                            search_placeholder: "Search\u{2026}".to_string(),
                            empty_text: "No framework found.".to_string(),
                            disabled: true,
                            ..Default::default()
                        }))
                    }
                    div {
                        h3 style="font-size:0.875rem;margin-bottom:0.5rem;" { "Auto-highlight" }
                        (render(Props {
                            id: "fw-4".to_string(),
                            name: "framework-4".to_string(),
                            options: frameworks.clone(),
                            selected: None,
                            placeholder: "Pick any\u{2026}".to_string(),
                            search_placeholder: "Search\u{2026}".to_string(),
                            empty_text: "No framework found.".to_string(),
                            auto_highlight: true,
                            ..Default::default()
                        }))
                    }
                    div {
                        h3 style="font-size:0.875rem;margin-bottom:0.5rem;" { "With clear button" }
                        (render(Props {
                            id: "fw-5".to_string(),
                            name: "framework-5".to_string(),
                            options: frameworks.clone(),
                            selected: Some("svelte".to_string()),
                            placeholder: "Select\u{2026}".to_string(),
                            search_placeholder: "Search\u{2026}".to_string(),
                            empty_text: "No framework found.".to_string(),
                            show_clear: true,
                            ..Default::default()
                        }))
                    }
                    div {
                        h3 style="font-size:0.875rem;margin-bottom:0.5rem;" { "Invalid state" }
                        (render(Props {
                            id: "fw-6".to_string(),
                            name: "framework-6".to_string(),
                            options: frameworks,
                            selected: None,
                            placeholder: "Required\u{2026}".to_string(),
                            search_placeholder: "Search\u{2026}".to_string(),
                            empty_text: "No framework found.".to_string(),
                            aria_invalid: true,
                            ..Default::default()
                        }))
                    }
                }
            }

            // Second demo: assign team member (realistic search-to-select)
            section {
                h2 { "Assign team member" }
                p.mui-showcase__caption { "A realistic search-to-select pattern for assigning people to a task." }
                div style="display:flex;flex-direction:column;gap:1rem;max-width:24rem;" {
                    div class="mui-field" {
                        label class="mui-field__label" for="assign-lead" { "Lead" }
                        (render(Props {
                            id: "assign-lead".to_string(),
                            name: "lead".to_string(),
                            options: team_members.clone(),
                            selected: Some("alice".to_string()),
                            placeholder: "Assign lead\u{2026}".to_string(),
                            search_placeholder: "Search people\u{2026}".to_string(),
                            empty_text: "No team member found.".to_string(),
                            disabled: false,
                            ..Default::default()
                        }))
                    }
                    div class="mui-field" {
                        label class="mui-field__label" for="assign-reviewer" { "Reviewer" }
                        (render(Props {
                            id: "assign-reviewer".to_string(),
                            name: "reviewer".to_string(),
                            options: team_members.clone(),
                            selected: None,
                            placeholder: "Assign reviewer\u{2026}".to_string(),
                            search_placeholder: "Search people\u{2026}".to_string(),
                            empty_text: "No team member found.".to_string(),
                            disabled: false,
                            ..Default::default()
                        }))
                        p class="mui-field__description" { "Optional. The reviewer will be notified when the task is ready." }
                    }
                    div class="mui-field" {
                        label class="mui-field__label" for="assign-watchers" { "Watchers (multi)" }
                        (render(Props {
                            id: "assign-watchers".to_string(),
                            name: "watchers".to_string(),
                            options: team_members,
                            selected: None,
                            selected_values: vec!["bob".to_string(), "elena".to_string(), "grace".to_string()],
                            multiple: true,
                            placeholder: "Add watchers\u{2026}".to_string(),
                            search_placeholder: "Search people\u{2026}".to_string(),
                            empty_text: "No team member found.".to_string(),
                            ..Default::default()
                        }))
                    }
                }
            }

            // Third demo: grouped options
            section {
                h2 { "Grouped options" }
                p.mui-showcase__caption { "Options separated into logical groups with uppercase labels." }
                div style="max-width:24rem;" {
                    (render(Props {
                        id: "stack".to_string(),
                        name: "stack".to_string(),
                        groups: grouped,
                        selected: Some("rust".to_string()),
                        placeholder: "Pick a stack\u{2026}".to_string(),
                        search_placeholder: "Search technologies\u{2026}".to_string(),
                        empty_text: "No match.".to_string(),
                        ..Default::default()
                    }))
                }
            }
        }
    }
}