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
//! 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
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Placement {
Top,
Bottom,
}
impl Placement {
fn class_part(&self) -> &'static str {
match self {
Self::Top => "top",
Self::Bottom => "bottom",
}
}
}
impl Default for Placement {
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,
/// Vertical placement relative to trigger (default: Bottom)
pub placement: Placement,
/// Horizontal alignment (default: Center)
pub align: Align,
}
impl Default for Props {
fn default() -> Self {
Self {
id: "popover".to_string(),
trigger: html! {},
content: html! {},
placement: Placement::default(),
align: Align::default(),
}
}
}
/// Render a popover with the given properties
pub fn render(props: Props) -> Markup {
let content_class = format!(
"mui-popover__content mui-popover__content--{}-{}",
props.placement.class_part(),
props.align.class_part()
);
let content_id = format!("{}-content", props.id);
html! {
span.mui-popover data-mui="popover" {
span.mui-popover__trigger {
(props.trigger)
}
div class=(content_class)
id=(content_id)
role="dialog"
hidden
data-visible="false"
tabindex="-1"
{
(props.content)
}
}
}
}
/// Showcase all popover placements and alignments
pub fn showcase() -> Markup {
html! {
div.mui-showcase__grid {
div {
p.mui-showcase__caption { "Popover with Form" }
{
(render(Props {
id: "demo-pop-1".to_string(),
trigger: html! { button.mui-btn.mui-btn--primary.mui-btn--md { "Open popover" } },
content: html! {
div {
h3.mui-showcase__subheading { "Dimensions" }
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"
}
}
},
placement: Placement::Bottom,
align: Align::Start,
}))
}
}
}
}
}