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
//! Command component — Cmd+K command palette with search, grouped items, and keyboard navigation.
use maud::{html, Markup};
/// A single command item in the palette
#[derive(Clone, Debug)]
pub struct CommandItem {
/// Display label for the command
pub label: String,
/// Optional keyboard shortcut (e.g., "⌘N")
pub shortcut: Option<String>,
/// Optional group name for categorization
pub group: Option<String>,
/// Whether the item is disabled
pub disabled: bool,
}
/// Command palette rendering properties
#[derive(Clone, Debug)]
pub struct Props {
/// Unique identifier for the command palette dialog
pub id: String,
/// List of command items
pub items: Vec<CommandItem>,
/// Placeholder text for the search input
pub placeholder: String,
}
impl Default for Props {
fn default() -> Self {
Self {
id: "command".to_string(),
items: vec![],
placeholder: "Type a command or search\u{2026}".to_string(),
}
}
}
/// Render a trigger button that opens the command palette
pub fn trigger(target_id: &str, label: &str) -> Markup {
html! {
button type="button"
class="mui-btn mui-btn--default mui-btn--md"
data-mui="command-trigger"
data-target=(target_id)
{
(label)
}
}
}
/// Render a keyboard shortcut slot for a command item.
///
/// Matches the `<kbd class="mui-kbd">` markup that `CommandItem::shortcut`
/// emits inside [`render`], so callers can drop a shortcut inline when
/// composing their own item markup.
pub fn shortcut(children: Markup) -> Markup {
html! {
kbd class="mui-kbd mui-command__shortcut" { (children) }
}
}
/// Render a visual separator between command groups.
///
/// Use between groups of items when composing a custom command list.
pub fn separator() -> Markup {
html! {
div class="mui-command__separator" role="separator" {}
}
}
/// Render the "no results" empty-state row for a command list.
///
/// Use when a search yields no matches and the caller is composing the
/// list markup by hand rather than relying on [`render`]'s built-in
/// empty slot.
pub fn empty(text: &str) -> Markup {
html! {
div class="mui-command__empty" { (text) }
}
}
/// Render the command palette
pub fn render(props: Props) -> Markup {
// Collect unique groups in insertion order
let mut groups: Vec<String> = Vec::new();
for item in &props.items {
let group_key = item.group.clone().unwrap_or_default();
let mut found = false;
for g in &groups {
if *g == group_key {
found = true;
break;
}
}
if !found {
groups.push(group_key);
}
}
html! {
dialog class="mui-command"
id=(props.id)
data-mui="command"
aria-label="Command palette"
aria-modal="true"
{
div class="mui-command__search-wrap" {
span class="mui-command__search-icon" aria-hidden="true" { "\u{2315}" }
input type="text" class="mui-command__search"
placeholder=(props.placeholder)
autocomplete="off"
aria-label="Search commands";
}
div class="mui-command__list" role="listbox" {
@for group_name in &groups {
div class="mui-command__group" {
@if !group_name.is_empty() {
div class="mui-command__group-label" { (group_name) }
}
@for item in &props.items {
@let item_group = item.group.clone().unwrap_or_default();
@if item_group == *group_name {
@if item.disabled {
div class="mui-command__item mui-command__item--disabled"
role="option"
tabindex="-1"
aria-disabled="true"
data-label=(item.label)
{
span class="mui-command__item-label" { (item.label) }
@if let Some(shortcut) = &item.shortcut {
kbd class="mui-kbd" { (shortcut) }
}
}
} @else {
div class="mui-command__item"
role="option"
tabindex="-1"
data-label=(item.label)
{
span class="mui-command__item-label" { (item.label) }
@if let Some(shortcut) = &item.shortcut {
kbd class="mui-kbd" { (shortcut) }
}
}
}
}
}
}
}
}
div class="mui-command__empty" hidden { "No results found." }
}
}
}
/// Showcase the command palette
pub fn showcase() -> Markup {
let items = vec![
CommandItem {
label: "Calendar".to_string(),
shortcut: None,
group: Some("Suggestions".to_string()),
disabled: false,
},
CommandItem {
label: "Search".to_string(),
shortcut: None,
group: Some("Suggestions".to_string()),
disabled: false,
},
CommandItem {
label: "Settings".to_string(),
shortcut: None,
group: Some("Suggestions".to_string()),
disabled: false,
},
CommandItem {
label: "New File".to_string(),
shortcut: Some("\u{2318}N".to_string()),
group: Some("Actions".to_string()),
disabled: false,
},
CommandItem {
label: "Save".to_string(),
shortcut: Some("\u{2318}S".to_string()),
group: Some("Actions".to_string()),
disabled: false,
},
CommandItem {
label: "Export".to_string(),
shortcut: None,
group: Some("Actions".to_string()),
disabled: false,
},
];
html! {
div.mui-showcase__grid {
div {
p.mui-showcase__caption { "Command palette trigger" }
div.mui-showcase__row {
(trigger("demo-command", "Open command palette"))
span.mui-text-muted style="font-size: 0.875rem;" {
"Press "
kbd.mui-kbd { "\u{2318}K" }
}
}
}
div {
(render(Props {
id: "demo-command".to_string(),
items,
placeholder: "Type a command or search\u{2026}".to_string(),
}))
}
// Composable slots demo: shortcut() + separator() + empty()
div {
p.mui-showcase__caption { "Composable slots: shortcut / separator / empty" }
div class="mui-command" style="position: static; display: block; max-width: 24rem; width: 100%;" {
div class="mui-command__list" role="listbox" {
div class="mui-command__group" {
div class="mui-command__group-label" { "File" }
div class="mui-command__item" role="option" tabindex="-1" {
span class="mui-command__item-label" { "New File" }
(shortcut(html! { "\u{2318}N" }))
}
div class="mui-command__item" role="option" tabindex="-1" {
span class="mui-command__item-label" { "Save" }
(shortcut(html! { "\u{2318}S" }))
}
}
(separator())
div class="mui-command__group" {
div class="mui-command__group-label" { "Edit" }
div class="mui-command__item" role="option" tabindex="-1" {
span class="mui-command__item-label" { "Undo" }
(shortcut(html! { "\u{2318}Z" }))
}
}
}
}
}
div {
p.mui-showcase__caption { "Empty state" }
div class="mui-command" style="position: static; display: block; max-width: 24rem; width: 100%;" {
div class="mui-command__list" role="listbox" {
(empty("No results found."))
}
}
}
}
}
}