Menu
A list of actions that opens from a button, such as Edit, Duplicate, and Delete.
Usage
import { Menu } from '@leverui/components';
<Menu.Root>
<Menu.Trigger />
<Menu.Positioner>
<Menu.Content>
<Menu.Item />
<Menu.ItemGroup>
<Menu.Item />
</Menu.ItemGroup>
<Menu.Separator />
<Menu.Arrow />
<Menu.CheckboxItem>
<Menu.ItemIndicator />
</Menu.CheckboxItem>
<Menu.RadioItemGroup>
<Menu.RadioItem>
<Menu.ItemIndicator />
</Menu.RadioItem>
</Menu.RadioItemGroup>
</Menu.Content>
</Menu.Positioner>
</Menu.Root>
Examples
Controlled
Use the open and onOpenChange prop to control the visibility of the menu.
open: falseStore
An alternative way to control the menu is to use the RootProvider component and the useMenu store hook.
open: falseThis way you can access the menu state and methods from outside the menu.
Command
Use the Menu.ItemCommand component to display a command in the menu.
Context menu
Use the Menu.ContextTrigger component to create a context menu.
Group
Use the Menu.ItemGroup component to group related menu items.
Danger Item
Here's an example of how to style a menu item that is used to delete an item.
Submenu
Here's an example of how to create a submenu.
Links
Pass the asChild prop to the Menu.Item component to render a link.
When using custom router links, you need to set the navigate prop on the Menu.Root component.
import { Menu } from '@leverui/components';
import { useNavigate } from '@solidjs/router';
export function Demo() {
const navigate = useNavigate();
return (
<Menu.Root navigate={({ value }) => navigate(`/${value}`)}>
{/* ... */}
</Menu.Root>
);
}
Radio Items
Here's an example of how to create a menu with radio items.
Checkbox Items
Here's an example of how to create a menu with checkbox items.
Icon and Command
Compose the menu to include icons and commands.
Placement
Use the positioning.placement prop to control the placement of the menu.
Avatar
Here's an example that composes the Menu with the Avatar component to display a menu underneath an avatar.
Anchor Point
Use the positioning.anchorPoint prop to control the anchor point of the menu.
You can derive it from the getBoundingClientRect of a DOM element, or use something like DOMRect.fromRect({ x: 0, y: 0, width: 1, height: 1 }) to create a new rect.
Mixed Layout
Here's an example of how to create a mixed layout of menu items. In this layout, the top horizontal menu includes common menu items.
Overflow
When you have a long list of menu items, you can set max-height with the style prop on the Menu.Content to create a scrollable menu.
The content has a default
max-height: var(--available-height), which is the maximum available height for the content relative to the viewport.
Hide When Detached
When the menu is rendered in an scrolling container, set the positioning.hideWhenDetached to true to hide the menu when the trigger is scrolled out of view.
Item0
Item1
Item2
Item3
Item4
Item5
Open From Dialog
To use the Menu within a Dialog, you need to avoid portalling the Menu.Positioner to the document's body.
-<Portal>
<Menu.Positioner>
<Menu.Content>
{/* ... */}
</Menu.Content>
</Menu.Positioner>
-</Portal>
If you have set scrollBehavior="inside" on the Dialog, you need to:
- Set the menu positioning to
fixedto avoid the menu from being clipped by the dialog. - Set
hideWhenDetachedtotrueto hide the menu when the trigger is scrolled out of view.
<Menu.Root positioning={{ strategy: 'fixed', hideWhenDetached: true }}>
{/* ... */}
</Menu.Root>
Open from Dialog (Nested)
When a Menu item opens a Dialog that contains another Menu, use createOverlay to avoid unexpected close behavior caused by event bubbling.
Guide
Styling highlighted items
An item is highlighted while the pointer is over it or the keyboard has moved
to it. Style [data-highlighted].
<Menu.Item value="custom" class="custom-highlight">
Custom highlighted item
</Menu.Item>
.custom-highlight[data-highlighted] {
background: var(--leverui-colors-blue-500);
color: white;
}
Styling open state
Style [data-state='open'] on the trigger.
<Menu.Trigger
asChild={(getProps) => (
<Button {...getProps()} class="custom-trigger">
Menu
</Button>
)}
/>
.custom-trigger[data-state='open'] {
background: var(--leverui-colors-blue-500);
color: white;
}
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
closeOnSelect | true | boolean | Close when an item is selected. |
composite | true | boolean | Combined with another composite widget, such as a combobox or tabs. |
lazyMount | true | boolean | Render the content only once the menu first opens. |
typeahead | true | boolean | Typing moves to the matching item. |
unmountOnExit | true | boolean | Remove the content from the page on close. |
variant | subtle | 'subtle' | 'solid' | Menu style. |
size | md | 'sm' | 'md' | Menu size. |
unstyled | — | boolean | Turn off the default styles. |
anchorPoint | — | Point | Point to position the menu at. The context menu or button trigger can set it. |
aria-label | — | string | Accessible label. |
defaultHighlightedValue | — | string | Item highlighted at first. Uncontrolled. |
defaultOpen | — | boolean | Open at first. Uncontrolled. |
defaultTriggerValue | — | string | Trigger value at first. Uncontrolled. |
highlightedValue | — | string | Highlighted item. Controlled. |
id | — | string | Unique id. |
ids | — | Partial<{ trigger: string | ((value?: string | undefined) => string); contextTrigger: string | ((value?: string | undefined) => string); content: string; groupLabel: (id: string) => string; group: (id: string) => string; positioner: string; arrow: string }> | Ids of the menu's elements. For combining with other components. |
immediate | — | boolean | Apply a present change right away, not on the next frame. |
loopFocus | false | boolean | Wrap keyboard navigation from the last item to the first. |
navigate | — | (details: NavigateDetails) => void | Called to go to the selected item when it is a link. |
onEscapeKeyDown | — | (event: KeyboardEvent) => void | Called when Escape is pressed. |
onExitComplete | — | VoidFunction | Called when the closing animation ends. |
onFocusOutside | — | (event: FocusOutsideEvent) => void | Called when focus moves outside. |
onHighlightChange | — | (details: HighlightChangeDetails) => void | Called when the highlighted item changes. |
onInteractOutside | — | (event: InteractOutsideEvent) => void | Called on an interaction outside. |
onOpenChange | — | (details: OpenChangeDetails) => void | Called when the menu opens or closes. |
onPointerDownOutside | — | (event: PointerDownOutsideEvent) => void | Called when the pointer is pressed outside. |
onRequestDismiss | — | (event: LayerDismissEvent) => void | Called when this layer closes because a parent layer closed. |
onSelect | — | (details: SelectionDetails) => void | Called when an item is selected. |
onTriggerValueChange | — | (details: TriggerValueChangeDetails) => void | Called when the trigger value changes. |
open | — | boolean | Open state. Controlled. |
positioning | — | PositioningOptions | Placement options. |
present | — | boolean | Present state. Controlled. |
skipAnimationOnMount | false | boolean | Skip the animation on first show. |
triggerValue | — | string | Trigger value. Controlled. |
Item
| Prop | Default | Type | Description |
|---|---|---|---|
value | — | string | Unique value for the item. Required. |
as | div | Native tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the item's props. |
closeOnSelect | — | boolean | Close the menu when this item is selected. |
disabled | — | boolean | Disable the item. |
onSelect | — | VoidFunction | Called when the item is selected. |
valueText | — | string | Text matched when someone types. Default: the item's text content. |
Give each item a value that does not change and a clear name. Run actions in
onSelect. Links can be native a elements. Do not put a navigation link that
must stay visible only in a menu.
Customize with CSS
Styles ship for size="sm" | "md" and variant="subtle" | "solid". Both take
one value, an array with a value per breakpoint, or an object keyed by
breakpoint name.
Each part has a leverui-menu__<part> class, a part attribute (such as
data-menu-content), and size and variant attributes. Menus in a portal and
nested menus keep them.
<Menu.Content class="account-menu" style={{ 'max-height': '20rem' }}>
...
</Menu.Content>
.account-menu {
--leverui-menu-background: var(--leverui-colors-bg-subtle);
}
Variables: --leverui-menu-background, --leverui-menu-z-index,
--leverui-menu-highlight-background, --leverui-menu-highlight-color.
unstyled on Root or a part turns off its default styles.
Explorer
Click a part name in the sidebar to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts