1use std::fmt;
4
5use hypersig::NodeRef;
6use hypersig::html::{El, Id, Role, attr, div};
7use hypersig::style::Style;
8use hypersig::style::value::{BorderStyle, PositionTryFallbacks, px, raw, rem};
9
10use crate::button::{ButtonSize, ButtonVariant, button};
11use crate::id::anchor;
12use crate::token;
13
14pub struct Popover<S> {
55 panel: NodeRef<S>,
56}
57
58impl<S> Popover<S> {
59 #[must_use]
61 pub const fn new(panel: NodeRef<S>) -> Self {
62 Self { panel }
63 }
64
65 fn id(self) -> Id {
66 self.panel.id()
67 }
68
69 fn trigger_id(self) -> Id {
70 self.id().with_key("trigger")
71 }
72
73 fn anchor(self) -> String {
74 anchor(&self.id())
75 }
76
77 #[must_use]
79 pub fn trigger(self, variant: ButtonVariant, size: ButtonSize) -> El<S> {
80 button(variant, size)
81 .id(self.trigger_id())
82 .attr(attr::ARIA_HASPOPUP, "dialog")
83 .command_for(&self.id())
84 .attr(attr::COMMAND, "toggle-popover")
85 .css(Style::new().anchor_name(raw(self.anchor())))
86 }
87
88 #[must_use]
90 pub fn popover(self) -> El<S> {
91 div()
92 .id(self.id())
93 .attr(attr::POPOVER, "auto")
94 .role(Role::Dialog)
95 .aria_labelledby(&[&self.trigger_id()])
96 .node_ref(self.panel)
97 .css(panel_style(self.anchor()))
98 }
99}
100
101impl<S> Clone for Popover<S> {
102 fn clone(&self) -> Self {
103 *self
104 }
105}
106
107impl<S> Copy for Popover<S> {}
108
109impl<S> fmt::Debug for Popover<S> {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 f.debug_tuple("Popover").field(&self.panel).finish()
112 }
113}
114
115fn panel_style(anchor: String) -> Style {
116 Style::new()
119 .position_anchor(raw(anchor))
120 .position_area(raw("block-end span-inline-end"))
121 .position_try_fallbacks(PositionTryFallbacks::FlipBlock)
122 .inset(raw("auto"))
123 .margin(px(0))
124 .margin_top(rem(0.25))
125 .width(rem(18))
126 .max_width(raw("calc(100vw - 2rem)"))
127 .padding(rem(1))
128 .border_width(px(1))
129 .border_style(BorderStyle::Solid)
130 .border_color(token::border().var())
131 .border_radius(px(6))
132 .background_color(token::popover().var())
133 .color(token::popover_foreground().var())
134 .box_shadow(raw(
135 "0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1)",
136 ))
137}
138
139#[cfg(test)]
140mod tests {
141 use hypersig::html::div;
142
143 use super::Popover;
144
145 struct Page;
146
147 hypersig::node_ref!(const PANEL: NodeRef<Page>);
148 const INFO: Popover<Page> = Popover::new(PANEL);
149
150 #[test]
151 fn the_panel_is_named_by_its_button() {
152 let trigger = INFO
153 .trigger(crate::ButtonVariant::Outline, crate::ButtonSize::Default)
154 .render();
155 let panel = INFO.popover().render();
156 let id = "hypersig_ui::popover::tests::PANEL";
157 assert!(trigger.contains(&format!(r#"id="{id}-trigger""#)));
158 assert!(trigger.contains(&format!(r#"commandfor="{id}""#)));
159 assert!(panel.contains(&format!(r#"aria-labelledby="{id}-trigger""#)));
160 assert!(panel.contains(r#"role="dialog""#));
161 }
162
163 #[test]
164 #[should_panic(
165 expected = "`hypersig_ui::popover::tests::PANEL` where the runtime can reach both"
166 )]
167 fn one_popover_placed_twice_panics_when_rendered() {
168 let _ = div::<Page>()
169 .child(INFO.popover())
170 .child(INFO.popover())
171 .render();
172 }
173}