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
//! Dialog component — modal dialog using native <dialog> element with focus trap and ESC/backdrop close.
use maud::{html, Markup};
/// Dialog size variants
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Size {
/// Default size (max-width 32rem, padded)
#[default]
Default,
/// Compact size (max-width 28rem, reduced padding)
Sm,
}
/// Dialog rendering properties
#[derive(Clone, Debug)]
pub struct Props {
/// Unique identifier for the dialog (used by trigger to open it)
pub id: String,
/// Dialog title
pub title: String,
/// Optional description text displayed below title
pub description: Option<String>,
/// Markup content displayed in dialog body
pub children: Markup,
/// Optional footer markup (buttons/actions, rendered right-aligned)
pub footer: Option<Markup>,
/// Initial open state (default false; if true renders with open attribute for SSR)
pub open: bool,
/// Whether to render the close button in the top-right (default true)
pub show_close_button: bool,
/// Dialog size variant (default Default)
pub size: Size,
}
impl Default for Props {
fn default() -> Self {
Self {
id: "dialog".to_string(),
title: "Dialog".to_string(),
description: None,
children: html! {},
footer: None,
open: false,
show_close_button: true,
size: Size::Default,
}
}
}
/// Render a dialog trigger button that opens the dialog with the given target_id
pub fn trigger(target_id: &str, label: &str) -> Markup {
html! {
button type="button"
class="mui-btn mui-btn--default mui-btn--md"
data-mui="dialog-trigger"
data-target=(target_id)
{
(label)
}
}
}
/// Render a close button for use inside the dialog (positioned absolute via CSS)
pub fn close_button(label: &str) -> Markup {
html! {
button type="button"
class="mui-dialog__close"
data-mui-close
aria-label=(label)
{
"\u{00d7}"
}
}
}
/// Render a dialog with the given properties
pub fn render(props: Props) -> Markup {
let title_id = format!("{}-title", props.id);
let desc_id = format!("{}-desc", props.id);
let has_desc = props.description.is_some();
let dialog_class = match props.size {
Size::Default => "mui-dialog",
Size::Sm => "mui-dialog mui-dialog--sm",
};
html! {
dialog class=(dialog_class)
id=(props.id)
data-mui="dialog"
aria-labelledby=(title_id)
aria-describedby=[if has_desc { Some(desc_id.as_str()) } else { None }]
aria-modal="true"
open[props.open]
{
// Close button — absolute positioned, outside header flow
@if props.show_close_button {
(close_button("Close"))
}
div class="mui-dialog__header" {
h2 class="mui-dialog__title" id=(title_id) {
(props.title)
}
}
@if let Some(desc) = props.description {
p class="mui-dialog__description" id=(desc_id) {
(desc)
}
}
div class="mui-dialog__body" {
(props.children)
}
@if let Some(footer) = props.footer {
div class="mui-dialog__footer" {
(footer)
}
}
}
}
}
/// Showcase all dialog use cases
pub fn showcase() -> Markup {
html! {
div.mui-showcase__grid {
// Edit Profile dialog
{
(trigger("demo-dialog-edit-profile", "Edit Profile"))
}
{
(render(Props {
id: "demo-dialog-edit-profile".to_string(),
title: "Edit Profile".to_string(),
description: Some("Update your personal information below.".to_string()),
children: html! {
div style="display:flex;align-items:center;gap:1rem;margin-bottom:1rem;" {
div style="width:3rem;height:3rem;border-radius:50%;background:var(--mui-muted);display:flex;align-items:center;justify-content:center;font-size:1.25rem;flex-shrink:0;" {
"JD"
}
p style="font-size:0.875rem;color:var(--mui-muted-fg);" {
"Upload a new avatar from your device."
}
}
div class="mui-field" {
label class="mui-label" { "Name" }
input class="mui-input" type="text" value="Jane Doe" {}
}
div class="mui-field" {
label class="mui-label" { "Email" }
input class="mui-input" type="email" value="jane@example.com" {}
}
},
footer: Some(html! {
button class="mui-btn mui-btn--secondary mui-btn--md" data-mui-close { "Cancel" }
button class="mui-btn mui-btn--primary mui-btn--md" { "Save" }
}),
..Default::default()
}))
}
// Share Document dialog
{
(trigger("demo-dialog-share-doc", "Share Document"))
}
{
(render(Props {
id: "demo-dialog-share-doc".to_string(),
title: "Share Document".to_string(),
description: Some("Invite a collaborator by email address.".to_string()),
children: html! {
div class="mui-field" {
label class="mui-label" { "Email address" }
input class="mui-input" type="email" placeholder="collaborator@company.com" {}
}
div class="mui-field" {
label class="mui-label" { "Permission" }
select class="mui-select__trigger" style="width:100%;" {
option value="viewer" { "Viewer" }
option value="editor" { "Editor" }
}
}
},
footer: Some(html! {
button class="mui-btn mui-btn--secondary mui-btn--md" data-mui-close { "Cancel" }
button class="mui-btn mui-btn--primary mui-btn--md" { "Send invite" }
}),
..Default::default()
}))
}
// Compact (Sm) dialog — demonstrates size: Sm
{
(trigger("demo-dialog-compact", "Compact Dialog"))
}
{
(render(Props {
id: "demo-dialog-compact".to_string(),
title: "Quick Action".to_string(),
description: Some("A compact dialog for short interactions.".to_string()),
children: html! {
p style="font-size:0.875rem;color:var(--mui-text-muted);" {
"This dialog uses the Sm size variant with reduced padding."
}
},
footer: Some(html! {
button class="mui-btn mui-btn--primary mui-btn--md" data-mui-close { "OK" }
}),
size: Size::Sm,
..Default::default()
}))
}
// No-close-button dialog — force a footer-only decision
{
(trigger("demo-dialog-forced-choice", "Forced Choice"))
}
{
(render(Props {
id: "demo-dialog-forced-choice".to_string(),
title: "Confirm Deletion".to_string(),
description: Some("This cannot be undone. Make a choice below.".to_string()),
children: html! {
p style="font-size:0.875rem;color:var(--mui-text-muted);" {
"The top-right close button is hidden — the user must use the footer buttons."
}
},
footer: Some(html! {
button class="mui-btn mui-btn--secondary mui-btn--md" data-mui-close { "Cancel" }
button class="mui-btn mui-btn--primary mui-btn--md" data-mui-close { "Delete" }
}),
show_close_button: false,
..Default::default()
}))
}
}
}
}