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
//! Popover component — non-modal floating panel anchored to a trigger.
//! Click trigger opens/closes. Click outside or ESC closes. Focus is not trapped.
use maud::{html, Markup};
/// Vertical placement of the popover relative to the trigger.
///
/// Deprecated in favor of [`Side`], which supports all four directions.
/// Retained for backward compatibility with existing callers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Placement {
Top,
Bottom,
}
impl Placement {
#[allow(dead_code)] // retained for symmetry with Side::class_part and external inspection
fn class_part(&self) -> &'static str {
match self {
Self::Top => "top",
Self::Bottom => "bottom",
}
}
/// Convert to the equivalent `Side` for the new 4-way side matrix.
fn to_side(self) -> Side {
match self {
Self::Top => Side::Top,
Self::Bottom => Side::Bottom,
}
}
}
impl Default for Placement {
fn default() -> Self {
Self::Bottom
}
}
/// Side of the trigger the popover should render on (shadcn-compatible).
///
/// Mirrors Radix/shadcn `side` prop: top | right | bottom | left.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
Top,
Right,
Bottom,
Left,
}
impl Side {
fn class_part(&self) -> &'static str {
match self {
Self::Top => "top",
Self::Right => "right",
Self::Bottom => "bottom",
Self::Left => "left",
}
}
}
impl Default for Side {
fn default() -> Self {
Self::Bottom
}
}
/// Horizontal alignment of the popover
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Align {
Start,
Center,
End,
}
impl Align {
fn class_part(&self) -> &'static str {
match self {
Self::Start => "start",
Self::Center => "center",
Self::End => "end",
}
}
}
impl Default for Align {
fn default() -> Self {
Self::Center
}
}
/// Popover rendering properties
#[derive(Clone, Debug)]
pub struct Props {
/// Unique identifier for the popover content
pub id: String,
/// The element that triggers the popover open/close (typically a button)
pub trigger: Markup,
/// Markup content displayed inside the popover
pub content: Markup,
/// Side of the trigger the popover renders on. When set, takes precedence
/// over `placement` (shadcn-compatible 4-way side selector).
pub side: Option<Side>,
/// Vertical placement relative to trigger (legacy 2-way API, default: Bottom).
/// Kept for backward compatibility; prefer `side` for new code.
pub placement: Placement,
/// Horizontal alignment (default: Center)
pub align: Align,
/// Offset in pixels between the trigger and the popover along the side axis.
/// Emitted as `data-side-offset` for JS-driven positioning engines.
pub side_offset: Option<u32>,
/// Controlled open state. When `Some(true)`, the popover is rendered visible
/// (no `hidden` attribute, `data-state="open"`). When `Some(false)`, rendered
/// hidden. When `None`, left for client JS to toggle (legacy default).
pub open: Option<bool>,
}
impl Default for Props {
fn default() -> Self {
Self {
id: "popover".to_string(),
trigger: html! {},
content: html! {},
side: None,
placement: Placement::default(),
align: Align::default(),
side_offset: None,
open: None,
}
}
}
/// Render a popover with the given properties
pub fn render(props: Props) -> Markup {
// New `side` takes precedence over legacy `placement`.
let side = props.side.unwrap_or_else(|| props.placement.to_side());
let content_class = format!(
"mui-popover__content mui-popover__content--{}-{}",
side.class_part(),
props.align.class_part()
);
let content_id = format!("{}-content", props.id);
let is_open = props.open.unwrap_or(false);
let data_state = if is_open { "open" } else { "closed" };
let data_visible = if is_open { "true" } else { "false" };
// Only emit `hidden` when NOT explicitly open. `open: Some(true)` must render visible.
let render_hidden = !is_open;
html! {
span.mui-popover data-mui="popover" {
span.mui-popover__trigger {
(props.trigger)
}
div class=(content_class)
id=(content_id)
role="dialog"
data-state=(data_state)
data-visible=(data_visible)
data-side=(side.class_part())
data-align=(props.align.class_part())
data-side-offset=[props.side_offset.map(|v| v.to_string())]
hidden[render_hidden]
tabindex="-1"
{
(props.content)
}
}
}
}
/// Render a popover header block (shadcn: `PopoverHeader`).
/// Groups title + description; adds spacing and bottom margin.
pub fn header(children: Markup) -> Markup {
html! {
div.mui-popover__header {
(children)
}
}
}
/// Render a popover title (shadcn: `PopoverTitle`). Semantic heading (h3)
/// styled as the panel's primary label.
pub fn title(children: Markup) -> Markup {
html! {
h3.mui-popover__title {
(children)
}
}
}
/// Render a popover description (shadcn: `PopoverDescription`). Muted
/// supporting copy under the title.
pub fn description(children: Markup) -> Markup {
html! {
p.mui-popover__description {
(children)
}
}
}
/// Showcase all popover placements and alignments
pub fn showcase() -> Markup {
html! {
div.mui-showcase__grid {
div {
p.mui-showcase__caption { "Popover with Form (bottom-start)" }
{
(render(Props {
id: "demo-pop-1".to_string(),
trigger: html! { button.mui-btn.mui-btn--primary.mui-btn--md { "Open popover" } },
content: html! {
div {
(header(html! {
(title(html! { "Dimensions" }))
(description(html! { "Set width and height for the element." }))
}))
div.mui-field {
label.mui-label { "Width" }
input.mui-input type="text" placeholder="e.g., 100px" {}
}
div.mui-field {
label.mui-label { "Height" }
input.mui-input type="text" placeholder="e.g., 100px" {}
}
button.mui-btn.mui-btn--primary style="margin-top: 1rem;" {
"Apply"
}
}
},
side: Some(Side::Bottom),
align: Align::Start,
side_offset: Some(8),
..Props::default()
}))
}
}
div {
p.mui-showcase__caption { "Right-Center with header/title/description" }
{
(render(Props {
id: "demo-pop-2".to_string(),
trigger: html! { button.mui-btn.mui-btn--md { "Show info" } },
content: html! {
(header(html! {
(title(html! { "Shortcut" }))
(description(html! { "Press ⌘K to open the command palette from anywhere." }))
}))
},
side: Some(Side::Right),
align: Align::Center,
side_offset: Some(12),
..Props::default()
}))
}
}
div {
p.mui-showcase__caption { "Left-End, controlled open state" }
{
(render(Props {
id: "demo-pop-3".to_string(),
trigger: html! { button.mui-btn.mui-btn--md { "Always open" } },
content: html! {
(header(html! {
(title(html! { "Controlled" }))
(description(html! { "This popover is rendered open via the `open` prop." }))
}))
},
side: Some(Side::Left),
align: Align::End,
open: Some(true),
..Props::default()
}))
}
}
}
}
}