forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

dropdown_menu.rs327 lines, 10.1 KiBHistoryRawDownload
1use topcoat::{
2 Result,
3 view::{Attributes, Child, StaticClass, View, attributes, class, component, view},
4};
5
6use super::floating::{FloatingAlign, FloatingAnchor, FloatingSide, floating_variants};
7
8/// A floating action menu controlled by a trigger.
9///
10/// Add a [`dropdown_menu_trigger`] and a [`dropdown_menu_content`] as children. Create
11/// an ID with [`element_id`](topcoat::view::element_id) and use it for the trigger's
12/// `target` and the content's `id`. The button opens and closes the menu without
13/// JavaScript. Clicking outside or pressing Escape also closes it. The menu appears
14/// above the page and is not cut off by a parent's overflow settings.
15///
16/// `attrs` adds attributes and classes to the wrapper `<div>`, which takes up no space
17/// in the layout. Use Tab to move between items. Arrow-key menu navigation is not
18/// provided.
19///
20/// ```ignore
21/// view! {
22/// let menu = element_id(cx);
23/// dropdown_menu(
24/// dropdown_menu_trigger(target: &menu, "Options")
25/// dropdown_menu_content(
26/// id: &menu,
27/// align: FloatingAlign::Start,
28/// dropdown_menu_item(close: Some(&menu), "Rename")
29/// dropdown_menu_item(close: Some(&menu), "Duplicate")
30/// dropdown_menu_separator()
31/// dropdown_menu_item(
32/// close: Some(&menu),
33/// attrs: attributes! { class="text-destructive" },
34/// "Delete"
35/// )
36/// )
37/// )
38/// }
39/// ```
40#[component]
41pub async fn dropdown_menu(
42 #[default] mut attrs: Attributes,
43 #[default] child: Child<'_>,
44) -> Result<impl View> {
45 Ok(view! {
46 <div
47 class=(class!("group/dropdown-menu contents", attrs.remove("class")))
48 (attrs)
49 >
50 (child)
51 </div>
52 })
53}
54
55/// A button for opening and closing the dropdown menu.
56///
57/// Set `target` to the [`dropdown_menu_content`]'s `id` and put the button's label in
58/// its children. Add [`button_variants`](super::button::button_variants) classes through
59/// `attrs` to give it the standard button style. All attributes go on the `<button>`.
60/// Use `group-has-[:popover-open]/dropdown-menu:` classes to change how children look
61/// while the menu is open.
62///
63/// ```ignore
64/// view! {
65/// dropdown_menu_trigger(
66/// target: &menu,
67/// attrs: attributes! {
68/// class=(button_variants(ButtonVariant::Outline, ButtonSize::Md))
69/// },
70/// "Options"
71/// icon(
72/// data: iconify_icon!("lucide:chevron-down"),
73/// attrs: attributes! {
74/// class="group-has-[:popover-open]/dropdown-menu:rotate-180"
75/// }
76/// )
77/// )
78/// }
79/// ```
80#[component]
81pub async fn dropdown_menu_trigger(
82 /// The menu ID this button controls.
83 target: &str,
84 /// Attributes to add to the button.
85 #[default]
86 mut attrs: Attributes,
87 /// Content shown inside the button.
88 #[default]
89 child: Child<'_>,
90) -> Result<impl View> {
91 Ok(view! {
92 <button
93 type="button"
94 popovertarget=(target)
95 class=(class!("cursor-pointer", attrs.remove("class")))
96 (attrs)
97 >
98 (child)
99 </button>
100 })
101}
102
103/// Classes for floating menu panels with their own background, border, and text color.
104const PANEL: StaticClass = class!(
105 "min-w-40 rounded-float border border-border bg-popover p-1",
106 "text-popover-foreground shadow-float outline-none",
107);
108
109/// The floating panel of a [`dropdown_menu`], holding the menu's items.
110///
111/// Use `side` to choose where the menu opens and `align` to line it up with the button.
112/// By default, it opens below the button and is centered on it. The browser tries other
113/// positions if there is not enough room in the window.
114#[component]
115pub async fn dropdown_menu_content(
116 /// The menu ID to pass to the trigger's `target`.
117 id: &str,
118 /// Where the menu opens relative to the button.
119 #[default]
120 side: FloatingSide,
121 /// Which edge or center of the button the menu lines up with.
122 #[default]
123 align: FloatingAlign,
124 /// Attributes to add to the menu's `<div>`.
125 #[default]
126 mut attrs: Attributes,
127 /// Items shown inside the menu.
128 #[default]
129 child: Child<'_>,
130) -> Result<impl View> {
131 Ok(view! {
132 <div
133 id=(id)
134 popover="auto"
135 class=(class!(
136 floating_variants(FloatingAnchor::Invoker, side, align),
137 PANEL,
138 attrs.remove("class"),
139 ))
140 (attrs)
141 >
142 (child)
143 </div>
144 })
145}
146
147/// Classes for a menu item and its interaction states.
148const ITEM: StaticClass = class!(
149 "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm",
150 "whitespace-nowrap hover:bg-foreground/5 focus-visible:bg-foreground/5",
151 "active:bg-foreground/10 disabled:pointer-events-none disabled:opacity-50",
152 "[&_svg]:size-4 [&_svg]:shrink-0",
153);
154
155/// A button for an action inside [`dropdown_menu_content`].
156///
157/// The button defaults to `type="button"`. Pass the menu's ID to `close` to close the
158/// menu and its submenus when clicked. Omit `close` to leave them open. If you set
159/// `type="submit"` through `attrs`, the button submits its form and ignores `close`.
160#[component]
161pub async fn dropdown_menu_item(
162 /// The menu ID to close when this button is clicked.
163 #[default]
164 close: Option<&str>,
165 /// Attributes to add to the button.
166 #[default]
167 mut attrs: Attributes,
168 /// Content shown inside the item.
169 #[default]
170 child: Child<'_>,
171) -> Result<impl View> {
172 Ok(view! {
173 <button
174 type="button"
175 popovertarget=(close)
176 popovertargetaction=(close.map(|_| "hide"))
177 class=(class!(ITEM, attrs.remove("class")))
178 (attrs)
179 >
180 (child)
181 </button>
182 })
183}
184
185/// A nested menu that opens from a row in the parent menu.
186///
187/// Add a [`dropdown_menu_sub_trigger`] and a [`dropdown_menu_sub_content`] as children.
188/// Give the trigger's `target` and the content's `id` the same value. The parent menu
189/// stays open when the submenu opens. Closing the parent closes the submenu too.
190///
191/// `attrs` adds attributes and classes to the wrapper `<div>`. The wrapper takes up no
192/// space in the layout.
193///
194/// ```ignore
195/// view! {
196/// let menu = element_id(cx);
197/// let move_to = element_id(cx);
198/// dropdown_menu_content(
199/// id: &menu,
200/// dropdown_menu_item(close: Some(&menu), "Back")
201/// dropdown_menu_sub(
202/// dropdown_menu_sub_trigger(target: &move_to, "Move to")
203/// dropdown_menu_sub_content(
204/// id: &move_to,
205/// dropdown_menu_item(close: Some(&menu), "Inbox")
206/// dropdown_menu_item(close: Some(&menu), "Archive")
207/// )
208/// )
209/// )
210/// }
211/// ```
212#[component]
213pub async fn dropdown_menu_sub(
214 #[default] mut attrs: Attributes,
215 #[default] child: Child<'_>,
216) -> Result<impl View> {
217 Ok(view! {
218 <div
219 class=(class!("group/dropdown-menu-sub contents", attrs.remove("class")))
220 (attrs)
221 >
222 (child)
223 </div>
224 })
225}
226
227/// The row that opens or closes a submenu.
228///
229/// Set `target` to the [`dropdown_menu_sub_content`]'s `id` and put the label in the
230/// children. The row includes a chevron and stays highlighted while the submenu is
231/// open. `attrs` adds attributes and classes to the row's `<button>`.
232#[component]
233pub async fn dropdown_menu_sub_trigger(
234 /// The submenu ID this button controls.
235 target: &str,
236 /// Attributes to add to the button.
237 #[default]
238 mut attrs: Attributes,
239 /// Content shown inside the row.
240 #[default]
241 child: Child<'_>,
242) -> Result<impl View> {
243 Ok(view! {
244 <button
245 type="button"
246 popovertarget=(target)
247 class=(class!(
248 ITEM,
249 "group-has-[:popover-open]/dropdown-menu-sub:bg-foreground/5",
250 attrs.remove("class"),
251 ))
252 (attrs)
253 >
254 (child)
255 <svg
256 class="ml-auto size-4 shrink-0"
257 aria-hidden="true"
258 viewBox="0 0 24 24"
259 fill="none"
260 stroke="currentColor"
261 stroke-width="2"
262 stroke-linecap="round"
263 stroke-linejoin="round"
264 ><path d="m9 18 6-6-6-6"></path></svg>
265 </button>
266 })
267}
268
269/// A submenu that opens beside its button, to the right unless `side` says otherwise.
270///
271/// The browser tries other positions if the submenu does not fit in the window.
272#[component]
273pub async fn dropdown_menu_sub_content(
274 /// The submenu ID to pass to the trigger's `target`.
275 id: &str,
276 /// Which side of the row the submenu opens on.
277 #[default(FloatingSide::Right)]
278 side: FloatingSide,
279 /// Attributes to add to the submenu's `<div>`.
280 #[default]
281 mut attrs: Attributes,
282 /// Items shown inside the submenu.
283 #[default]
284 child: Child<'_>,
285) -> Result<impl View> {
286 Ok(view! {
287 <div
288 id=(id)
289 popover="auto"
290 class=(class!(
291 floating_variants(FloatingAnchor::Invoker, side, FloatingAlign::Start),
292 PANEL,
293 attrs.remove("class"),
294 ))
295 (attrs)
296 >
297 (child)
298 </div>
299 })
300}
301
302/// A non-interactive heading grouping the items after it.
303#[component]
304pub async fn dropdown_menu_label(
305 #[default] mut attrs: Attributes,
306 #[default] child: Child<'_>,
307) -> Result<impl View> {
308 Ok(view! {
309 <p
310 class=(class!(
311 "px-2 py-1.5 text-xs font-medium text-muted-foreground",
312 attrs.remove("class"),
313 ))
314 (attrs)
315 >
316 (child)
317 </p>
318 })
319}
320
321/// A hairline rule separating groups of items.
322#[component]
323pub async fn dropdown_menu_separator(#[default] mut attrs: Attributes) -> Result<impl View> {
324 Ok(view! {
325 <hr class=(class!("-mx-1 my-1 border-border", attrs.remove("class"))) (attrs)>
326 })
327}