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
//! 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>,
}
/// Menu entry: item, separator, or label
#[derive(Debug, Clone)]
pub enum MenuEntry {
Item(MenuItem),
Separator,
Label(String),
}
/// 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"
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" hidden {
@for entry in &props.items {
(render_entry(entry))
}
}
}
}
}
/// Render a single menu entry (shared between menu 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)
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) }
}
}
}
}
/// 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,
}),
],
}))
}
}
}
}
}