Biji UI

Menubar

Displays a menu to the user, which can consist of links or functions, triggered by a button.

Installation

biji-ui = { version = "0.5.0", features = ["menubar"] }

Usage

use std::time::Duration;
use leptos::{portal::Portal, prelude::*};
use biji_ui::components::menubar;

#[component]
pub fn MyMenubar() -> impl IntoView {
    view! {
        <menubar::Root class="flex" allow_item_loop=true>
            <menubar::Menu
                class="relative"
                positioning={menubar::Positioning::BottomStart}
                hide_delay={Duration::from_millis(200)}
            >
                <menubar::Trigger class="py-1.5 px-2 text-sm rounded-sm hover:bg-accent">
                    "File"
                </menubar::Trigger>
                <Portal>
                    <menubar::Content
                        class="z-50 flex flex-col p-1 w-56 rounded-md border shadow-md bg-background"
                        show_class="opacity-100 transition duration-150 ease-in"
                        hide_class="opacity-0 transition duration-200 ease-out"
                    >
                        <menubar::Item class="py-1.5 px-2 text-sm rounded-sm data-[highlighted]:bg-muted hover:bg-accent">
                            "New file"
                        </menubar::Item>
                        <menubar::Item
                            disabled=true
                            class="py-1.5 px-2 text-sm rounded-sm data-[disabled]:opacity-50 data-[disabled]:pointer-events-none"
                        >
                            "New window"
                        </menubar::Item>
                    </menubar::Content>
                </Portal>
            </menubar::Menu>
            <menubar::Menu
                class="relative"
                positioning={menubar::Positioning::BottomStart}
                hide_delay={Duration::from_millis(200)}
            >
                <menubar::Trigger class="py-1.5 px-2 text-sm rounded-sm hover:bg-accent">
                    "Edit"
                </menubar::Trigger>
                <Portal>
                    <menubar::Content
                        class="z-50 flex flex-col p-1 w-56 rounded-md border shadow-md bg-background"
                        show_class="opacity-100 transition duration-150 ease-in"
                        hide_class="opacity-0 transition duration-200 ease-out"
                    >
                        <menubar::Item class="py-1.5 px-2 text-sm rounded-sm data-[highlighted]:bg-muted hover:bg-accent">
                            "Undo"
                        </menubar::Item>
                        <menubar::Item class="py-1.5 px-2 text-sm rounded-sm data-[highlighted]:bg-muted hover:bg-accent">
                            "Redo"
                        </menubar::Item>
                    </menubar::Content>
                </Portal>
            </menubar::Menu>
        </menubar::Root>
    }
}

API Reference

Root

NameTypeDefaultDescription
classString""CSS class applied to the menubar root element.
allow_menu_loopboolfalseWhen true, horizontal keyboard navigation wraps from the last menu back to the first and vice versa.
allow_item_loopboolfalseWhen true, vertical keyboard navigation within a menu wraps from the last item back to the first.
prevent_scrollboolfalseWhen true, prevents the page from scrolling while any menu is open.

Menu

NameTypeDefaultDescription
classString""CSS class applied to the individual menu container.
disabledboolfalseDisables the menu trigger when true.
positioningPositioningBottomStartWhere to position the menu content relative to its trigger.
avoid_collisionsAvoidCollisionsFlipHow the menu reacts when it would overflow the viewport.
hide_delayDuration200msHow long to wait before unmounting the content after closing begins. Should match your CSS transition duration.

Trigger

NameTypeDefaultDescription
classString""CSS class applied to the menu trigger element.

Content

NameTypeDefaultDescription
classString""CSS class applied in both open and closed states.
show_classString""CSS class applied when the menu is open.
hide_classString""CSS class applied while the menu is closing.

Item

NameTypeDefaultDescription
classString""CSS class applied to the item element.
disabledboolfalsePrevents interaction with the item and applies data-disabled.

SubMenu

NameTypeDefaultDescription
classString""CSS class applied to the submenu wrapper element.
disabledboolfalseDisables the submenu trigger and prevents it from opening.
positioningPositioningRightStartWhere to position the submenu content.
hide_delayDuration200msHow long to wait before unmounting the submenu content after closing begins.

SubMenuTrigger

NameTypeDefaultDescription
classString""CSS class applied to the submenu trigger element.

SubMenuContent

NameTypeDefaultDescription
classString""CSS class applied in both open and closed states.
show_classString""CSS class applied when the menu is open.
hide_classString""CSS class applied while the menu is closing.

Positioning

NameTypeDefaultDescription
TopStartPositioning Above the trigger, aligned to its left edge.
TopPositioning Above the trigger, centered.
TopEndPositioning Above the trigger, aligned to its right edge.
RightStartPositioning To the right of the trigger, aligned to its top edge.
RightPositioning To the right of the trigger, centered.
RightEndPositioning To the right of the trigger, aligned to its bottom edge.
BottomStartPositioningdefaultBelow the trigger, aligned to its left edge.
BottomPositioning Below the trigger, centered.
BottomEndPositioning Below the trigger, aligned to its right edge.
LeftStartPositioning To the left of the trigger, aligned to its top edge.
LeftPositioning To the left of the trigger, centered.
LeftEndPositioning To the left of the trigger, aligned to its bottom edge.

AvoidCollisions

NameTypeDefaultDescription
FlipAvoidCollisionsdefaultKeeps the preferred side. Flips to the opposite side if it does not fit. If neither fits, uses whichever has more space.
AutoPlaceAvoidCollisions Always places the menu on the side with the most available space, regardless of the preferred positioning.
NoneAvoidCollisions No collision detection. Always uses the exact positioning specified.

Data Attributes

AttributeDescription
data-highlightedPresent on Trigger, Item, and SubMenuTrigger when they have keyboard focus or are hovered.
data-disabledPresent on Trigger, Item, and SubMenuTrigger when disabled is true.
data-openPresent on Trigger with value true when its menu is open.

Keyboard Navigation

KeyDescription
ArrowLeftMoves focus to the previous menu trigger in the bar (follows the open state).
ArrowRightMoves focus to the next menu trigger in the bar (follows the open state).
ArrowDownOpens the focused menu and moves focus to its first item.
ArrowUpOpens the focused menu and moves focus to its last item.
EnterOpens the focused menu trigger and focuses the first item.
EscapeCloses all open menus and returns focus to the menubar.