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
//! Menu component — dropdown menu for actions
use maud::{html, Markup};
/// A single menu item
#[derive(Debug, Clone)]
pub struct MenuItem {
pub label: String,
pub action: String,
pub disabled: bool,
pub destructive: bool,
/// Optional keyboard shortcut displayed on the right (e.g. "⌘Z")
pub shortcut: Option<String>,
}
/// A single radio option inside a [`MenuEntry::RadioGroup`].
#[derive(Debug, Clone, Default)]
pub struct RadioItem {
pub label: String,
pub value: String,
pub checked: bool,
pub disabled: bool,
/// Optional keyboard shortcut displayed on the right (e.g. "⌘1")
pub shortcut: Option<String>,
}
/// Menu entry: item, separator, label, checkbox, radio group, submenu, or group.
///
/// Additive — new variants may be added in minor releases. Existing callers
/// that construct `Item`, `Separator`, or `Label` remain source-compatible.
#[derive(Debug, Clone)]
pub enum MenuEntry {
Item(MenuItem),
Separator,
Label(String),
/// A menuitemcheckbox with an on/off state.
CheckboxItem {
label: String,
checked: bool,
disabled: bool,
shortcut: Option<String>,
},
/// A named radio group wrapping a set of [`RadioItem`]s.
RadioGroup {
name: String,
items: Vec<RadioItem>,
},
/// A submenu trigger + nested content.
Sub {
trigger_label: String,
items: Vec<MenuEntry>,
},
/// Generic grouping wrapper (`role="group"`) for related entries.
Group {
items: Vec<MenuEntry>,
},
}
/// Menu rendering properties
#[derive(Debug, Clone)]
pub struct Props {
/// Text displayed on the trigger button
pub trigger_label: String,
/// Unique identifier for the menu
pub id: String,
/// Menu entries (items, separators, and labels)
pub items: Vec<MenuEntry>,
}
/// Render a dropdown menu with the given properties
pub fn render(props: Props) -> Markup {
html! {
div.mui-menu data-mui="menu" {
button.mui-menu__trigger.mui-btn.mui-btn--default.mui-btn--md
type="button"
id=(format!("{}-trigger", props.id))
aria-expanded="false"
aria-haspopup="menu"
aria-controls=(format!("{}-items", props.id))
{
(props.trigger_label)
span.mui-menu__chevron aria-hidden="true" { "▾" }
}
div.mui-menu__content id=(format!("{}-items", props.id)) role="menu" aria-labelledby=(format!("{}-trigger", props.id)) hidden {
@for entry in &props.items {
(render_entry(entry))
}
}
}
}
}
/// Render a single menu entry (shared between menu, menubar, and context menu).
pub fn render_entry(entry: &MenuEntry) -> Markup {
html! {
@match entry {
MenuEntry::Item(item) => {
@let cls = if item.destructive { "mui-menu__item mui-menu__item--danger" } else { "mui-menu__item" };
button
type="button"
role="menuitem"
class=(cls)
data-action=(item.action)
data-variant=[item.destructive.then_some("destructive")]
tabindex="-1"
disabled[item.disabled]
{
(item.label.clone())
@if let Some(shortcut) = &item.shortcut {
span.mui-menu__shortcut { (shortcut) }
}
}
}
MenuEntry::Separator => {
div.mui-menu__separator role="separator" {}
}
MenuEntry::Label(text) => {
div.mui-menu__label { (text) }
}
MenuEntry::CheckboxItem { label, checked, disabled, shortcut } => {
button
type="button"
role="menuitemcheckbox"
class="mui-menu__item"
aria-checked=(if *checked { "true" } else { "false" })
aria-disabled=(if *disabled { "true" } else { "false" })
tabindex="-1"
disabled[*disabled]
{
span.mui-menu__checkbox-indicator aria-hidden="true" {}
(label.clone())
@if let Some(shortcut) = shortcut {
span.mui-menu__shortcut { (shortcut) }
}
}
}
MenuEntry::RadioGroup { name, items } => {
div.mui-menu__radio-group role="group" data-radio-group=(name) {
@for item in items {
button
type="button"
role="menuitemradio"
class="mui-menu__item"
aria-checked=(if item.checked { "true" } else { "false" })
aria-disabled=(if item.disabled { "true" } else { "false" })
data-value=(item.value)
data-radio-group=(name)
tabindex="-1"
disabled[item.disabled]
{
span.mui-menu__radio-indicator aria-hidden="true" {}
(item.label.clone())
@if let Some(shortcut) = &item.shortcut {
span.mui-menu__shortcut { (shortcut) }
}
}
}
}
}
MenuEntry::Sub { trigger_label, items } => {
div.mui-menu__sub-wrapper {
button
type="button"
role="menuitem"
class="mui-menu__item mui-menu__sub-trigger"
aria-haspopup="menu"
aria-expanded="false"
tabindex="-1"
{
(trigger_label.clone())
}
div.mui-menu__sub role="menu" hidden {
@for inner in items {
(render_entry(inner))
}
}
}
}
MenuEntry::Group { items } => {
div.mui-menu__group role="group" {
@for inner in items {
(render_entry(inner))
}
}
}
}
}
}
/// Showcase menu component
pub fn showcase() -> Markup {
html! {
div.mui-showcase__grid {
div {
p.mui-showcase__caption { "File menu" }
div.mui-showcase__row {
(render(Props {
trigger_label: "File".into(),
id: "demo-menu-file".into(),
items: vec![
MenuEntry::Item(MenuItem {
label: "New".into(),
action: "new".into(),
disabled: false,
destructive: false,
shortcut: Some("\u{2318}N".into()),
}),
MenuEntry::Item(MenuItem {
label: "Open\u{2026}".into(),
action: "open".into(),
disabled: false,
destructive: false,
shortcut: Some("\u{2318}O".into()),
}),
MenuEntry::Separator,
MenuEntry::Item(MenuItem {
label: "Save".into(),
action: "save".into(),
disabled: false,
destructive: false,
shortcut: Some("\u{2318}S".into()),
}),
MenuEntry::Item(MenuItem {
label: "Save As\u{2026}".into(),
action: "save-as".into(),
disabled: false,
destructive: false,
shortcut: Some("\u{21e7}\u{2318}S".into()),
}),
MenuEntry::Separator,
MenuEntry::Item(MenuItem {
label: "Print\u{2026}".into(),
action: "print".into(),
disabled: false,
destructive: false,
shortcut: Some("\u{2318}P".into()),
}),
MenuEntry::Separator,
MenuEntry::Item(MenuItem {
label: "Exit".into(),
action: "exit".into(),
disabled: false,
destructive: false,
shortcut: None,
}),
],
}))
}
}
div {
p.mui-showcase__caption { "User menu" }
div.mui-showcase__row {
(render(Props {
trigger_label: "My Account".into(),
id: "demo-menu-user".into(),
items: vec![
MenuEntry::Label("Account".into()),
MenuEntry::Item(MenuItem {
label: "Profile".into(),
action: "profile".into(),
disabled: false,
destructive: false,
shortcut: None,
}),
MenuEntry::Item(MenuItem {
label: "Settings".into(),
action: "settings".into(),
disabled: false,
destructive: false,
shortcut: None,
}),
MenuEntry::Separator,
MenuEntry::Item(MenuItem {
label: "Sign out".into(),
action: "sign-out".into(),
disabled: false,
destructive: true,
shortcut: None,
}),
],
}))
}
}
div {
p.mui-showcase__caption { "View menu — checkbox + radio + submenu" }
div.mui-showcase__row {
(render(Props {
trigger_label: "View".into(),
id: "demo-menu-view".into(),
items: vec![
MenuEntry::Label("Appearance".into()),
MenuEntry::CheckboxItem {
label: "Show Bookmarks Bar".into(),
checked: true,
disabled: false,
shortcut: Some("\u{21e7}\u{2318}B".into()),
},
MenuEntry::CheckboxItem {
label: "Show Full URLs".into(),
checked: false,
disabled: false,
shortcut: None,
},
MenuEntry::Separator,
MenuEntry::Label("Panel position".into()),
MenuEntry::RadioGroup {
name: "panel-position".into(),
items: vec![
RadioItem {
label: "Top".into(),
value: "top".into(),
checked: false,
disabled: false,
shortcut: None,
},
RadioItem {
label: "Right".into(),
value: "right".into(),
checked: true,
disabled: false,
shortcut: None,
},
RadioItem {
label: "Bottom".into(),
value: "bottom".into(),
checked: false,
disabled: false,
shortcut: None,
},
],
},
MenuEntry::Separator,
MenuEntry::Sub {
trigger_label: "More Tools".into(),
items: vec![
MenuEntry::Item(MenuItem {
label: "Save Page As\u{2026}".into(),
action: "save-page".into(),
disabled: false,
destructive: false,
shortcut: Some("\u{2318}S".into()),
}),
MenuEntry::Item(MenuItem {
label: "Create Shortcut\u{2026}".into(),
action: "create-shortcut".into(),
disabled: false,
destructive: false,
shortcut: None,
}),
MenuEntry::Separator,
MenuEntry::Item(MenuItem {
label: "Developer Tools".into(),
action: "devtools".into(),
disabled: false,
destructive: false,
shortcut: Some("\u{2325}\u{2318}I".into()),
}),
],
},
],
}))
}
}
}
}
}