Skip to main content

lumen_dioxus/navigation/
chrome.rs

1//! Site and application chrome: `AppHeader`, `SiteHeader`, `SiteFooter`, `Sidebar`,
2//! `SidebarItem`, `SidebarTrigger`, and `ContextNavigation`.
3
4use std::rc::Rc;
5
6use dioxus::prelude::*;
7
8use crate::ButtonSize;
9use crate::class::{Glass, compose};
10use crate::hooks::use_unique_id;
11use crate::overlay::{focus_element, is_escape};
12
13use super::util::{has_content, has_slot, optional_text, resolve_safe_href};
14use super::web::{use_scroll_past, use_site_header_sheet};
15
16/// `data-surface` for a glass prop.
17pub(crate) fn surface(glass: Glass) -> &'static str {
18    if glass == Glass::Off {
19        "default"
20    } else {
21        "glass"
22    }
23}
24
25/// Whether a panel uses Lumen's styling or only its behavior hooks.
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
27#[non_exhaustive]
28pub enum NavigationVariant {
29    /// Lumen's surface styling.
30    #[default]
31    Default,
32    /// Structure and hooks only, for custom styling.
33    Unstyled,
34}
35
36impl NavigationVariant {
37    /// The `data-variant` value.
38    pub fn as_str(self) -> &'static str {
39        match self {
40            Self::Default => "default",
41            Self::Unstyled => "unstyled",
42        }
43    }
44
45    pub(crate) fn modifier(self, base: &str) -> Option<String> {
46        (self == Self::Unstyled).then(|| format!("{base}--unstyled"))
47    }
48}
49
50/// Props for [`AppHeader`].
51#[derive(Props, Clone, PartialEq)]
52pub struct AppHeaderProps {
53    /// Leading control, usually the sidebar trigger.
54    pub leading: Option<Element>,
55    /// Badges, status dots, or metadata. Wraps below the title on narrow widths.
56    pub status: Option<Element>,
57    /// Trailing controls such as search, notifications, or the account menu.
58    pub actions: Option<Element>,
59    /// Keeps the bar pinned to the top of its scroll container.
60    #[props(default)]
61    pub sticky: bool,
62    /// Extra classes appended after the Lumen classes.
63    #[props(into)]
64    pub class: Option<String>,
65    /// Any other `<header>` or global attribute.
66    #[props(extends = GlobalAttributes, extends = header)]
67    pub attributes: Vec<Attribute>,
68    /// The title or context, such as a [`Breadcrumb`](crate::Breadcrumb).
69    pub children: Element,
70}
71
72/// The top bar of an application shell.
73#[component]
74pub fn AppHeader(props: AppHeaderProps) -> Element {
75    let class = compose([
76        Some("ui-app-header"),
77        props.sticky.then_some("ui-app-header--sticky"),
78        props.class.as_deref(),
79    ]);
80    let leading = props.leading.filter(|slot| has_slot(Some(slot)));
81    let status = props.status.filter(|slot| has_slot(Some(slot)));
82    let actions = props.actions.filter(|slot| has_slot(Some(slot)));
83
84    rsx! {
85        header {
86            class,
87            "data-slot": "app-header",
88            "data-sticky": if props.sticky { "true" } else { "false" },
89            ..props.attributes,
90            if let Some(leading) = leading {
91                div { class: "ui-app-header__leading", "data-slot": "app-header-leading", {leading} }
92            }
93            div { class: "ui-app-header__title", "data-slot": "app-header-title", {props.children} }
94            if let Some(status) = status {
95                div { class: "ui-app-header__status", "data-slot": "app-header-status", {status} }
96            }
97            if let Some(actions) = actions {
98                div { class: "ui-app-header__actions", "data-slot": "app-header-actions", {actions} }
99            }
100        }
101    }
102}
103
104/// Props for [`SiteFooter`].
105#[derive(Props, Clone, PartialEq)]
106pub struct SiteFooterProps {
107    /// Logo lockup and a short description shown before the navigation groups.
108    pub brand: Option<Element>,
109    /// Bottom row for the copyright notice and attribution links.
110    pub legal: Option<Element>,
111    /// Accessible name of the footer navigation landmark. Defaults to `Footer`.
112    #[props(default = "Footer".to_string(), into)]
113    pub nav_label: String,
114    /// Extra classes appended after the Lumen class.
115    #[props(into)]
116    pub class: Option<String>,
117    /// Any other `<footer>` or global attribute.
118    #[props(extends = GlobalAttributes, extends = footer)]
119    pub attributes: Vec<Attribute>,
120    /// Navigation groups: each a `div { "data-slot": "site-footer-group", ... }` with a heading
121    /// and a list of links.
122    pub children: Element,
123}
124
125/// A website footer with brand, navigation groups, and a legal row.
126#[component]
127pub fn SiteFooter(props: SiteFooterProps) -> Element {
128    let class = compose([Some("ui-site-footer"), props.class.as_deref()]);
129    let brand = props.brand.filter(|slot| has_slot(Some(slot)));
130    let legal = props.legal.filter(|slot| has_slot(Some(slot)));
131    let has_navigation = has_slot(Some(&props.children));
132    let nav_label = optional_text(Some(&props.nav_label)).unwrap_or_else(|| "Footer".to_string());
133
134    rsx! {
135        footer { class, "data-slot": "site-footer", ..props.attributes,
136            div { class: "ui-site-footer__inner", "data-slot": "site-footer-inner",
137                if brand.is_some() || has_navigation {
138                    div { class: "ui-site-footer__top", "data-slot": "site-footer-top",
139                        if let Some(brand) = brand {
140                            div { class: "ui-site-footer__brand", "data-slot": "site-footer-brand", {brand} }
141                        }
142                        if has_navigation {
143                            nav {
144                                aria_label: nav_label,
145                                class: "ui-site-footer__nav",
146                                "data-slot": "site-footer-nav",
147                                {props.children}
148                            }
149                        }
150                    }
151                }
152                if let Some(legal) = legal {
153                    div { class: "ui-site-footer__legal", "data-slot": "site-footer-legal", {legal} }
154                }
155            }
156        }
157    }
158}
159
160/// How a [`SiteHeader`] collapses its navigation and actions while it is narrower than 40rem.
161#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
162#[non_exhaustive]
163pub enum SiteHeaderCollapse {
164    /// Opens them inline below the brand. Server rendering keeps them visible until the header
165    /// mounts.
166    #[default]
167    Panel,
168    /// Opens them in an off-canvas native popover from the inline-end edge, which the toggle
169    /// opens through the Popover API before hydration too.
170    Sheet,
171}
172
173impl SiteHeaderCollapse {
174    /// The collapse name, like lumen-core's `LumenSiteHeaderCollapse`.
175    pub fn as_str(self) -> &'static str {
176        match self {
177            Self::Panel => "panel",
178            Self::Sheet => "sheet",
179        }
180    }
181}
182
183/// Props for [`SiteHeader`].
184#[derive(Props, Clone, PartialEq)]
185pub struct SiteHeaderProps {
186    /// Logo lockup, usually a link to the home page.
187    pub brand: Option<Element>,
188    /// Theme toggle, repository link, or call to action. Collapses with the navigation.
189    pub actions: Option<Element>,
190    /// How the navigation and actions collapse below 40rem: `Panel` opens them inline below the
191    /// brand; `Sheet` opens them in an off-canvas native popover from the inline-end edge.
192    #[props(default)]
193    pub collapse: SiteHeaderCollapse,
194    /// Accessible name of the collapsed-menu toggle. Defaults to `Menu`.
195    #[props(default = "Menu".to_string(), into)]
196    pub menu_label: String,
197    /// Accessible name of the close button inside the open sheet. Defaults to `Close menu`. Used
198    /// with `collapse: SiteHeaderCollapse::Sheet`.
199    #[props(default = "Close menu".to_string(), into)]
200    pub close_label: String,
201    /// Accessible name of the primary navigation landmark. Defaults to `Main`.
202    #[props(default = "Main".to_string(), into)]
203    pub nav_label: String,
204    /// Keeps the header pinned to the top of the viewport while the page scrolls.
205    #[props(default = true)]
206    pub sticky: bool,
207    /// Header id; the menu panel id is `{id}-panel`. Generated when unset.
208    #[props(into)]
209    pub id: Option<String>,
210    /// Extra classes appended after the Lumen classes.
211    #[props(into)]
212    pub class: Option<String>,
213    /// Any other `<header>` or global attribute.
214    #[props(extends = GlobalAttributes, extends = header)]
215    pub attributes: Vec<Attribute>,
216    /// The primary navigation links.
217    pub children: Element,
218}
219
220/// The root's `data-state` value.
221fn open_state(open: bool) -> &'static str {
222    if open { "open" } else { "closed" }
223}
224
225/// `"true"` or `"false"`.
226fn bool_attribute(value: bool) -> &'static str {
227    if value { "true" } else { "false" }
228}
229
230/// A public website header whose navigation collapses behind a menu toggle on narrow widths.
231///
232/// With the default `collapse: SiteHeaderCollapse::Panel`, server rendering keeps the navigation
233/// visible with the toggle hidden, like lumen-react before hydration. Once mounted, the toggle
234/// appears and opens the menu; Escape closes it and returns focus to the toggle, and following a
235/// navigation link closes it.
236///
237/// With `collapse: SiteHeaderCollapse::Sheet`, the panel is a native popover that the toggle and
238/// the close button inside it open and hide through the Popover API, so it works before hydration
239/// and closes on Escape and outside presses without a script. On web and desktop, a script then
240/// mirrors lumen-core's sheet controller: it reflects the popover in `data-state` and
241/// `aria-expanded`, moves focus to the close button when the sheet opens and back to the toggle
242/// when Escape or the close button hides it, and hides the sheet after a plain link click, when
243/// focus leaves it, and when the header grows wide enough to show the navigation inline. Dioxus
244/// Native has no Popover API, so keep `collapse` at `Panel` there.
245#[component]
246pub fn SiteHeader(props: SiteHeaderProps) -> Element {
247    let generated = use_unique_id();
248    let panel_id = format!(
249        "{}-panel",
250        props
251            .id
252            .clone()
253            .unwrap_or_else(|| format!("ui-site-header-{generated}"))
254    );
255    let sheet = props.collapse == SiteHeaderCollapse::Sheet;
256    let mut open = use_signal(|| false);
257    let mut enhanced = use_signal(|| false);
258    let mut toggle_node = use_signal(|| None::<Rc<MountedData>>);
259    let scrolled = use_scroll_past(Some(8.0));
260    // The native popover owns the sheet's open state; this mirrors it once the script reports.
261    let sheet_open = use_site_header_sheet(sheet.then_some(panel_id.as_str()));
262
263    use_effect(move || enhanced.set(true));
264
265    let enhanced = enhanced();
266    // The panel disclosure state, which a sheet never uses.
267    let is_open = !sheet && open();
268    let sheet_open = sheet_open();
269    let class = compose([
270        Some("ui-site-header"),
271        props.sticky.then_some("ui-site-header--sticky"),
272        sheet.then_some("ui-site-header--sheet"),
273        props.class.as_deref(),
274    ]);
275    let (state, expanded) = if sheet {
276        (sheet_open.map(open_state), sheet_open.map(bool_attribute))
277    } else {
278        (
279            enhanced.then_some(open_state(is_open)),
280            Some(bool_attribute(is_open)),
281        )
282    };
283    let brand = props.brand.filter(|slot| has_slot(Some(slot)));
284    let actions = props.actions.filter(|slot| has_slot(Some(slot)));
285    let menu_label = optional_text(Some(&props.menu_label)).unwrap_or_else(|| "Menu".to_string());
286    let close_label =
287        optional_text(Some(&props.close_label)).unwrap_or_else(|| "Close menu".to_string());
288    let nav_label = optional_text(Some(&props.nav_label)).unwrap_or_else(|| "Main".to_string());
289    let toggle_target = sheet.then(|| panel_id.clone());
290    let close_target = panel_id.clone();
291
292    rsx! {
293        header {
294            class,
295            "data-collapse": sheet.then_some(props.collapse.as_str()),
296            "data-collapsible": (!sheet && enhanced).then_some("true"),
297            "data-scrolled": (scrolled() == Some(true)).then_some(""),
298            "data-slot": "site-header",
299            "data-state": state,
300            "data-sticky": if props.sticky { "true" } else { "false" },
301            "data-ui-owner": "dioxus",
302            "data-ui-site-header": "true",
303            id: props.id,
304            ..props.attributes,
305            div {
306                class: "ui-site-header__inner",
307                "data-slot": "site-header-inner",
308                onkeydown: move |event| {
309                    // The Popover API closes a sheet on Escape; only the panel needs this.
310                    if !sheet && is_escape(&event) && open() {
311                        event.prevent_default();
312                        open.set(false);
313                        focus_element(toggle_node());
314                    }
315                },
316                if let Some(brand) = brand {
317                    div { class: "ui-site-header__brand", "data-slot": "site-header-brand", {brand} }
318                }
319                // A sheet toggle stays visible and works through `popovertarget` before hydration,
320                // and reports `aria-expanded` only once the script mirrors the popover.
321                button {
322                    aria_controls: panel_id.clone(),
323                    aria_expanded: expanded,
324                    class: "ui-site-header__toggle",
325                    "data-slot": "site-header-toggle",
326                    "data-ui-site-header-toggle": "true",
327                    hidden: !sheet && !enhanced,
328                    popovertarget: toggle_target,
329                    r#type: "button",
330                    onclick: move |_| {
331                        if !sheet {
332                            open.toggle();
333                        }
334                    },
335                    onmounted: move |event| toggle_node.set(Some(event.data())),
336                    span { aria_hidden: "true", class: "ui-site-header__toggle-icon" }
337                    span { class: "ui-site-header__toggle-label", "{menu_label}" }
338                }
339                div {
340                    class: "ui-site-header__panel",
341                    "data-slot": "site-header-panel",
342                    id: panel_id,
343                    popover: sheet.then_some("auto"),
344                    if sheet {
345                        button {
346                            class: "ui-site-header__close",
347                            "data-slot": "site-header-close",
348                            popovertarget: close_target,
349                            popovertargetaction: "hide",
350                            r#type: "button",
351                            span { aria_hidden: "true", class: "ui-site-header__toggle-icon" }
352                            span { class: "ui-site-header__toggle-label", "{close_label}" }
353                        }
354                    }
355                    nav {
356                        aria_label: nav_label,
357                        class: "ui-site-header__nav",
358                        "data-slot": "site-header-nav",
359                        // Following a navigation link should leave the page with the menu closed.
360                        onclick: move |_| {
361                            if *open.peek() {
362                                open.set(false);
363                            }
364                        },
365                        {props.children}
366                    }
367                    if let Some(actions) = actions {
368                        div { class: "ui-site-header__actions", "data-slot": "site-header-actions", {actions} }
369                    }
370                }
371            }
372        }
373    }
374}
375
376/// How a [`Sidebar`] behaves on narrow viewports.
377#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
378#[non_exhaustive]
379pub enum SidebarCollapse {
380    /// The sidebar always stays in the page flow.
381    #[default]
382    None,
383    /// The sidebar stays in the page flow from 48rem and becomes an off-canvas popover sheet
384    /// below that width, opened by a [`SidebarTrigger`]. The sidebar needs an `id`.
385    Sheet,
386}
387
388impl SidebarCollapse {
389    /// The collapse name, like lumen-core's `LumenSidebarCollapse`.
390    pub fn as_str(self) -> &'static str {
391        match self {
392            Self::None => "none",
393            Self::Sheet => "sheet",
394        }
395    }
396}
397
398/// Props for [`Sidebar`].
399#[derive(Props, Clone, PartialEq)]
400pub struct SidebarProps {
401    /// Surface styling or hooks only.
402    #[props(default)]
403    pub variant: NavigationVariant,
404    /// Glass intensity.
405    #[props(default, into)]
406    pub glass: Glass,
407    /// Narrow-viewport behavior. `Sheet` requires an `id`, which [`SidebarTrigger`] targets.
408    #[props(default)]
409    pub collapse: SidebarCollapse,
410    /// Extra classes appended after the Lumen classes.
411    #[props(into)]
412    pub class: Option<String>,
413    /// Any other `<aside>` or global attribute. Set `id` when `collapse` is `Sheet`.
414    #[props(extends = GlobalAttributes, extends = aside)]
415    pub attributes: Vec<Attribute>,
416    /// Navigation, usually a `nav` of [`SidebarItem`]s.
417    pub children: Element,
418}
419
420/// The side panel of an application shell.
421///
422/// With `collapse: SidebarCollapse::Sheet` and an `id`, the sidebar is a native popover: it stays
423/// in the page flow from 48rem and is hidden below that width until a [`SidebarTrigger`] opens it
424/// as a drawer with a scrim. The browser's Popover API opens and closes it, closes it on Escape
425/// and outside presses, and returns focus, without a script. Unlike lumen-react, the sheet does
426/// not close itself after a link click inside it or when the viewport grows past 48rem. Dioxus
427/// Native has no Popover API, so keep `collapse` at `None` there: below 48rem the sheet would stay
428/// hidden.
429#[component]
430pub fn Sidebar(props: SidebarProps) -> Element {
431    let sheet = props.collapse == SidebarCollapse::Sheet;
432    let unstyled = props.variant.modifier("ui-sidebar");
433    let glass = props.glass.class("ui-sidebar");
434    let class = compose([
435        Some("ui-sidebar"),
436        sheet.then_some("ui-sidebar--sheet"),
437        unstyled.as_deref(),
438        glass.as_deref(),
439        props.class.as_deref(),
440    ]);
441
442    rsx! {
443        aside {
444            class,
445            "data-collapse": sheet.then_some(props.collapse.as_str()),
446            "data-slot": "sidebar",
447            "data-surface": surface(props.glass),
448            // lumen-react marks the sheet so Lumen's web runtime leaves it alone.
449            "data-ui-owner": sheet.then_some("dioxus"),
450            "data-ui-sidebar": sheet.then_some(""),
451            "data-variant": props.variant.as_str(),
452            popover: sheet.then_some("auto"),
453            ..props.attributes,
454            {props.children}
455        }
456    }
457}
458
459/// Props for [`SidebarItem`].
460#[derive(Props, Clone, PartialEq)]
461pub struct SidebarItemProps {
462    /// Visible link text.
463    #[props(into)]
464    pub label: String,
465    /// Destination. Unsafe values (executable schemes, credentials) are dropped.
466    #[props(into)]
467    pub href: Option<String>,
468    /// Secondary line under the label.
469    #[props(into)]
470    pub description: Option<String>,
471    /// Leading icon, hidden from assistive technology because the label names the link.
472    pub icon: Option<Element>,
473    /// Trailing count or short status, read as "Label (badge)". Blank values render nothing.
474    #[props(into)]
475    pub badge: Option<String>,
476    /// Marks the link as the current page.
477    #[props(default)]
478    pub current: bool,
479    /// Renders the link without `href` and with `aria-disabled`, so it cannot navigate or take
480    /// focus.
481    #[props(default)]
482    pub disabled: bool,
483    /// Click handler. Call `prevent_default` on the event to route inside the app instead.
484    pub onclick: Option<EventHandler<MouseEvent>>,
485    /// Extra classes appended after the Lumen class.
486    #[props(into)]
487    pub class: Option<String>,
488    /// Any other `<a>` or global attribute.
489    #[props(extends = GlobalAttributes, extends = a)]
490    pub attributes: Vec<Attribute>,
491}
492
493/// A sidebar navigation link with an optional icon, a two-line label, and a trailing count.
494#[component]
495pub fn SidebarItem(props: SidebarItemProps) -> Element {
496    let class = compose([Some("ui-sidebar-item"), props.class.as_deref()]);
497    let href = if props.disabled {
498        None
499    } else {
500        props.href.as_deref().and_then(resolve_safe_href)
501    };
502    let badge = props
503        .badge
504        .as_deref()
505        .map(str::trim)
506        .filter(|badge| !badge.is_empty())
507        .map(str::to_string);
508    let description = props
509        .description
510        .filter(|description| !description.trim().is_empty());
511    let icon = props.icon.filter(|slot| has_slot(Some(slot)));
512    let onclick = props.onclick;
513    let disabled = props.disabled;
514
515    rsx! {
516        a {
517            aria_current: props.current.then_some("page"),
518            aria_disabled: disabled.then_some("true"),
519            class,
520            "data-slot": "sidebar-item",
521            href,
522            role: disabled.then_some("link"),
523            onclick: move |event| {
524                if disabled {
525                    return;
526                }
527
528                if let Some(handler) = onclick {
529                    handler.call(event);
530                }
531            },
532            ..props.attributes,
533            if let Some(icon) = icon {
534                span { aria_hidden: "true", class: "ui-sidebar-item__icon", "data-slot": "sidebar-item-icon", {icon} }
535            }
536            span { class: "ui-sidebar-item__text", "data-slot": "sidebar-item-text",
537                span { class: "ui-sidebar-item__label", "data-slot": "sidebar-item-label", "{props.label}" }
538                if let Some(description) = description {
539                    span { class: "ui-sidebar-item__description", "data-slot": "sidebar-item-description", "{description}" }
540                }
541            }
542            if let Some(badge) = badge {
543                span { class: "ui-sidebar-item__badge", "data-slot": "sidebar-item-badge",
544                    span { class: "ui-sr-only", " (" }
545                    "{badge}"
546                    span { class: "ui-sr-only", ")" }
547                }
548            }
549        }
550    }
551}
552
553/// What a [`SidebarTrigger`] does to its sheet.
554#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
555#[non_exhaustive]
556pub enum SidebarTriggerAction {
557    /// Opens the sheet, or closes it when it is open. For a trigger outside the sheet.
558    #[default]
559    Toggle,
560    /// Closes the sheet. For a trigger inside it.
561    Close,
562}
563
564impl SidebarTriggerAction {
565    /// The `data-action` value.
566    pub fn as_str(self) -> &'static str {
567        match self {
568            Self::Toggle => "toggle",
569            Self::Close => "close",
570        }
571    }
572
573    /// The `popovertargetaction` value, like `resolveLumenSidebarTriggerAction`.
574    fn popover_target_action(self) -> &'static str {
575        match self {
576            Self::Toggle => "toggle",
577            Self::Close => "hide",
578        }
579    }
580
581    /// The default accessible name, like `resolveLumenSidebarTriggerLabel`.
582    fn default_label(self) -> &'static str {
583        match self {
584            Self::Toggle => "Menu",
585            Self::Close => "Close menu",
586        }
587    }
588}
589
590/// Props for [`SidebarTrigger`].
591#[derive(Props, Clone, PartialEq)]
592pub struct SidebarTriggerProps {
593    /// The `id` of a [`Sidebar`] rendered with `collapse: SidebarCollapse::Sheet`.
594    #[props(into)]
595    pub target: String,
596    /// `Toggle` opens and closes the sheet from outside it; `Close` hides it from inside.
597    #[props(default)]
598    pub action: SidebarTriggerAction,
599    /// Accessible name of the default icon. Defaults to `Menu`, or `Close menu` for `Close`.
600    /// Ignored when the trigger has children.
601    #[props(into)]
602    pub label: Option<String>,
603    /// Disables the button.
604    #[props(default)]
605    pub disabled: bool,
606    /// Click handler.
607    pub onclick: Option<EventHandler<MouseEvent>>,
608    /// Extra classes appended after the Lumen classes.
609    #[props(into)]
610    pub class: Option<String>,
611    /// Any other `<button>` or global attribute. `target` and `action` already set
612    /// `popovertarget` and `popovertargetaction`.
613    #[props(extends = GlobalAttributes, extends = button)]
614    pub attributes: Vec<Attribute>,
615    /// Visible content. It replaces the default icon and its visually hidden label.
616    pub children: Element,
617}
618
619/// A ghost [`Button`](crate::Button) that opens or closes a [`Sidebar`] rendered with
620/// `collapse: SidebarCollapse::Sheet`.
621///
622/// It is wired with `popovertarget`, so the browser's Popover API runs it without a script. It is
623/// an icon button with a visually hidden label unless it has children, which render at the default
624/// button size instead. Lumen's CSS hides the trigger from 48rem and in browsers without the
625/// Popover API.
626#[component]
627pub fn SidebarTrigger(props: SidebarTriggerProps) -> Element {
628    let action = props.action;
629    let has_children = has_content(&props.children);
630    let size = if has_children {
631        ButtonSize::Default
632    } else {
633        ButtonSize::Icon
634    };
635    let class = compose([
636        Some("ui-button"),
637        Some("ui-button--ghost"),
638        Some(size.class()),
639        Some("ui-sidebar-trigger"),
640        props.class.as_deref(),
641    ]);
642    let label =
643        optional_text(props.label.as_deref()).unwrap_or_else(|| action.default_label().to_string());
644    let controls = props.target.clone();
645    let onclick = props.onclick;
646
647    // Rendered directly rather than through `Button` so `data-slot` names the trigger, like
648    // `DialogClose`.
649    rsx! {
650        button {
651            aria_controls: controls,
652            class,
653            "data-action": action.as_str(),
654            "data-slot": "sidebar-trigger",
655            disabled: props.disabled,
656            popovertarget: props.target,
657            popovertargetaction: action.popover_target_action(),
658            r#type: "button",
659            onclick: move |event| {
660                if let Some(handler) = onclick {
661                    handler.call(event);
662                }
663            },
664            ..props.attributes,
665            span { class: "ui-button__content",
666                if has_children {
667                    {props.children}
668                } else {
669                    span { aria_hidden: "true", class: "ui-sidebar-trigger__icon" }
670                    span { class: "ui-sr-only", "{label}" }
671                }
672            }
673        }
674    }
675}
676
677/// Props for [`ContextNavigation`].
678#[derive(Props, Clone, PartialEq)]
679pub struct ContextNavigationProps {
680    /// A context switcher, such as a platform or version select, shown before the navigation.
681    pub context: Option<Element>,
682    /// Surface styling or hooks only.
683    #[props(default)]
684    pub variant: NavigationVariant,
685    /// Extra classes appended after the Lumen classes.
686    #[props(into)]
687    pub class: Option<String>,
688    /// Any other `<div>` or global attribute.
689    #[props(extends = GlobalAttributes, extends = div)]
690    pub attributes: Vec<Attribute>,
691    /// The navigation for the selected context, usually a [`NavigationMenu`](crate::NavigationMenu).
692    pub children: Element,
693}
694
695/// Navigation that changes with a context switcher, such as docs for one platform.
696#[component]
697pub fn ContextNavigation(props: ContextNavigationProps) -> Element {
698    let unstyled = props.variant.modifier("ui-context-navigation");
699    let class = compose([
700        Some("ui-context-navigation"),
701        unstyled.as_deref(),
702        props.class.as_deref(),
703    ]);
704    let context = props.context.filter(|slot| has_slot(Some(slot)));
705
706    rsx! {
707        div {
708            class,
709            "data-slot": "context-navigation",
710            "data-variant": props.variant.as_str(),
711            ..props.attributes,
712            if let Some(context) = context {
713                div {
714                    class: "ui-context-navigation__context",
715                    "data-slot": "context-navigation-context",
716                    {context}
717                }
718            }
719            {props.children}
720        }
721    }
722}