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
//! Switch component — maud-ui Wave 2
use maud::{html, Markup};
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Size {
#[default]
Default,
Sm,
}
#[derive(Clone, Debug)]
pub struct Props {
pub name: String,
pub id: String,
pub label: String,
pub checked: bool,
pub disabled: bool,
/// Explicit accessible name. Required when `label` is empty (e.g., when
/// the switch sits next to an external label or description block).
/// Falls back to `label` if not set.
pub aria_label: Option<String>,
/// Visual size variant. `Default` = 2.75rem × 1.5rem, `Sm` = 2rem × 1.125rem.
pub size: Size,
/// Propagates to the hidden `<input>` as `aria-invalid="true"` when set.
pub aria_invalid: bool,
/// Propagates to the hidden `<input>` as `required` when set.
pub required: bool,
}
impl Default for Props {
fn default() -> Self {
Self {
name: "switch".to_string(),
id: "switch".to_string(),
label: "Toggle".to_string(),
checked: false,
disabled: false,
aria_label: None,
size: Size::Default,
aria_invalid: false,
required: false,
}
}
}
pub fn render(props: Props) -> Markup {
let aria_checked = if props.checked { "true" } else { "false" };
let value = if props.checked { "true" } else { "false" };
let accessible_name = props
.aria_label
.clone()
.unwrap_or_else(|| props.label.clone());
let size_cls = match props.size {
Size::Default => "",
Size::Sm => "mui-switch--sm",
};
let switch_class = if size_cls.is_empty() {
"mui-switch".to_string()
} else {
format!("mui-switch {}", size_cls)
};
html! {
span class="mui-switch-wrap" {
@if props.disabled {
button type="button" class=(switch_class.clone()) role="switch"
aria-checked=(aria_checked)
aria-label=(accessible_name)
id=(props.id.clone())
data-mui="switch"
data-name=(props.name.clone())
disabled {
span class="mui-switch__thumb" aria-hidden="true";
}
} @else {
button type="button" class=(switch_class.clone()) role="switch"
aria-checked=(aria_checked)
aria-label=(accessible_name)
id=(props.id.clone())
data-mui="switch"
data-name=(props.name.clone()) {
span class="mui-switch__thumb" aria-hidden="true";
}
}
input type="hidden" name=(props.name.clone()) value=(value)
class="mui-switch__value" aria-hidden="true"
aria-invalid=[if props.aria_invalid { Some("true") } else { None }]
required[props.required];
@if !props.label.is_empty() {
label for=(props.id) class="mui-switch__label" {
(props.label)
}
}
}
}
}
pub fn showcase() -> Markup {
html! {
div.mui-showcase__grid {
// Realistic settings panel
section {
h2 { "Notification Settings" }
div style="display:flex;flex-direction:column;gap:1rem;max-width:28rem;" {
// Marketing emails — off
div style="display:flex;align-items:flex-start;justify-content:space-between;gap:1rem;" {
div {
label for="sw-marketing" style="font-size:0.875rem;font-weight:500;color:var(--mui-text);display:block;" {
"Marketing emails"
}
span style="font-size:0.8125rem;color:var(--mui-text-muted);" {
"Receive emails about new products, features, and more."
}
}
(render(Props {
name: "marketing".to_string(),
id: "sw-marketing".to_string(),
label: String::new(),
checked: false,
disabled: false,
aria_label: Some("Marketing emails".to_string()),
..Default::default()
}))
}
// Push notifications — on
div style="display:flex;align-items:flex-start;justify-content:space-between;gap:1rem;" {
div {
label for="sw-push" style="font-size:0.875rem;font-weight:500;color:var(--mui-text);display:block;" {
"Push notifications"
}
span style="font-size:0.8125rem;color:var(--mui-text-muted);" {
"Receive notifications directly on your device."
}
}
(render(Props {
name: "push".to_string(),
id: "sw-push".to_string(),
label: String::new(),
checked: true,
disabled: false,
aria_label: Some("Push notifications".to_string()),
..Default::default()
}))
}
// Airplane mode — disabled
div style="display:flex;align-items:flex-start;justify-content:space-between;gap:1rem;opacity:0.6;" {
div {
label for="sw-airplane" style="font-size:0.875rem;font-weight:500;color:var(--mui-text);display:block;" {
"Airplane mode"
}
span style="font-size:0.8125rem;color:var(--mui-text-muted);" {
"Managed by your organization."
}
}
(render(Props {
name: "airplane".to_string(),
id: "sw-airplane".to_string(),
label: String::new(),
checked: false,
disabled: true,
aria_label: Some("Airplane mode".to_string()),
..Default::default()
}))
}
}
}
// Simple inline states
section {
h2 { "States" }
div.mui-showcase__row {
(render(Props {
name: "demo-off".to_string(),
id: "demo-off".to_string(),
label: "Off".to_string(),
checked: false,
disabled: false,
aria_label: None,
..Default::default()
}))
(render(Props {
name: "demo-on".to_string(),
id: "demo-on".to_string(),
label: "On".to_string(),
checked: true,
disabled: false,
aria_label: None,
..Default::default()
}))
(render(Props {
name: "demo-disabled".to_string(),
id: "demo-disabled".to_string(),
label: "Disabled".to_string(),
checked: false,
disabled: true,
aria_label: None,
..Default::default()
}))
(render(Props {
name: "demo-locked".to_string(),
id: "demo-locked".to_string(),
label: "Locked on".to_string(),
checked: true,
disabled: true,
aria_label: None,
..Default::default()
}))
}
}
// Size variant — Sm
section {
h2 { "Sizes" }
p.mui-showcase__caption { "Default and compact (sm) variants." }
div.mui-showcase__row {
(render(Props {
name: "size-default".to_string(),
id: "size-default".to_string(),
label: "Default".to_string(),
checked: true,
..Default::default()
}))
(render(Props {
name: "size-sm".to_string(),
id: "size-sm".to_string(),
label: "Compact".to_string(),
checked: true,
size: Size::Sm,
..Default::default()
}))
}
}
// Form integration — required + aria-invalid
section {
h2 { "Form Integration" }
p.mui-showcase__caption { "Required and invalid states propagate to the hidden input." }
div.mui-showcase__row {
(render(Props {
name: "terms".to_string(),
id: "sw-terms".to_string(),
label: "Accept terms (required)".to_string(),
checked: false,
required: true,
..Default::default()
}))
(render(Props {
name: "consent".to_string(),
id: "sw-consent".to_string(),
label: "Consent (invalid)".to_string(),
checked: false,
aria_invalid: true,
..Default::default()
}))
}
}
}
}
}