Biji UI

Dropdown Menu

Displays a menu of items that users can select from when triggered.

Installation

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

Usage

use std::time::Duration;
use leptos::prelude::*;
use biji_ui::components::menu;

#[component]
pub fn MyDropdownMenu() -> impl IntoView {
    view! {
        <menu::Menu
            positioning={menu::Positioning::Bottom}
            hide_delay={Duration::from_millis(200)}
        >
            <menu::Trigger class="rounded border px-3 py-1.5 text-sm">
                "Open menu"
            </menu::Trigger>
            <menu::Content
                class="z-50 flex flex-col p-1 w-48 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"
            >
                <menu::Item class="px-2 py-1.5 text-sm rounded-sm hover:bg-accent data-[highlighted]:bg-muted">
                    "Profile"
                </menu::Item>
                <menu::Item class="px-2 py-1.5 text-sm rounded-sm hover:bg-accent data-[highlighted]:bg-muted">
                    "Settings"
                </menu::Item>
                <menu::Item
                    disabled=true
                    class="px-2 py-1.5 text-sm rounded-sm data-[disabled]:opacity-50 data-[disabled]:pointer-events-none"
                >
                    "Billing"
                </menu::Item>
            </menu::Content>
        </menu::Menu>
    }
}

API Reference

Menu

NameTypeDefaultDescription
classString""CSS class applied to the menu root element.
disabledboolfalseDisables the entire menu and its trigger when true.
allow_loopboolfalseWhen true, keyboard navigation wraps from the last item back to the first and vice versa.
positioningPositioningBottomStartWhere to position the content relative to the trigger.
hide_delayDuration200msHow long to wait before unmounting the content after closing begins. Should match your CSS transition duration.
avoid_collisionsAvoidCollisionsFlipHow the menu reacts when it would overflow the viewport.
prevent_scrollboolfalseWhen true, prevents the page from scrolling while the menu is open.

Trigger

NameTypeDefaultDescription
classString""CSS class applied to the 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 the value true when the menu is open.

Keyboard Navigation

KeyDescription
ArrowDownMoves focus to the next item in the menu.
ArrowUpMoves focus to the previous item in the menu.
ArrowRightOpens the focused submenu and moves focus to its first item.
ArrowLeftCloses the current submenu and returns focus to its trigger.
EnterActivates the focused item (clicks the inner button or link).
EscapeCloses the menu.